我试图在添加到列表时通过其构造函数创建一个T类型的新对象。

我得到一个编译错误:错误消息是:

'T':创建变量实例时不能提供参数

但是我的类确实有构造函数参数!我该怎么做呢?

public static string GetAllItems<T>(...) where T : new()
{
   ...
   List<T> tabListItems = new List<T>();
   foreach (ListItem listItem in listCollection) 
   {
       tabListItems.Add(new T(listItem)); // error here.
   } 
   ...
}

当前回答

如果你只是想用构造函数参数初始化一个成员字段或属性,在c# >= 3中你可以很容易地做到:

public static string GetAllItems<T>(...) where T : InterfaceOrBaseClass, new() 
{ 
   ... 
   List<T> tabListItems = new List<T>(); 
   foreach (ListItem listItem in listCollection)  
   { 
       tabListItems.Add(new T{ BaseMemberItem = listItem }); // No error, BaseMemberItem owns to InterfaceOrBaseClass. 
   }  
   ... 
} 

这和Garry Shutler说的一样,但我想补充一点。

当然,您可以使用属性技巧来做更多的事情,而不仅仅是设置字段值。 属性“set()”可以触发设置其相关字段所需的任何处理,以及对对象本身的任何其他需求,包括在使用对象之前检查是否要进行完整的初始化,模拟完整的构造(是的,这是一个丑陋的解决方法,但它克服了M$ new()的限制)。

我不能确定这是一个计划好的漏洞还是一个意外的副作用,但它确实有效。

有趣的是,微软的人给语言添加了新功能,却似乎没有做一个完整的副作用分析。 整个通用的东西就是一个很好的证据…

其他回答

这有点麻烦,当我说有点麻烦时,我可能是指讨厌,但假设你可以为你的参数化类型提供一个空构造函数,那么:

public static T GetTInstance<T>() where T: new()
{
    var constructorTypeSignature = new Type[] {typeof (object)};
    var constructorParameters = new object[] {"Create a T"};
    return (T) new T().GetType().GetConstructor(constructorTypeSignature).Invoke(constructorParameters);
}

将有效地允许您从带有参数的参数化类型构造一个对象。在这种情况下,我假设我想要的构造函数有一个类型object的参数。我们使用约束允许的空构造函数创建T的虚拟实例,然后使用反射获取它的其他构造函数之一。

使用c# 11中的静态抽象接口方法特性,可以使用工厂模式构建一个变通方案。例如:

public interface IFactory<TSelf> where TSelf : IFactory<TSelf> {
  static abstract TSelf New(int i, string s);
}

public struct Foo : IFactory<Foo> {

  public static Foo New(int i, string s) {
    return new Foo(i, s);
  }

  public readonly int I;
  public readonly string S;

  public Foo(int i, string s) {
    I = i;
    S = s;
  }
}

public static class Maker {
  public static T Make<T>(int i, string s) where T : IFactory<T> {
    return T.New(i, s);
  }
}

这种方法的一个限制是它只能用于您拥有的类,因为您需要在目标类上实现特定的工厂接口。

在。net 3.5和之后,你可以使用activator类:

(T)Activator.CreateInstance(typeof(T), args)

我认为必须用where语句约束T,以只允许具有新构造函数的对象。

现在它接受任何东西,包括没有它的对象。

因为没有人愿意发表“反思”的答案(我个人认为这是最好的答案),下面是:

public static string GetAllItems<T>(...) where T : new()
{
   ...
   List<T> tabListItems = new List<T>();
   foreach (ListItem listItem in listCollection) 
   {
       Type classType = typeof(T);
       ConstructorInfo classConstructor = classType.GetConstructor(new Type[] { listItem.GetType() });
       T classInstance = (T)classConstructor.Invoke(new object[] { listItem });

       tabListItems.Add(classInstance);
   } 
   ...
}

编辑:由于. net 3.5的Activator,这个答案已弃用。CreateInstance,但是它在旧的。net版本中仍然有用。