将c#by-reference类型转换为匹配的非引用类型
我使用reflection检查C#方法的参数。 该方法有一些out参数,对于这些参数我得到了类型,它们具有IsByRef = true。 例如,如果参数声明为“out string xxx”,则参数的类型为System.String&。 有没有办法将System.String转换回System.String? 解决方案当然不仅适用于System.String,也适用于任何类型。
使用Type.GetElementType()
。
演示:
上述就是C#学习教程:将c#by-reference类型转换为匹配的非引用类型分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注—计算机技术网(www.ctvol.com)!
using System; using System.Reflection; class Test { public void Foo(ref string x) { } static void Main() { MethodInfo method = typeof(Test).GetMethod("Foo"); Type stringByRef = method.GetParameters()[0].ParameterType; Console.WriteLine(stringByRef); Type normalString = stringByRef.GetElementType(); Console.WriteLine(normalString); } }
本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。
ctvol管理联系方式QQ:251552304
本文章地址:https://www.ctvol.com/cdevelopment/958044.html