谁能详细解释下这段代码,主要是where T : new() 的用法

renmms 2011-04-27 03:34:46

protected static T CreateNewInstance<T>() where T : new()
{
return new T();
}


可以看懂,细节不知道是啥意思。比如 where T : new()

请问这是什么写法,请详细的讲一讲。这是lamd表达式吗?
...全文
848 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
jesse_hk 2011-12-21
  • 打赏
  • 举报
回复
何必再包一层?

用 Activator.CreateInstance<T>
机器人 2011-04-27
  • 打赏
  • 举报
回复
何必再包一层?

用 Activator.CreateInstance<T>
renmms 2011-04-27
  • 打赏
  • 举报
回复
解决方法:


protected static object CreateNewInstance<T>(string s)
{
return Activator.CreateInstance(typeof(T), new object[] { s });
}



谢谢大家指点!
advisd 2011-04-27
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 renmms 的回复:]
引用 4 楼 ojlovecd 的回复:
引用 2 楼 renmms 的回复:

还有一个问题,如果想实例化一个带参数的构造函数,怎么写?

C# code

protected static T CreateNewInstance<T>(string s) where T : new()
{
return new T(s);
}




这样写报错!

泛型不支……
[/Quote]
那楼主这样吧
protected static T CreateNewInstance<T>(T t) where T : new()
{
//t的其他操作。之后再还回出去
return t;

}




renmms 2011-04-27
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 yzhl002 的回复:]
有没有构造参数取决于你的返回类型,如果你直接写成T是不能给构造函数添加参数的,但是如果你现在有一个具体的类,比如Person,里面有一个构造方法Person(string name),那么你的代码可以写成

C# code

protected static Person CreateInstrance<T>(string a) where T : new()
{
……
[/Quote]

这样写编译不过。
advisd 2011-04-27
  • 打赏
  • 举报
回复
嗯,T表示泛型,本来可以为Object的。但是为了确保约束T的范围。后面用where T: new();
来约束,或者是需要这个泛型是无参的构造函数。
protected static T CreateNewInstance<T>(string s) where T : String
{
return new T();
}

yzhl002 2011-04-27
  • 打赏
  • 举报
回复
有没有构造参数取决于你的返回类型,如果你直接写成T是不能给构造函数添加参数的,但是如果你现在有一个具体的类,比如Person,里面有一个构造方法Person(string name),那么你的代码可以写成

protected static Person CreateInstrance<T>(string a) where T : new()
{
return new Person(a);
}
renmms 2011-04-27
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 ojlovecd 的回复:]
引用 2 楼 renmms 的回复:

还有一个问题,如果想实例化一个带参数的构造函数,怎么写?

C# code

protected static T CreateNewInstance<T>(string s) where T : new()
{
return new T(s);
}




这样写报错!

泛型不支持这种用法,只能约束T是具有无参构造函数的。……
[/Quote]

那请问还有什么办法是可以把类当作参数传递的?
我想把类名当作参数来传递一个方法,然后在方法里通过类名来实例化(构造函数有参),然后做其他的操作。

比如:

Class BaseClass
{
}

Class A: BaseClass
{
}

Class B : BaseClass
{
}

Method:

Void Method (类名数组)
{
\\Step 1 :实例化
\\ Step 2: 其他操作
}

如果把类名写个string类型的话 传递一个string 数组,通过反射可以实现,但是我觉得不太好
令狐苦瓜 2011-04-27
  • 打赏
  • 举报
回复
very helpful if your generic type must create an instance of the type parameter, as you cannot assume the format of custom constructors. Note that this constraint must be listed last on a multiconstrained type.
我姓区不姓区 2011-04-27
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 renmms 的回复:]

还有一个问题,如果想实例化一个带参数的构造函数,怎么写?

C# code

protected static T CreateNewInstance<T>(string s) where T : new()
{
return new T(s);
}




这样写报错!
[/Quote]
泛型不支持这种用法,只能约束T是具有无参构造函数的。
我姓区不姓区 2011-04-27
  • 打赏
  • 举报
回复
where T: new()
where后的称为泛型约束,这里约束泛型参数T必须具有无参的构造函数
renmms 2011-04-27
  • 打赏
  • 举报
回复
还有一个问题,如果想实例化一个带参数的构造函数,怎么写?


protected static T CreateNewInstance<T>(string s) where T : new()
{
return new T(s);
}



这样写报错!
isjoe 2011-04-27
  • 打赏
  • 举报
回复
T必须有无参构造函数

110,532

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧