假设在C#中myType是对某些人的引用Type.myType仅使用,是否可以创建一个List对象myType?
例如,在下面的代码中,虽然它是错误的,但我想实例化via
 new List  .<myType> ( ) 
using System ;
using System.Reflection ;
using System.Collections.Generic ;
class MyClass
    {
    }
class MainClass
    {
    public static void Main ( string [] args )
        {
        Type  myType  =  typeof ( MyClass ) ;
        List < myType >  myList  =  new List < myType > ( ) ;
        }
    }
你可以使用Reflection做到这一点:
Type typeList = typeof(List<>);
Type actualType = typeList.MakeGenericType(myType);
object obj = Activator.CreateInstance(actualType);
| 归档时间: | 
 | 
| 查看次数: | 3088 次 | 
| 最近记录: |