Here is the original question where I'm looking for a way to generate a generic delegate: .Net generate generic methods
Here is the code to generate a generic delegate in .NET 3.5:
public delegate void PropertyChangedDelegate<OwnerType, PropertyType>(OwnerType sender, String propertyName, PropertyType oldValue, PropertyType newValue);
EventInfo eventInfo = type.GetEvent(property.Name + "Changed");
MethodInfo propertyChangedMethodInfo = this.GetType().GetMethod("content_PropertyChanged", BindingFlags.NonPublic | BindingFlags.Instance);
MethodInfo genericPropertyChangedMethodInfo = propertyChangedMethodInfo.MakeGenericMethod(eventInfo.EventHandlerType.GetGenericArguments());
Delegate delegate_ = Delegate.CreateDelegate(eventInfo.EventHandlerType, genericPropertyChangedMethodInfo);
eventInfo.AddEventHandler(obj, delegate_);
void content_PropertyChanged<OwnerType, PropertyType>(OwnerType sender, String propertyName, PropertyType oldValue, PropertyType newValue)
{
}
This works in .NET 3.5, but now when I tried to port to the compact framework 3.5, the Delegate.CreateDelegate method requires a third parameter... The parameter description says:
it should be the first argument, or 'the object to which the delegate is bound'.
I tried putting 'obj' in there, and 'this', and null, and I always get an invalid argument exception.
Any ideas?
Unfortunately .NETCF (.NET Compact Framework) doesn't support the same method signatures as .NET (full framework) as only a subset is implemented.
You can see this on the MSDN library where only one of the ten method overloads is "Supported by the .NET Compact Framework", indicated with the small graphic of a PDA/mobile.
This means you have to use Delegate.CreateDelegate(Type, Object, MethodInfo) in .NETCF.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With