Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/302.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在C语言中是否有办法将属性转换为包含它的字符串';她叫什么名字?_C#_Reflection_Properties_Inotifypropertychanged - Fatal编程技术网

C# 在C语言中是否有办法将属性转换为包含它的字符串';她叫什么名字?

C# 在C语言中是否有办法将属性转换为包含它的字符串';她叫什么名字?,c#,reflection,properties,inotifypropertychanged,C#,Reflection,Properties,Inotifypropertychanged,我希望得到给定属性的字符串表示形式。通过这种方式,我可以将此字符串用于NotifyPropertyChanged,重构属性名称后仍然可以 编辑:我正在使用.NET4.0 更新:我还想让dependencyproptys的名称可用,也就是说,我需要静态变量赋值期间的值 要解释的示例代码相同: // actual code private int prop = 42; public int Prop { get { return prop; } se

我希望得到给定属性的字符串表示形式。通过这种方式,我可以将此字符串用于
NotifyPropertyChanged
,重构属性名称后仍然可以

编辑:我正在使用.NET4.0

更新:我还想让
dependencypropty
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
    }
}

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

// 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));
}
}
来源和更多解释:

在这里:

如果您尚未使用.Net 4.5,因此无法使用CallerMemberName,则可以使用此方法:

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

生成的代码:

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

}
这是一个复制品

所以你可以这么做

NotifyPropertyChanged(MethodBase.GetCurrentMethod().Name);

看起来与我在另一个答案中找到的解决方案类似。这是一个不错的解决方案,但我使用的是.NET 4.0。
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));
        }
    }
}
NotifyPropertyChanged(MethodBase.GetCurrentMethod().Name);