Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MethodInfo.CreateDelegate() creates working delegate but delegate.Method throws #110357

Closed
atykhyy opened this issue Dec 3, 2024 · 2 comments
Closed

Comments

@atykhyy
Copy link

atykhyy commented Dec 3, 2024

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'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.

Configuration

Runtime: Microsoft.NETCore.App 8.0.6 [windows] (probably irrelevant)

Other information

No response

@dotnet-policy-service dotnet-policy-service bot added the untriaged New issue has not been triaged by the area owner label Dec 3, 2024
Copy link
Contributor

Tagging subscribers to this area: @dotnet/area-system-reflection
See info in area-owners.md if you want to be subscribed.

@steveharter
Copy link
Member

This looks like a duplicate of #108619.

@steveharter steveharter closed this as not planned Won't fix, can't repro, duplicate, stale Dec 4, 2024
@dotnet-policy-service dotnet-policy-service bot removed the untriaged New issue has not been triaged by the area owner label Dec 4, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants