mercredi 8 avril 2020

In C#, Relfection incorrectly determines IsGenericType value for ByRef method argument

Could someone please explain why I see what I see (the screenshot of the console below)? For 'ref' and non-'ref' method argument I got different generic-related properties from Reflection.

public class MyClass { }

    public class Aref
    {
        public virtual void method(object je, ref List<MyClass> batchlist, object doc)
        {
        }
    }


    public class A
    {
        public virtual void method(object je, List<MyClass> batchlist, object doc)
        {
        }
    }

    private static void WriteGenericProps(Type type)
    {
        var method = type.GetMethod(nameof(Aref.method));
        var param = method.GetParameters().First(p => p.Name == "batchlist");

        Console.WriteLine(nameof(param.ParameterType.IsByRef) + "=" + param.ParameterType.IsByRef);
        Console.WriteLine(nameof(param.ParameterType.IsGenericType) + "=" + param.ParameterType.IsGenericType);
        Console.WriteLine(nameof(param.ParameterType.ContainsGenericParameters) + "=" + param.ParameterType.ContainsGenericParameters);
        Console.WriteLine(nameof(param.ParameterType.IsConstructedGenericType) + "=" + param.ParameterType.IsConstructedGenericType);
        Console.WriteLine(nameof(param.ParameterType.IsGenericParameter) + "=" + param.ParameterType.IsGenericParameter);
        Console.WriteLine(nameof(param.ParameterType.IsGenericTypeDefinition) + "=" + param.ParameterType.IsGenericTypeDefinition);
    }

    static void Main(string[] args)
    {
        var t = typeof(Aref);
        Console.WriteLine(t.Name);
        WriteGenericProps(t);
        Console.WriteLine();

        t = typeof(A);
        Console.WriteLine(t.Name);
        WriteGenericProps(t);
    }

enter image description here





Aucun commentaire:

Enregistrer un commentaire