开发者

Invoke a IDynamicMetaObjectProvider's members without a reference to Microsoft.CSharp.dll

I have a a dynamic value (implementation of IDynamicMetaObjectProvider) that I'd like to call methods and properties on.

Examples I have found so far of invoking members on a dynamic value use typ开发者_高级运维es out of Microsoft.CSharp.dll, e.g.

IDynamicMetaObjectProvider x = GetDynamicValue();
CallSite<Func<CallSite, object, object, object>> site = CallSite<Func<CallSite, object, object, object>>.Create(
            Binder.SetMember(
                Microsoft.CSharp.RuntimeBinder.CSharpBinderFlags.None,
                "Foo",
                null,
                new[] { CSharpArgumentInfo.Create(CSharpArgumentInfoFlags.None, null) }
            )
        );
site.Target(site, x, 42);

I want to be able to invoke a IDynamicMetaObjectProvider's members without using Microsoft.CSharp.dll. Note that I am not talking about using the C# dynamic keyword on anything related to C# but using a IDynamicMetaObjectProvider directly.

Also note that using Reflection won't work. Reflection bypasses dynamic call binding and simply performs Reflection on the underlying type. I need a technique that works with any implementation of IDynamicMetaObjectProvider.


You would have to implement your own CallSiteBinder as there are no language agnostic CallSiteBinders. Basically the idea is that the Binder handles the subtle differences between info provided from your calling languages to pick the correct member to bind to on any object regardless of language.

Since it's not like a CSharp Binder can't bind to an IronPython object, the flexibilty on using different binders, as per library consumers language, depends on how much flexibility you are providing via your library. Given the above example if you weren't allowing the the library consumers to change any binder parameters then you would be fine using the CSharp Binder if you did offer more maybe it would be useful to offer switching out the binder via dependency injection or overloaded method.

However, I'm thinking that CSharpBinder should be fine if you want to pass just some of the binder parameters because the csharp binder gives you pretty good flexibility binding wise (with the one exception of not being about to change bindings to be case insensitive), and it's really the best choice for binders in a generic library, since it's the only language binder included standard in the .net 4.0 installation and Mono 2.8 installation.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜