转换C#通过引用类型匹配的非按引用类型 [英] Convert c# by-reference type to the matching non-by-reference type

查看:86
本文介绍了转换C#通过引用类型匹配的非按引用类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我考察使用反射一个C#方法的参数。该方法具有一定的输出参数,并为这些我回去种,其中有IsByRef = TRUE。例如,如果该参数被声明为出字符串XXX的参数具有System.String类型和放大器;.有没有一种办法System.String和放大器转换;回System.String?该解决方案应为System.String但对于任何类型的课程不仅工作

I examine the parameters of a C# method using reflection. The method has some out parameters and for these I get back types, which have IsByRef=true. For example if the parameter is declared as "out string xxx", the parameter has type System.String&. Is there a way to convert System.String& back to System.String? The solution should of course not only work for System.String but for any type.

推荐答案

使用的 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#通过引用类型匹配的非按引用类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆