我使用反射检查 C# 方法的参数。该方法有一些输出参数,对于这些参数,我得到了 IsByRef=true 的返回类型。例如,如果参数声明为“out string xxx”,则参数的类型为 System.String&。有没有办法将 System.String& 转换回 System.String?该解决方案当然不仅适用于 System.String,而且适用于任何类型。
最佳答案
演示:
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);
}
}
关于c# - 将 c# 按引用类型转换为匹配的非按引用类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1454363/