【发布时间】:2011-06-20 11:18:21
【问题描述】:
我有一个泛型类,我想用泛型类型实例化一个对象。我想为该类型的构造函数使用一个参数。
我的代码:
public class GenericClass<T> where T : Some_Base_Class, new()
{
public static T SomeFunction(string s)
{
if (String.IsNullOrEmpty(s))
return new T(some_param);
}
}
我收到一个错误
new T(some_param)
'T': 创建变量实例时无法提供参数 输入
任何想法我该怎么做?
【问题讨论】:
-
this 是我能找到的最好的解决方法....
-
你会经常这样做吗?什么版本的 C#?
-
另一种方法是在
Some_Base_Class上创建一个公共属性,然后您可以执行以下操作:new T() { SomeProperty = "value" }