2013-08-01 62 views
1

我想獲得給定屬性的字符串表示。這樣我可以使用這個字符串NotifyPropertyChanged,並且在重構屬性名稱後仍然可以。C#中有一種方法可以將屬性轉換爲包含名稱的字符串嗎?

編輯:我使用.NET 4.0

更新:我也想有可供DependencyProprty S上的名字,也就是我需要在靜態變量賦值時間的價值。

相同的示例代碼解釋:

// 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 
    } 
} 

回答

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

+0

將是一個不錯解決方案,但我使用.NET 4.0。 – Onur

0

我發現這裏的解決方案#2:

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)); 
     } 
    } 
} 
相關問題