1

我想得到一个给定属性的字符串表示。这样,我可以使用这个字符串,NotifyPropertyChanged并且在重构属性名称后仍然可以。

编辑:我正在使用 .NET 4.0

更新:我还想为DependencyProprtys 提供名称,即我需要在静态变量赋值期间使用该值。

相同的示例代码来解释:

// actual code

private int prop = 42;
public int Prop
{
    get
    {
        return prop;
    }
    set
    {
        prop = value;
        NotifyPropertyChanged("Prop"); // I'd like to replace the hard-coded string here
    }
}


// code as I'd like it to be

private int propNew = 42;
private static readonly string PropNewName = GainStringFromPropertySomeHow(PropNew); // should be "PropNew"
public int PropNew
{
    get
    {
        return propNew;
    }
    set
    {
        propNew = value;
        NotifyPropertyChanged(PropNewName); // <== will remain correct even if PropNew name is changed
    }
}

重构后:

private int prop = 42;
public int PropNameChanged
{
    get
    {
        return prop;
    }
    set
    {
        prop = value;
        NotifyPropertyChanged("Prop"); // oops
    }
}


private int propNew = 42;
private static readonly string PropNewName = GainStringFromPropertySomeHow(PropNewNameChanged); // should be "PropNewNameChanged"
public int PropNewNameChanged
{
    get
    {
        return propNew;
    }
    set
    {
        propNew = value;
        NotifyPropertyChanged(PropNewName); // still correct
    }
}
4

4 回答 4

1

如果您尚未使用 .Net 4.5,因此无法使用 CallerMemberName,则可以使用此方法:https ://stackoverflow.com/a/3191598/869250

于 2013-08-01T09:04:59.513 回答
1

这是如何通过反射获取当前属性名称的副本?

所以你可以这样做

NotifyPropertyChanged(MethodBase.GetCurrentMethod().Name);
于 2013-08-01T09:05:19.090 回答
1

我认为这可能会有所帮助:

// This method is called by the Set accessor of each property.
// The CallerMemberName attribute that is applied to the optional propertyName
// parameter causes the property name of the caller to be substituted as an argument.
private void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
{
if (PropertyChanged != null)
{
    PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}

来源和更多解释:http: //msdn.microsoft.com/de-de/library/system.componentmodel.inotifypropertychanged.aspx

在这里:http: //msdn.microsoft.com/de-de/library/system.runtime.compilerservices.callermembernameattribute.aspx

于 2013-08-01T08:54:58.747 回答
0

我在 Stackoverflow 上找到了一个解决方案:

https://stackoverflow.com/a/672212/1254743

https://stackoverflow.com/a/2820759/1254743

结果代码:

public static class PropertyNameExtractor
{
    /// <summary>
    /// Usage: PropertyNameExtractor.ExposeProperty(() => this.YourProperty)
    /// yields: "YourProperty"
    /// </summary>
    public static string ExposeProperty<T>(Expression<Func<T>> property)
    {
        var expression = GetMemberInfo(property);
        return expression.Member.Name;

    }

    private static MemberExpression GetMemberInfo(Expression method)
    {
        LambdaExpression lambda = method as LambdaExpression;
        if (lambda == null)
            throw new ArgumentNullException("method");

        MemberExpression memberExpr = null;

        if (lambda.Body.NodeType == ExpressionType.Convert)
        {
            memberExpr =
                ((UnaryExpression)lambda.Body).Operand as MemberExpression;
        }
        else if (lambda.Body.NodeType == ExpressionType.MemberAccess)
        {
            memberExpr = lambda.Body as MemberExpression;
        }

        if (memberExpr == null)
            throw new ArgumentException("method");

        return memberExpr;
    }

}

在我的课堂上:

class MyClass: INotifyPropertyChanged
{
    public MyClass()
    {
        this.nameOf_MyProperty = PropertyNameExtractor.ExposeProperty(() => this.MyProperty);

    }


    private readonly string nameOf_MyProperty;
    private int myProperty = 42 ;
    public int MyProperty
    {
        get
        {
            return myProperty;
        }
        set
        {
            myProperty= value;
            NotifyPropertyChanged(nameOf_MyProperty);
        }
    }

    private void NotifyPropertyChanged(String PropertyName)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(PropertyName));
        }
    }
}
于 2013-08-01T09:05:11.347 回答