本文介绍了确定的DynamicObject成员访问预期的类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有可能确定一个动态成员访问期待什么类型?我试过

Is it possible to determine what type a dynamic member access expects? I've tried

dynamic foo = new MyDynamicObject();
int x = foo.IntValue;
int y = (int)foo.IntValue;



而在 TryGetMember 拦截 GetMemberBinder.ReturnType 是反对两种方式。我还实施 TryConvert 想知道是否有可能会被调用来进行转换,但它从来没有被击中。

And in the TryGetMember intercept GetMemberBinder.ReturnType is object either way. I also implemented TryConvert wondering if it might get invoked to do the conversion, but it never is hit.

时有一些其他覆盖我的思念,让我确定是什么类型的调用者想要这样我可以做适当的转换?

Is there some other override I'm missing that lets me determine what Type the caller wants so that I can do the appropriate conversion?

推荐答案

在C#中使用动态时,编译器始终将粘合剂返回对象的类型,然后做了第二个动态的隐式转换的预期收益类型。因此,在从C#调用时DynamicObject,GetMemberBinder.ReturnType永远是对象,但如果返回另一个排序跳板动态对象与TryConvert实现你可以得到这种类型的,即说除非用户执行 VAR或动态作为变量,那么他们有一个代理,直到它变成静态类型,不会做任何事情。

In C#, when using dynamic, the compiler always sets the binder to return type of object, and then does a second dynamic implicit conversion to the expected return type. So on a DynamicObject when called from c#, GetMemberBinder.ReturnType will always be object, but that said if you return another sort of springboard dynamic object with TryConvert implemented you could get that type, except if the user does var or dynamic as the variable, then they have a proxy that won't do anything until it becomes statically typed.

的做不同的事情,但沿着这些线路,因为它也必须有一个强有力的执行基于返回类型改变的愿望 - 只是你必须通过一个接口来描述动态对象。

ImpromptuInterface does something different but along these lines, because it also has the desire to have a dynamic implementation that changes based on return types -- just you would have to describe the dynamic object via an interface.

这篇关于确定的DynamicObject成员访问预期的类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-02 12:24