0

を持つ構成オブジェクトがあり、一連の を同じプロパティenumにバインドしたいと考えていました。RadioButton時々それを繰り返しているように見えるので、複数のアイテムを同じプロパティにバインドするための一般的な解決策を考え出そうとしました。私が見つけた解決策は、式を実行するプロキシを作成することでした。以下のコードを参照してください。

public enum GenderEnum
{
    Male,
    Female
}

public class DataClass
{
    public GenderEnum Gender { get; set; }
}

class ExpressionBinder<T>
{
    public Func<T> Getter;
    public Action<T> Setter;

    public T Value
    {
        get { return Getter.Invoke(); }
        set { Setter.Invoke(value); }
    }
    public ExpressionBinder(Func<T> getter, Action<T> setter)
    {
        Getter = getter;
        Setter = setter;
    }
}

このように使用できます:

radioMale.Tag = GenderEnum.Male;
radioFemale.Tag = GenderEnum.Female;

var proxyM = new ExpressionBinder<bool>(
    () => DataObj.Gender == (GenderEnum)radioMale.Tag,
    (val) => { if (val) DataObj.Gender = (GenderEnum)radioMale.Tag; });

var proxyF = new ExpressionBinder<bool>(
    () => DataObj.Gender == (GenderEnum)radioFemale.Tag,
    (val) => { if (val) DataObj.Gender = (GenderEnum)radioFemale.Tag; });

radioMale.DataBindings.Add("Checked", proxyM, "Value");
radioFemale.DataBindings.Add("Checked", proxyF, "Value");

これは単なる例です。簡単にしたかったのです。しかし、このアプローチは実際にはWORKSです。質問は次のとおりです。

比較をカプセル化し、派生クラス内に式を設定したいと考えています。

class ComparisonBinder : ExpressionBinder {}

残念ながら、私はそれをどのように使用したいかしか言えません。

radioMale.DataBindings.Add("Checked", 
    new ComparisonBinder<ConfigClass>((c) c.Gender, GenderEnum.Male), 
    "Value");
radioFemale.DataBindings.Add("Checked", 
    new ComparisonBinder<ConfigClass>((c) c.Gender, GenderEnum.Female),
    "Value");

では、これをどのように実装しますか?必要に応じて、私の ExpressionBinder クラスを自由に変更してください。


最終的なコード

(再度編集: オブジェクト型を使用して TValue パラメーターを省略することはできません。ラムダ式の変換中に、コンパイラーは への呼び出しを追加しますConvert()。)

class ComparisonBinder<TSource, TValue> : ExpressionBinder<bool>
{
    private TSource instance;
    private TValue comparisonValue;
    private PropertyInfo pInfo;

    public ComparisonBinder(TSource instance, Expression<Func<TSource, TValue>> property, TValue comparisonValue)
    {
        pInfo = GetPropertyInfo(property);

        this.instance = instance;
        this.comparisonValue = comparisonValue;

        Getter = GetValue;
        Setter = SetValue;
    }

    private bool GetValue()
    {
        return comparisonValue.Equals(pInfo.GetValue(instance, null));
    }

    private void SetValue(bool value)
    {
        if (value)
        {
            pInfo.SetValue(instance, comparisonValue,null);
        }
    }

    /// <summary>
    /// Adapted from surfen's answer (http://stackoverflow.com/a/10003320/219838)
    /// </summary>
    private PropertyInfo GetPropertyInfo(Expression<Func<TSource, TValue>> propertyLambda)
    {
        Type type = typeof(TSource);

        MemberExpression member = propertyLambda.Body as MemberExpression;

        if (member == null)
            throw new ArgumentException(string.Format(
                "Expression '{0}' refers to a method, not a property.",
                propertyLambda));

        PropertyInfo propInfo = member.Member as PropertyInfo;
        if (propInfo == null)
            throw new ArgumentException(string.Format(
                "Expression '{0}' refers to a field, not a property.",
                propertyLambda));

        if (type != propInfo.ReflectedType &&
            !type.IsSubclassOf(propInfo.ReflectedType))
            throw new ArgumentException(string.Format(
                "Expresion '{0}' refers to a property that is not from type {1}.",
                propertyLambda,
                type));

        return propInfo;
    }
}

使用法:

radioMale.DataBindings.Add("Checked", 
    new ComparisonBinder<DataClass,GenderEnum>(DataObj, (x) => x.Gender, GenderEnum.Male), "Value");
radioFemale.DataBindings.Add("Checked", 
    new ComparisonBinder<DataClass,GenderEnum>(DataObj, (x) => x.Gender, GenderEnum.Female), "Value");
4

1 に答える 1

2

PropertyInfoこれは、いくつかのラムダマジックを使用して実行できると思います:

class ComparisonBinder<TSource,TValue> : ExpressionBinder<bool>
{
    public ComparisonBinder(TSource source, Expression<Func<TSource, bool>> propertyLambda, TValue comparisonValue) :base(null,null)
    {
         var propertyInfo = GetPropertyInfo<TSource,bool>(source, propertyLambda);
         this.Getter = () => comarisonValue.Equals((TValue)propertyInfo.GetValue(source));
         this.Setter = (bool value) => { if(value) propertyInfo.SetValue(source, comparisonValue); };
    }
}

使用法:

radioMale.DataBindings.Add("Checked", 
    new ComparisonBinder<ConfigClass, GenderEnum>(config, c => c.Gender, GenderEnum.Male), 
    "Value");
radioFemale.DataBindings.Add("Checked", 
    new ComparisonBinder<ConfigClass, GenderEnum>(config, c => c.Gender, GenderEnum.Female),
    "Value");

テストはしていませんが、エラーを修正してこのソリューションを使用できることを願っています。

GetPropertyInfo(): https://stackoverflow.com/a/672212/724944からコピー

public PropertyInfo GetPropertyInfo<TSource, TProperty>(
    TSource source,
    Expression<Func<TSource, TProperty>> propertyLambda)
{
    Type type = typeof(TSource);

    MemberExpression member = propertyLambda.Body as MemberExpression;
    if (member == null)
        throw new ArgumentException(string.Format(
            "Expression '{0}' refers to a method, not a property.",
            propertyLambda.ToString()));

    PropertyInfo propInfo = member.Member as PropertyInfo;
    if (propInfo == null)
        throw new ArgumentException(string.Format(
            "Expression '{0}' refers to a field, not a property.",
            propertyLambda.ToString()));

    if (type != propInfo.ReflectedType &&
        !type.IsSubclassOf(propInfo.ReflectedType))
        throw new ArgumentException(string.Format(
            "Expresion '{0}' refers to a property that is not from type {1}.",
            propertyLambda.ToString(),
            type));

    return propInfo;
}
于 2012-04-04T00:23:04.007 に答える