Call an object method regardless of parameter type

I use DynamicObject to wrap internal objects and masks, but when I try to call certain methods on an internal object, they require typed parameters, however, I consider all parameters as an Object type so that the call does not execute.

code:

public override bool TryInvokeMember(InvokeMemberBinder binder, object[] args, out object result) { try { result = mInternalObject.GetType().InvokeMember(binder.Name, (BindingFlags.InvokeMethod | BindingFlags.Instance | BindingFlags.Public), null, mInternalObject, args); return true; } catch (Exception) { return base.TryInvokeMember(binder, args, out result); } } 

So basically, I am wondering how to make it ignore the paramater types and call the method with the object anyway, any sugestions?

+4
source share
2 answers

I suspect you want something line by line (psuedo code, very simplistic):

 var mem = internalObject.GetType().GetMember(binder.Name); if (mem.IsGenericDefinition) mem = mem.MakeGeneric(Array.Convert(args, x => x.GetType())); var result = mem.Invoke(null, internalObject, args); 
+3
source

Instead of reflecting, since you are using dynamic, you can use really late binding from the open source Impromptu Interface . This is faster than reflection, and will work with a large number of objects (for example, with other dynamic objects) and can cause generics, while at the same time allowing you to point too, making everything much easier.

0
source

All Articles