有没有办法在运行时创建一个接口?我试过了:
string typeSignature = "IFoo";
...
TypeBuilder tb = moduleBuilder.DefineType(typeSignature, TypeAttributes.Interface);但这给出了一个例外:
‘接口必须声明为抽象。’
所以我试着:
TypeBuilder tb = moduleBuilder.DefineType(typeSignature, TypeAttributes.Interface
| TypeAttributes.Abstract);
Type dynamicType = tb.CreateType();但是dynamicType.GetType().IsInterface返回false。出于某种原因,它认为dynamicType是一个类。
发布于 2019-06-07 06:22:12
正如Ralf在评论中所建议的,您可以尝试,
TypeBuilder tb = moduleBuilder.DefineType(
typeSignature,
TypeAttributes.Interface | TypeAttributes.Abstract | TypeAttributes.Public);
Type dynamicType = tb.CreateType();
dynamicType.IsInterface // truehttps://stackoverflow.com/questions/56485064
复制相似问题