Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.NET generate generic method - compact framework

Tags:

c#

.net

generics

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?

like image 621
Ryan Brown Avatar asked May 17 '13 16:05

Ryan Brown


1 Answers

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.

CreateDelegate method

This means you have to use Delegate.CreateDelegate(Type, Object, MethodInfo) in .NETCF.

like image 127
Kevin Hogg Avatar answered Oct 03 '22 23:10

Kevin Hogg