Csharp/C#教程:为Type.GetMethod指定params分享


为Type.GetMethod指定params

我正在使用reflection来获取TryParse方法信息(为第一个人提供upvote以猜测原因;)。

如果我打电话:

typeof(Int32).GetMethod("Parse", BindingFlags.Static | BindingFlags.Public, null, new Type[] { typeof(string) }, null); 

我得到一个方法,但稍微扩展一下:

 typeof(Int32).GetMethod("TryParse", BindingFlags.Static | BindingFlags.Public, null, new Type[] { typeof(string), typeof(Int32) }, null); 

我一无所获。 我的spidersense告诉我这是因为第二个参数是out参数。

谁知道我在这里做错了什么?

试试这个

 typeof(Int32).GetMethod("TryParse", BindingFlags.Static | BindingFlags.Public, null, new Type[] { typeof(string), typeof(Int32).MakeByRefType() }, null); 

就像@ Jab一样,但是更短一点:

上述就是C#学习教程:为Type.GetMethod指定params分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注—计算机技术网(www.ctvol.com)!

 var tryParseMethod = typeof(int).GetMethod(nameof(int.TryParse), new[] { typeof(string), typeof(int).MakeByRefType() }); // use it var parameters = new object[] { "1", null }; var success = (bool)tryParseMethod.Invoke(null, parameters); var result = (int)parameters[1]; 

本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

ctvol管理联系方式QQ:251552304

本文章地址:https://www.ctvol.com/cdevelopment/953457.html

(0)
上一篇 2021年11月19日
下一篇 2021年11月19日

精彩推荐