Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/299.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何取消引用ref传递的参数的ParameterType_C#_Dereference_Parameterinfo - Fatal编程技术网

C# 如何取消引用ref传递的参数的ParameterType

C# 如何取消引用ref传递的参数的ParameterType,c#,dereference,parameterinfo,C#,Dereference,Parameterinfo,我有以下代码片段(这只是一个示例,用于指出我正在寻找解决方案的问题): 在参考参数的情况下,我陷入了困境。在输出中,我可以看到参数何时通过引用传递,但我看不到的是引用指向的参数的类型是否为基元类型的数组 我想了解参考文献所指类型的相关信息。为了做到这一点,我假设我需要以某种方式取消对参数的引用,但我不知道如何执行此操作(例如,在本例中,我希望看到参数“int a”是基本的(在取消引用之后) 所以问题是,如何取消引用?如果类型是byref,请在其上使用GetElementType()来获取底层的非

我有以下代码片段(这只是一个示例,用于指出我正在寻找解决方案的问题):

在参考参数的情况下,我陷入了困境。在输出中,我可以看到参数何时通过引用传递,但我看不到的是引用指向的参数的类型是否为基元类型的数组

我想了解参考文献所指类型的相关信息。为了做到这一点,我假设我需要以某种方式取消对参数的引用,但我不知道如何执行此操作(例如,在本例中,我希望看到参数“int a”是基本的(在取消引用之后)


所以问题是,如何取消引用?

如果类型是byref,请在其上使用
GetElementType()
来获取底层的非引用类型。

这正是我需要的。Thx
static void Main()
{
    MethodInfo method = typeof(Test).GetMethod("Refer");
    ParameterInfo[] parameters = method.GetParameters();
    foreach (ParameterInfo parameter in parameters)
    {
        Type paramType = parameter.ParameterType;
        Console.WriteLine("Type of {0} is {1}", parameter.Name, paramType.Name);

        Console.WriteLine("{0} is passed by ref   : {1}", parameter.Name, paramType.IsByRef ? "Yes" : "No");

        // extracting element type (works for arrays, too)
        if (paramType.IsByRef)
            paramType = paramType.GetElementType();

        // this will print Yes for a ref int
        Console.WriteLine("{0} is a primitive type: {1}", parameter.Name, paramType.IsPrimitive ? "Yes" : "No");

        // ...
    }
}
static void Main()
{
    MethodInfo method = typeof(Test).GetMethod("Refer");
    ParameterInfo[] parameters = method.GetParameters();
    foreach (ParameterInfo parameter in parameters)
    {
        Type paramType = parameter.ParameterType;
        Console.WriteLine("Type of {0} is {1}", parameter.Name, paramType.Name);

        Console.WriteLine("{0} is passed by ref   : {1}", parameter.Name, paramType.IsByRef ? "Yes" : "No");

        // extracting element type (works for arrays, too)
        if (paramType.IsByRef)
            paramType = paramType.GetElementType();

        // this will print Yes for a ref int
        Console.WriteLine("{0} is a primitive type: {1}", parameter.Name, paramType.IsPrimitive ? "Yes" : "No");

        // ...
    }
}