开发者

Why does dynamic fail to invoke methods on my dynamicly built object?

开发者 https://www.devze.com 2023-01-25 17:13 出处:网络
I have a type that I build with TypeBuilder to match an 开发者_Go百科interface, like this:: interface IFoo

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;}}
}
0

精彩评论

暂无评论...
验证码 换一张
取 消