Dynamic type with lists in C #

I have a little problem for you guys.

I would like to do this:

Type[] classes = new Type[]{ Class1, Class2 };

foreach(Type t in classes){   
  List<t> list = new List<t>(); 
}

Is there any way to do this?

+3
source share
5 answers

You cannot assign a generic type at runtime because a genus type must be resolved at compile time.

You can create a generic type in a dynamic way using reflection, but if you don't produce hard code, all you get in return is an object.

, , , XY. , -, , , , .


, dynamic List<T> / , T:

using System;
using System.Collections.Generic;

namespace ConsoleApplication61
{
    class Program
    {
        static void Main(string[] args)
        {
            dynamic o = CreateGeneric(typeof(List<>), typeof(int));
            o.Add(1);
            Console.WriteLine(o[0]);
            Console.Read();
        }

        public static object CreateGeneric(Type generic, Type innerType, params object[] args)
        {
            System.Type specificType = generic.MakeGenericType(new System.Type[] { innerType });
            return Activator.CreateInstance(specificType, args);
        }
    }
}

Add . DLR - , 1 int, .

, , , ( ), ; ( IntelliSense), .

CreateGeneric.

.NET 4 CLR. @MartinLiversage, , . int List<int>, dynamic.


.NET 4 . . dynamic . , " ", " , ".

+5

:

foreach(Type t in classes)
{
   var listType = typeof(List<>).MakeGenericType(t);
   var instance = Activator.CreateInstance(listType);
}
+5

Type.MakeGenericType

, , ya: CodeRef

public static object CreateGeneric(Type generic, Type innerType, params object[] args)
{
    System.Type specificType = generic.MakeGenericType(new System.Type[] { innerType });
    return Activator.CreateInstance(specificType, args);
}

:

var o = CreateGeneric(typeof(List<>), t);

, ( - , ).

MethodInfo addMethod = o.GetType().GetMethod("Add");
addMethod.Invoke(o, new object[] { item.ToType(t) });

Generic, .

+2

:

Type[] classes = new Type[] { typeof(A), typeof(B) };           
foreach (Type t in classes)
{
     Type genericType = typeof(List<>).MakeGenericType(t);
     var list = Activator.CreateInstance(genericType);
}
+1

, , , - .

, , L.B , .

Perhaps this is the design you have. Think about these types, see what they have in common, and think about whether they come from the same base type or make both implementations of the same interface. In this case, you can create an instance of the list of objects of the base type / interface and work with them.

Just to give you a start:

    abstract class Vehicle {
        public int NumberOfWheels { get; set; }
    }

    class Car : Vehicle
    {
        public Car()
        {
            NumberOfWheels = 4;
        }
    }

    class Bicycle : Vehicle
    {
        public Bicycle()
        {
            NumberOfWheels = 2;
        }
    }

    static void Main(string[] args)
    {

        var v1 = new Car();
        var v2 = new Bicycle();

        var list = new List<Vehicle>();
        list.Add(v1);
        list.Add(v2);

        foreach (var v in list)
        {
            Console.WriteLine(v.NumberOfWheels);
        }

        Console.ReadKey();
    }
+1
source

All Articles