我正在尝试创建 IEnumerable<AnotherDynamicType>
类型的属性使用Reflection.Emit
.
我有一个辅助方法,可以很好地添加属性,接受类型作为输入:
public void AddProperty(TypeBuilder typeBuilder, string propertyName, Type type)
{
....
}
可以这样调用:
AddProperty(typeBuilder,"MyPropertyName",typeof(string));
但是现在我想向类添加一个属性:
public IEnumerable<AnotherDynamicType> MyList {get;set;}
假设目标 AnotherDynamicType
,如何定义该调用属性的“类型”也是动态创建的吗?
以下内容无法编译:
AddProperty(typeBuilder,"MyPropertyName",typeof(IEnumerable<typeof(anotherDynamicType)>));
最佳答案
您可以使用Type.MakeGenericType
:
var anotherTypeEnumerable = typeof(IEnumerable<>)
.MakeGenericType(anotherDynamicType);
关于c# - Reflection.Emit 中的 IEnumerable 属性创建,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31458669/