加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 创建数组get方法的委托失败

发布时间:2020-12-16 00:10:31 所属栏目:百科 来源:网络整理
导读:尝试创建一个访问数组的委托,我得到一个ArgumentException说该方法无法绑定. Cannot bind to the target method because its signature or security transparency is not compatible with that of the delegate type. 重现此代码的最小代码量如下: var meth
尝试创建一个访问数组的委托,我得到一个ArgumentException说该方法无法绑定.

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

重现此代码的最小代码量如下:

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

虽然默认情况下Get方法是不可见的,但第一行确实有效并找到了正确的方法.当签名匹配时,我认为该异常与Get方法是安全透明的事实有关,而Func&lt ;,>可能不是.如何确定委托类型是否安全透明以及如何解决此问题?

解决方法

我认为你发现了一个CLR错误.有两个根本不同的代码块绑定委托,BindToMethodInfo和BindToMethodName.由于它们内部发生的缓存,它们很难解开,我猜测那里有一些错误,并且与在.NET中特别处理数组这一事实有关.

以前没有注意到这一点在某种程度上是可以解释的,当然每个试图做你所做的事情的人都使用记录在案的Array.GetValue()方法.像这样:

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

哪个工作正常.您可以在connect.microsoft.net上发布反馈,以听取专家的意见,但不要指望快速修复错误.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读