jeudi 6 juin 2019

C# Reflection Create Interface at Runtime

Is there a way to create an Interface at runtime? I tried:

string typeSignature = "IFoo";
...
TypeBuilder tb = moduleBuilder.DefineType(typeSignature, TypeAttributes.Interface);

But that gives an exception:

'Interface must be declared abstract.'

So I tried:

TypeBuilder tb = moduleBuilder.DefineType(typeSignature, TypeAttributes.Interface 
    | TypeAttributes.Abstract);
Type dynamicType = tb.CreateType();

But dynamicType.GetType().IsInterface returns false. For some reason it thinks dynamicType is a class.





Aucun commentaire:

Enregistrer un commentaire