Starry

プログラミングやクラウドについて

C# リフレクション

C#のリフレクションを最近触ったので少し復習がてら記事にしてみました。
まずはクラスを用意。

class Fruits
    {
        public int Apple { get; set; }
        public string Banana { get; set; }
        public int? Cherry { get; set; }
        public int Drian { get; } = 3;

        public int Sum()
        {
            return Apple + int.Parse(Banana) + (int)Cherry + Drian;
        }
    }


GetPropertiesでプロパティの情報を取り出せる。例えば.Nameでフィールド名を取り出せる。

class Program
    {
        static void Main(string[] args)
        {
            var fruit = new Fruits();
            foreach (var prop in fruit.GetType().GetProperties())
            {
                Console.WriteLine(prop.Name);
            }
        }
    }

結果

Apple
Banana
Cherry
Drian


.canWriteでsetterを持っているか判断する。

class Program
    {
        static void Main(string[] args)
        {
            var fruit = new Fruits();
            foreach (var prop in fruit.GetType().GetProperties())
            {
                Console.WriteLine(prop.Name + ":" + prop.CanWrite);
            }
        }
    }

結果

Apple:True
Banana:True
Cherry:True
Drian:False


.PropertyNameでフィールドの型名が取り出せる。

class Program
    {
        static void Main(string[] args)
        {
            var fruit = new Fruits();
            foreach (var prop in fruit.GetType().GetProperties())
            {
                Console.WriteLine(prop.Name + ":" + prop.PropertyType);
            }
        }
    }

結果

Apple:System.Int32
Banana:System.String
Cherry:System.Nullable`1[System.Int32]
Drian:System.Int32


.SetValueメソッドでは値を入れることができる。
一つ目の引数は入れ物のオブジェクト。
二つ目の引数は入れ物に入れる値。

class Program
    {
        static void Main(string[] args)
        {
            var fruit = new Fruits();
            var instance = new Fruits();
            foreach (var prop in fruit.GetType().GetProperties())
            {
                if (!prop.CanWrite) continue;
                if(prop.PropertyType == typeof(int))
                {
                    prop.SetValue(instance, 1);
                }
                else if(prop.PropertyType == typeof(int?))
                {
                    prop.SetValue(instance, 2);
                }
                else if(prop.PropertyType == typeof(string))
                {
                    prop.SetValue(instance, "3");
                }
            }

            Console.WriteLine("Apple = " + instance.Apple);
            Console.WriteLine("Banana = " + instance.Banana);
            Console.WriteLine("Cherry = " + instance.Cherry);
        }

結果

Apple = 1
Banana = 3
Cherry = 2


.GetValueで引数に入っている値を取り出すことができる。

class Program
    {
        static void Main(string[] args)
        {
            var fruit = new Fruits();
            var instance = new Fruits();
            foreach (var prop in fruit.GetType().GetProperties())
            {
                if (!prop.CanWrite) continue;
                if (prop.PropertyType == typeof(int))
                {
                    prop.SetValue(instance, 1);
                    Console.WriteLine(prop.GetValue(instance));
                }
                else if (prop.PropertyType == typeof(int?))
                {
                    prop.SetValue(instance, 2);
                    Console.WriteLine(prop.GetValue(instance));
                }
                else if (prop.PropertyType == typeof(string))
                {
                    prop.SetValue(instance, "3");
                    Console.WriteLine(prop.GetValue(instance));
                }
            }
        }
    }

結果

1
3
2


他にも色々あるけど割とこの上記のものをよく使った。