Right now, I have to do this
private delegate void set(int obj); //declare the prototype
...
Delegate delegate1 = Delegate.CreateDelegate(typeof(set), new testObject(), props[0].GetSetMethod());
((set)delegate1)(1);
Is there a way to CreateDelegate without that prototype and call it with any parameter? GetSetMethod() returns a very specific MethodInfo that takes a specific type as an argument.
Thanks
In .NET 3.5 you can use
Expression.GetActionType:Or if you want an open delegate (i.e. to any
testObjectinstance, not bound to the new one):However; note that you’d have to use these delegates via
DynamicInvoke, which is much slower than using a fully typed delegate viaInvoke.Another option (for this scenario) is to bind to a delegate that takes
object, and use casting inside the delegate – via perhaps some fancy generics, a compiledExpression, or custom IL.