2009-09-21 17 views
14

Examino los parámetros de un método C# utilizando la reflexión. El método tiene algunos parámetros de salida y para estos obtengo tipos devueltos, que tienen IsByRef = verdadero. Por ejemplo, si el parámetro se declara como "out string xxx", el parámetro tiene el tipo System.String &. ¿Hay alguna forma de convertir System.String & a System.String? La solución, por supuesto, no solo debería funcionar para System.String, sino para cualquier tipo.Convierta el tipo de referencia C# al tipo de referencia no por correspondencia

+2

mente si cambio el título de "referencia" a "por referencia"? –

+0

No, no hay problema para mí. – Achim

Respuesta

24

Use Type.GetElementType().

Demostración:

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);   
    } 
} 
Cuestiones relacionadas