Reputation: 7301
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
Upvotes: 2
Views: 2130
Reputation: 1063005
In .NET 3.5 you can use Expression.GetActionType
:
Type setterType = Expression.GetActionType(props[0].PropertyType);
Delegate delegate1 = Delegate.CreateDelegate(setterType,
new testObject(), props[0].GetSetMethod()
);
Or if you want an open delegate (i.e. to any testObject
instance, not bound to the new one):
Type setterType = Expression.GetActionType(
props[0].DeclaringType, props[0].PropertyType);
Delegate delegate1 = Delegate.CreateDelegate(setterType,
null, props[0].GetSetMethod()
);
However; note that you'd have to use these delegates via DynamicInvoke
, which is much slower than using a fully typed delegate via Invoke
.
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 compiled Expression
, or custom IL.
Upvotes: 3