简体   繁体   中英

Pass parameter through constructor in generic constraints

I want to pass parameter in constructor into generic function with constraints. Is it possible to create instance of T with parameters? Something like list.Add(new T(1));

namespace ConsoleApplication
{
class Program
{
    static void Main(string[] args)
    {
        List<Base> list = new List<Base>();
        AddElement<Quick>(list,5);
        AddElement<Range>(list, 5); 
        Console.WriteLine(list.Count);
        Console.ReadKey();
    }
    public static void AddElement<T>(List<Base> list, int number) where T : Base, new ()
    {
       for (int i = 0; i < number; i++)
       {
           //do not working
           list.Add(new T(i));
       }
    }
}
public abstract class Base
{

}

public class Quick:Base
{
    private int ID;
    public Quick()
    {

    }
    public Quick(int ID)
    {
        this.ID = ID;
    }
}

public class Range : Base
{
    private int ID;
    public Range()
    {

    }
    public Range(int ID)
    {
        this.ID = ID;
    }
}
}

The usual way around this is to pass a factory method or Func<T> to the method:

public static void AddElement<T>(List<Base> list, int number, Func<T> factory) where T : Base
{
    for (int i = 0; i < number; i++)
    {
        list.Add(factory());
    }
}

You can use that with your Quick class like this:

var baseList = new List<Base>();
AddElement(baseList, 5, () => new Quick(1));

If you wanted to be able to pass the int constructor argument inside AddElement<T>() you can use a Func<int, T> instead like so:

public static void AddElement<T>(List<Base> list, int number, Func<int, T> factory) where T : Base
{
    for (int i = 0; i < number; i++)
    {
        list.Add(factory(i));
    }
}

Then call it like so:

var baseList = new List<Base>();
AddElement(baseList, 5, x => new Quick(x));

No, you can only do that with types ( T ) that have a constructor which takes no arguments . From MSDN :

The type argument must have a public parameterless constructor.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM