简化通用扩展方法

时间:2015-10-02 05:13:36

标签: c# generics

我编写了一个通用的扩展方法,它基本上添加了一个绑定到窗体的ui元素(如窗体或按钮)。

它现在运行良好,在一些项目中尝试过,但它仍然是一个非常复杂的方法,有3个通用" T-arguments"。

public static string GetPropertyName<T1,T2>(Expression<Func<T1,T2>> propertyExpression)
{
    return ((MemberExpression)propertyExpression.Body).Member.Name;
}

GetPropertyName如下所示:

DataBindings.Add<Form, FrmOptionsPresenter, double>(
    m_frmOptionsPresenter,
    binder => binder.Opacity,
    dataSrc => dataSrc.Opacity);

这是我如何调用该方法的两个例子:

{{1}}

就像我上面所说的,电话有点看起来很重。

我现在想知道的是: 有什么建议我可以让这些方法更简单易读吗?

提前致谢!

1 个答案:

答案 0 :(得分:1)

看看我们的实施:

public static class BindableComponentExtensions
{
    public static void Bind<T>(this IBindableComponent component, T value, Expression<Func<object>> controlProperty, Expression<Func<T, object>> modelProperty)
        where T : INotifyPropertyChanged
    {
        component.DataBindings.Add(new Binding(
            StringExtensions.PropertyName(controlProperty),
            value,
            StringExtensions.PropertyName(modelProperty),
            true,
            DataSourceUpdateMode.OnPropertyChanged));
    }

    public static void Bind<TComponent, T>(this TComponent component, T value,
        Expression<Func<TComponent, object>> controlProperty, Expression<Func<T, object>> modelProperty)
        where TComponent : IBindableComponent
        where T : INotifyPropertyChanged
    {
        component.DataBindings.Add(new Binding(
            StringExtensions.PropertyName(controlProperty),
            value,
            StringExtensions.PropertyName(modelProperty),
            true,
            DataSourceUpdateMode.OnPropertyChanged));
    }

    public static void OneWayBind<TComponent, T>(
        this TComponent component, T value, 
        Expression<Func<TComponent, object>> controlProperty, 
        Expression<Func<T, object>> modelProperty
    )
        where TComponent : IBindableComponent
        where T : INotifyPropertyChanged
    {
        component.DataBindings.Add(new Binding(
            StringExtensions.PropertyName(controlProperty),
            value,
            StringExtensions.PropertyName(modelProperty),
            true,
            DataSourceUpdateMode.Never));
    }
}

用法:

combobox.Bind(name, c => c.SelectedValueId, v => v.OrganizationLegalFormId);
_auditName.OneWayBind(_model, c => c.Text, v => v.DisplayName);

name_model是数据模型类,c => c.SelectedValueIdc => c.Text是控件属性,v => v.DisplayNamev => v.OrganizationLegalFormId是模型属性。

StringExtensions.PropertyName是您的PropertyNameResolver.GetPropertyName

public static class StringExtensions
{
    public static string MethodName<T>(Expression<Action<T>> action)
    {
        return ReflectionExtensions.GetMethodInfo(action).Name;
    }

    public static string PropertyName<T, TProperty>(Expression<Func<T, TProperty>> propertyExpression)
    {
        return PropertyNameInternal(propertyExpression);
    }

    public static string PropertyName<T>(Expression<Func<T>> propertyExpression)
    {
        return PropertyNameInternal(propertyExpression);
    }

    private static string PropertyNameInternal(LambdaExpression propertyExpression)
    {
        if (propertyExpression == null)
            throw new ArgumentNullException(@"propertyExpression");

        var memberExpression = propertyExpression.Body as MemberExpression;
        if (memberExpression == null)
        {
            var ubody = (UnaryExpression)propertyExpression.Body;
            memberExpression = ubody.Operand as MemberExpression;
        }

        if (memberExpression == null)
            throw new ArgumentException(@"The expression is not a member access expression.", @"propertyExpression");

        var property = memberExpression.Member as PropertyInfo;
        if (property == null)
            throw new ArgumentException(@"The member access expression does not access a property.", @"propertyExpression");


        return memberExpression.Member.Name;
    }
}