jeudi 3 août 2017

С# reflection check that FieldType is some list

Is it possible to specify that test is List<> without specifing in condition type of List<int>?

namespace ConsoleApplication9
{
    class Program
    {
        static void Main(string[] args)
        {
            SuperClass1 class1 = new SuperClass1();
            PrintAllFields(class1);
        }

        public static void PrintAllFields(object obj)
        {
            var SuperClassType = obj.GetType();
            foreach (var item in SuperClassType.GetFields())
            {
                if (item.FieldType == typeof(List<>))
                {
                    Console.WriteLine("it's List");
                }
                else if (item.FieldType == typeof(Int32))
                {
                    Console.WriteLine("it's int32");
                }
                else if (item.FieldType == typeof(Byte))
                {
                    Console.WriteLine("it's byte");
                }
            }
        }
    }

    public class SuperClass1
    {
        public int param1;
        public int param2;
        public int param3;
        public byte param4;
        public List<int> test;
        public SuperClass1()
        {
            test = new List<int>();
        }

    }
}





Aucun commentaire:

Enregistrer un commentaire