大今拉麵射了
public void 作法1()
{
//建立泛型List的Type物件 (注意:此還沒沒決定型別)
Type genericListType = typeof(List<>);
//決定型別,並且轉為Type物件
Type type = typeof(int);
//透過MakeGenericType取得已知型別的泛型型別之Type物件
Type specificListType = genericListType.MakeGenericType(type);
//建立實體
IList list = (IList)Activator.CreateInstance(specificListType);
list.Add(1);
}
public void 做法2()
{
//使用上更加的精簡
IList list = (IList)MakeGenericType(typeof(List<>), typeof(double));
list.Add(1);
}
//直接包成方法
public static object MakeGenericType(Type generics, params Type[] typeArguments)
{
generics = generics.MakeGenericType(typeArguments);
return Activator.CreateInstance(generics);
}