使用类型生成器我可以动态创建类型。是否可以从匿名类型执行此操作?
到目前为止我有这个;
//CreateType generates a type (so that I can set it's properties once instantiated) from an //anonymous object. I am not interested in the initial value of the properties, just the Type.
Type t = CreateType(new {Name = "Ben", Age = 23});
var myObject = Activator.CreateInstance(t);
现在可以使用类型“t”作为类型参数吗?
我的方法:
public static void DoSomething<T>() where T : new()
{
}
我想使用动态创建的类型“t”来调用这个方法。这样我就可以打电话;
DoSomething<t>(); //This won't work obviously