开发者

How can I handle generic method invocations in my DynamicObject?

I'm trying to construct a DynamicObject that is able to handle generic method invocations, but it seems that 开发者_如何学Cthe needed API - although present in RC versions of 4.0 Framework - has been marked internal in RTM (namely, CSharpInvokeMemberBinder is now internal). Is there an equivalent for this code that would work in 4.0 RTM?

public class TransparentObject<T> : DynamicObject {
    private readonly T target;

    public TransparentObject(T target) {
        this.target = target;
    }

    public override bool TryInvokeMember(
      InvokeMemberBinder binder, object[] args, out object result) {
        var csBinder = binder as CSharpInvokeMemberBinder;
        var method = typeof(T).GetMethod(binder.Name, BindingFlags.Public
          | BindingFlags.NonPublic | BindingFlags.Instance);
        if (method == null)
            throw new MissingMemberException(string.Format(
              "Method '{0}' not found for type '{1}'", binder.Name, typeof(T)));
        if (csBinder.TypeArguments.Count > 0)
            method = method.MakeGenericMethod(csBinder.TypeArguments.ToArray());
        result = method.Invoke(target, args);
        return true;
    }
}

(Code taken from http://bugsquash.blogspot.com/2009/05/testing-private-methods-with-c-40.html )

I am aware that I can use reflection to get generic type parameters here, but I'm looking for a nicer solution - if there is one.


The fastest equivalent I can guess:

    private static readonly Func<InvokeMemberBinder, IList<Type>> GetTypeArguments;

    static TransparentObject()
    {
        var type = typeof(RuntimeBinderException).Assembly.GetTypes().Where(x => x.FullName == "Microsoft.CSharp.RuntimeBinder.CSharpInvokeMemberBinder").Single();
        var dynamicMethod = new DynamicMethod("@", typeof(IList<Type>), new[] { typeof(InvokeMemberBinder) }, true);
        var il = dynamicMethod.GetILGenerator();
        il.Emit(OpCodes.Ldarg_0);
        il.Emit(OpCodes.Castclass, type);
        il.Emit(OpCodes.Call, type.GetProperty("Microsoft.CSharp.RuntimeBinder.ICSharpInvokeOrInvokeMemberBinder.TypeArguments", BindingFlags.Instance | BindingFlags.NonPublic).GetGetMethod(true));
        il.Emit(OpCodes.Ret);
        GetTypeArguments = (Func<InvokeMemberBinder, IList<Type>>)dynamicMethod.CreateDelegate(typeof(Func<InvokeMemberBinder, IList<Type>>));
    }

    public override bool TryInvokeMember(InvokeMemberBinder binder, object[] args, out object result)
    {
        var method = typeof(T).GetMethod(binder.Name, BindingFlags.Public| BindingFlags.NonPublic | BindingFlags.Instance);
        if (method == null) throw new MissingMemberException(string.Format("Method '{0}' not found for type '{1}'", binder.Name, typeof(T)));
        var typeArguments = GetTypeArguments(binder);
        if (typeArguments.Count > 0) method = method.MakeGenericMethod(typeArguments.ToArray());
        result = method.Invoke(target, args);
        return true;
    }
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜