LoginSignup
40

More than 5 years have passed since last update.

【C#】プロパティ名でプロパティにアクセスする

Last updated at Posted at 2016-11-19

はじめに

 外部ファイルのプロパティ名と値を用いてインスタンスを設定する際に、プロパティ名からプロパティにアクセスできたら便利だと思いいい方法がないか考えました。
 C#6.0で書いています。

任意のプロパティにアクセス

 System.Reflection.PropertyInfoクラスのメソッドを用いることで、プロパティ名から値の取得と設定が行えました。

using System;

namespace QiitaConsole
{
    class Program
    {
        static void Main(string[] args)
        {
            var student = new Student() { ID = "0001", Name = "aiueo" };

            // プロパティ情報の取得
            var property = typeof(Student).GetProperty("Name");

            // インスタンスの値を取得
            var beforeName = property.GetValue(student);

            // インスタンスに値を設定
            property.SetValue(student, "newName");

            Console.WriteLine($"{student.ID}, {beforeName}, {student.Name}");
            // 0001, aiueo, newName
        }

        class Student
        {
            public string ID { set; get; }

            public string Name { set; get; }
        }
    }
}

インデクサを用いる場合

 これをインデクサを用いてアクセスできるようにするには以下のように書きます

using System;

namespace QiitaConsole
{
    class Program
    {
        static void Main(string[] args)
        {
            var student = new Student() { ID = "0001", Name = "aiueo" };

            var beforeName = student["Name"];

            student["Name"] = "newName";

            Console.WriteLine($"{student.ID}, {beforeName}, {student.Name}");
            // 0001, aiueo, newName
        }

        class Student
        {
            public string ID { set; get; }

            public string Name { set; get; }

            public object this[string propertyName]
            {
                get
                {
                    return typeof(Student).GetProperty(propertyName).GetValue(this);
                }

                set
                {
                    typeof(Student).GetProperty(propertyName).SetValue(this, value);
                }
            }
        }
    }
}

注意点

 リフレクションを使っているためプライベート変数までアクセスできてしまいます。使用する際は、ロジックが書かれてあるクラスを対象とせず、値の保持のみを目的とした単純なクラスのみで利用するようにしましょう。間違って他のメンバにアクセスされると危険です。

using System;

namespace QiitaConsole
{
    class Program
    {
        static void Main(string[] args)
        {
            var student = new Student();

            var property = typeof(Student).GetProperty("Name");

            var beforeName = property.GetValue(student);

            property.SetValue(student, "newName");

            Console.WriteLine($"{student.ID}, {beforeName}, {student.Name}");
            // 0001, aiueo, newName
        }

        class Student
        {
            public Student()
            {
                ID = "0001";
                Name = "aiueo";
            }

            public string ID { set; get; }

            public string Name { private set; get; }
        }
    }
}

まとめ

 プロパティ名でプロパティにアクセスすることができました。

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
40