繁体   English   中英

将C#按引用类型转换为匹配的按引用类型

[英]Convert c# by-reference type to the matching non-by-reference type

我使用反射检查C#方法的参数。 该方法具有一些out参数,对于这些参数,我可以返回类型,它们具有IsByRef = true。 例如,如果参数声明为“输出字符串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);        
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM