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?