我试图在添加到列表时通过其构造函数创建一个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.
   } 
   ...
}

当前回答

为了在函数中创建泛型类型的实例,必须使用"new"标志约束它。

public static string GetAllItems<T>(...) where T : new()

但是,只有当你想调用没有参数的构造函数时,这才会起作用。这里不是这样。相反,您必须提供另一个参数,允许基于参数创建对象。最简单的是函数。

public static string GetAllItems<T>(..., Func<ListItem,T> del) {
  ...
  List<T> tabListItems = new List<T>();
  foreach (ListItem listItem in listCollection) 
  {
    tabListItems.Add(del(listItem));
  }
  ...
}

然后你可以这样称呼它

GetAllItems<Foo>(..., l => new Foo(l));

其他回答

这在你的情况下行不通。你只能指定一个构造函数为空的约束:

public static string GetAllItems<T>(...) where T: new()

你可以通过定义这个接口来使用属性注入:

public interface ITakesAListItem
{
   ListItem Item { set; }
}

然后你可以改变你的方法如下:

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

另一种替代方法是JaredPar描述的Func方法。

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

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版本中仍然有用。

使用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);
  }
}

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

对象初始化器

如果你的带形参的构造函数除了设置属性之外没有做任何事情,你可以在c# 3中使用对象初始化器而不是调用构造函数来做这件事(这是不可能的,正如前面提到的):

public static string GetAllItems<T>(...) where T : new()
{
   ...
   List<T> tabListItems = new List<T>();
   foreach (ListItem listItem in listCollection) 
   {
       tabListItems.Add(new T() { YourPropertyName = listItem } ); // Now using object initializer
   } 
   ...
}

使用它,您也可以将任何构造函数逻辑放在默认(空)构造函数中。

Activator.CreateInstance ()

或者,你可以像这样调用Activator.CreateInstance():

public static string GetAllItems<T>(...) where T : new()
{
   ...
   List<T> tabListItems = new List<T>();
   foreach (ListItem listItem in listCollection) 
   {
        object[] args = new object[] { listItem };
        tabListItems.Add((T)Activator.CreateInstance(typeof(T), args)); // Now using Activator.CreateInstance
   } 
   ...
}

注意Activator。CreateInstance可能有一些性能开销,如果执行速度是最优先考虑的,并且另一个选项对您来说是可维护的,那么您可能希望避免这些开销。

为了在函数中创建泛型类型的实例,必须使用"new"标志约束它。

public static string GetAllItems<T>(...) where T : new()

但是,只有当你想调用没有参数的构造函数时,这才会起作用。这里不是这样。相反,您必须提供另一个参数,允许基于参数创建对象。最简单的是函数。

public static string GetAllItems<T>(..., Func<ListItem,T> del) {
  ...
  List<T> tabListItems = new List<T>();
  foreach (ListItem listItem in listCollection) 
  {
    tabListItems.Add(del(listItem));
  }
  ...
}

然后你可以这样称呼它

GetAllItems<Foo>(..., l => new Foo(l));