I'm trying to construct a DynamicObject
that is able to handle generic method invocations, but it seems that the 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.