You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When invoked on a method of a generic value type to create an open delegate, MethodInfo.CreateDelegate() creates a working delegate (i.e. the delegate can be invoked given suitable arguments) but the delegate's Method property throws an exception complaining about incompatible type and method handles. This behavior does not occur if the value type is not generic or if the generic type is a reference type and normal Action<> is used in place of RefAction<>. The code snippet below illustrates the problem.
Reproduction Steps
void Main()
{
var d2 = CreateMyDelegate (typeof (D)) ; // ok
var x2 = new D () ;
((RefAction<D>) d2).Invoke (ref x2) ; // ok
d2.Method.ToString() ; // ok
var d3 = CreateMyDelegate (typeof (E<int>)) ; // ok
((Action<E<int>>) d3).Invoke (new E<int> ()) ; // ok
d3.Method.ToString() ; // ok
var d1 = CreateMyDelegate (typeof (C<int>)) ; // ok
var x1 = new C<int> () ;
((RefAction<C<int>>) d1).Invoke (ref x1) ; // ok
d1.Method.ToString() ; // .Method throws
}
static Delegate CreateMyDelegate (Type t)
{
var m = t.GetMethod ("Foo") ;
return m.CreateDelegate ((t.IsValueType ? typeof (RefAction<>) : typeof (Action<>)).MakeGenericType (t)) ;
}
delegate void RefAction<T> (ref T t) where T : struct ;
struct C<T> { public void Foo () {} }
struct D { public void Foo () {} }
class E<T> { public void Foo () {} }
Expected behavior
d1.Method returns the method used to create the open delegate.
Actual behavior
d1.Method throws an exception despite the open delegate being created successfully.
Regression?
No response
Known Workarounds
I have to pass the MethodInfo used to create the delegate around with the delegate since I can't rely on the delegate's Method property.
Description
When invoked on a method of a generic value type to create an open delegate,
MethodInfo.CreateDelegate()
creates a working delegate (i.e. the delegate can be invoked given suitable arguments) but the delegate'sMethod
property throws an exception complaining about incompatible type and method handles. This behavior does not occur if the value type is not generic or if the generic type is a reference type and normalAction<>
is used in place ofRefAction<>
. The code snippet below illustrates the problem.Reproduction Steps
Expected behavior
d1.Method
returns the method used to create the open delegate.Actual behavior
d1.Method
throws an exception despite the open delegate being created successfully.Regression?
No response
Known Workarounds
I have to pass the
MethodInfo
used to create the delegate around with the delegate since I can't rely on the delegate'sMethod
property.Configuration
Runtime: Microsoft.NETCore.App 8.0.6 [windows] (probably irrelevant)
Other information
No response
The text was updated successfully, but these errors were encountered: