I have a type that I build with TypeBuilder
to match an 开发者_Go百科interface, like this::
interface IFoo
{
int Property{get;}
}
My code builds the get_Property method. If I cast my built object as an IFoo, everything works. However, when I use the object as a dynamic the the code complains that my IFoo does not implement get_Property. Why is this happening, can I not use the dynamic functionality with types built at runtime?
dynamic
uses the public API. If you are using TypeBuilder
it is possible that you have just provided a method and marked it as implementing that method, in which case it is (essentially) an explicit interface implementation, and undiscoverable by dynamic
. To use dynamic
it would probably need PropertyBuilder
and a public property, with the property implementation method also marked as the interface implementation.
For comparison, dynamic
would also fail with:
class Foo : IFoo {
int IFoo.Property { get {return 5;}}
}
精彩评论