Create delegate of array get method fails

Georg

Trying to create a delegate to access an array, I get an ArgumentException saying that the method could not be bound.

Cannot bind to the target method because its signature or security transparency is not compatible with that of the delegate type.

The minimum amount of code to reproduce this is the following:

var method = typeof(string[,]).GetMethod("Get");
var func = Delegate.CreateDelegate(typeof(Func<int, int, string>), new string[4,5], method);

Although the Get method is invisible by default, the first line does actually work and finds the correct method. As the signature matches, I assume the exception has something to do with the fact that the Get method is security-transparent, whereas Func<,,> is probably not. How to determine whether a delegate type is security-transparent and how to resolve this issue?

Hans Passant

I think you found a CLR bug. There are two fundamentally different chunks of code that binds a delegate, BindToMethodInfo and BindToMethodName. They are quite painful to unravel due to the caching that occurs inside them, I'm guessing there's something wrong there and related to the fact that arrays are treated rather specially in .NET.

That this hasn't been noticed before is somewhat explainable, surely everybody that tried to do what you do is using the documented Array.GetValue() method. Like this:

   var method = typeof(string[,]).GetMethod("GetValue", 
                                     new Type[] { typeof(int), typeof(int) });
   var func = Delegate.CreateDelegate(typeof(Func<int, int, object>), 
                                     new string[4, 5], method);

Which works fine. You can post feedback at connect.microsoft.net to hear the experts' opinion, don't count on a quicky bug fix though.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

How to create new array from an array method?

From Dev

Delegate method not being called?

From Dev

Looking for subviewsDidChange delegate method

From Dev

Generic Method assigned to Delegate

From Dev

MFMailComposeViewController Delegate Method Doesn't Get Called

From Dev

Best HTTP Method for Get or Create

From Dev

UIPageViewController delegate method not called

From Dev

Create array in method and use it in another method

From Dev

Create method from byte array

From Dev

UITableView Delegate Method

From Dev

Enums in delegate method declaration

From Dev

No overload for method matches delegate?

From Dev

Custom delegate method in Swift

From Dev

Delegate Method is not called in Swift?

From Dev

How to create delegate of Func<a,b> when a's method has parameters

From Dev

willDisplayCell delegate method Swift

From Dev

RXSwift How to create wrapper for delegate method with return value

From Dev

Logging for each method in "delegate"

From Dev

What is this delegate method doing?

From Dev

Delegate method is not called (is nil)

From Dev

How to create generic delegate from a method within an object

From Dev

Generic Method assigned to Delegate

From Dev

AFNetworking fails to create a request on a GET request

From Dev

MFMailComposeViewController Delegate Method Doesn't Get Called

From Dev

C# Create delegate of method where parameter types are unknown in runtime

From Dev

Custom delegate method doesn't get called

From Dev

how delegate can get the method address?

From Dev

Delegate method does not get called - iOS

From Dev

Create zip error: Namespace method fails on IShellDispatch