我是一名有用的助手,可以为您翻译文本。
我有一个泛型类,在其中我想要使用泛型类型实例化一个对象。我想要使用类型的构造函数参数。
我的代码:
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': 在创建变量类型的实例时无法提供参数
有什么想法可以解决这个问题吗?
Some_Base_Class
上创建一个公共属性,然后您可以执行以下操作:new T() { SomeProperty = "value" }
。 - nZeus