将c#by-reference类型转换为匹配的非引用类型

我使用reflection检查C#方法的参数。 该方法有一些out参数,对于这些参数我得到了类型,它们具有IsByRef = true。 例如,如果参数声明为“out string xxx”,则参数的类型为System.String&。 有没有办法将System.String转换回System.String? 解决方案当然不仅适用于System.String,也适用于任何类型。

使用Type.GetElementType()

演示:

 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); } }