65

Accountクラスのインスタンスがあります。各アカウント オブジェクトには、所有者、参照などがあります。

アカウント プロパティにアクセスする 1 つの方法は、次のようなアクセサーを使用することです。

account.Reference;

しかし、次のような動的文字列セレクターを使用してアクセスできるようにしたいと思います。

account["PropertyName"];

JavaScript と同じです。したがってaccount["Reference"]、値を返すものがありますが、その後に新しい値を割り当てることもできます。

account["Reference"] = "124ds4EE2s";

使えることに気づきました

DataBinder.Eval(account,"Reference") 

文字列に基づいてプロパティを取得しますが、これを使用してプロパティに値を割り当てることはできません。

どうすればそれができるかについて何か考えはありますか?

4

11 に答える 11

94

まず、これを使用しないでください。C# は厳密に型指定された言語であるため、その側面に伴う型の安全性とパフォーマンスの利点を活用してください。

プロパティの値を動的に取得および設定する正当な理由がある場合 (つまり、型やプロパティ名をコードで定義できない場合)、リフレクションを使用する必要があります。

最もインラインに見える方法は次のとおりです。

object value = typeof(YourType).GetProperty("PropertyName").GetValue(yourInstance);
...
typeof(YourType).GetProperty("PropertyName").SetValue(yourInstance, "value");

ただし、PropertyInfoオブジェクトをキャッシュして読みやすくすることができます。

System.Reflection.PropertyInfo prop = typeof(YourType).GetProperty("PropertyName");

object value = prop.GetValue(yourInstance);
...
prop.SetValue(yourInstance, "value");
于 2010-05-25T13:47:36.860 に答える
41

インデクサーとリフレクションを組み合わせてみてください...

public object this[string propertyName]
{
    get
    {
        PropertyInfo property = GetType().GetProperty(propertyName);
        return property.GetValue(this, null);
    }
    set
    {
        PropertyInfo property = GetType().GetProperty(propertyName);
        property.SetValue(this,value, null);
    }
}
于 2010-05-25T15:20:12.970 に答える
7

それらが独自のオブジェクトである場合は、フィールドにアクセスするためのインデクサーを提供できます。これはあまりお勧めしませんが、必要なものは許可されます。

public object this[string propertyName]
{
    get
    {
        if(propertyName == "Reference")
            return this.Reference;
        else
            return null;
    }
    set
    {
        if(propertyName == "Reference")
            this.Reference = value;
        else
            // do error case here            
    }
}

これを行うと、型の安全性が失われることに注意してください。

于 2010-05-25T13:56:41.027 に答える
3

おそらくプロパティを使用する必要があるという以前のポスターに同意します。リフレクションは、プロパティへの直接アクセスに比べて非常に遅くなります。

一方、ユーザー定義のプロパティのリストを維持する必要がある場合は、C# プロパティを使用できません。のふりをするDictionaryか、 のように動作するプロパティを公開する必要がありますDictionary。Account クラスでユーザー定義プロパティをサポートする方法の例を次に示します。

public class Account
{
    Dictionary<string, object> properties;
    public object this[string propertyName]
    {
        get
        {
            if (properties.ContainsKey[propertyName])
                return properties[propertyName];
            else
                return null;
        }
        set
        {
            properties[propertyName] = value;
        }
    }
}
于 2010-05-25T14:03:26.470 に答える
2

私は個人的に拡張メソッドを使用することを好むので、ここに私のコードがあります:

public static class ReflectionExtensions
{
    public static void SetPropertyValue(this object Target,
        string PropertyName,
        object NewValue)
    {
        if (Target == null) return; //or throw exception

        System.Reflection.PropertyInfo prop = Target.GetType().GetProperty(PropertyName);

        if (prop == null) return; //or throw exception

        object value = prop.GetValue(Target, null);

        prop.SetValue(Target, NewValue, null);
    }
}
于 2014-10-03T13:40:50.720 に答える
1

リフレクションを使用する必要があります。

PropertyInfo property = typeof(Account).GetProperty("Reference");

property.SetValue(myAccount, "...", null);

これは非常に遅くなることに注意してください。

于 2010-05-25T13:48:37.347 に答える