How to call DynamicObject.TryGetMember directly?

后端 未结 2 738
星月不相逢
星月不相逢 2020-12-05 06:59

I\'m implementing a general purpose function to extract a value from an arbitrary provided dynamic object, but don\'t know how to call TryGetMember because it r

2条回答
  •  伪装坚强ぢ
    2020-12-05 07:42

    You don't call TryGetMember directly, what you need is to use the dynamic api's directly to get the same effect by using a csharp member binder and a call site.

    This is made even easier by open source framework Dynamitey (via nuget) as it has a static method that does this. It works for any IDynamicMetaObjectProvider not just DynamicObject and (it works for regular types faster than reflection too).

    return Dynamic.InvokeGet(Source, FieldName);
    

提交回复
热议问题