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

标签 c# reflection

我使用反射检查 C# 方法的参数。该方法有一些输出参数,对于这些参数,我得到了 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);        
    }
}

关于c# - 将 c# 按引用类型转换为匹配的非按引用类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1454363/

相关文章:

java - 为什么将 Type[] 转换为 Class[] 会抛出 ClassCastException?

c# - 如何在没有 setter 的情况下设置属性值

c# - 如何使用 DDay.iCal 创建 Outlook "appointment"?

c# - 如何限制文本框仅接受特定字符

java - 实现 java.lang.reflect.Proxy 的注释背后的基本原理是什么?

java - 在 Groovy 中推断字符串值类型

c# - 使用反射从父类获取自己的属性名称

c# - 由于其保护级别,链接按钮单击事件无法访问

C# 从 PictureBox 中移除绘图

c# - 如何在 AvalonEdit 中启用虚拟空间?