Click here to Skip to main content
15,886,632 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
Hi,

i have seen some of the INotifyPropertyChanged member method using [CallerMemberName] as a method argument prefix. i want to know how actually it works. how to do some this similar to it? can any one explain me please?

Note:
I have already read some of the links [^]where we can find some additional improvements in .net4.5 regarding attribute usage. please let me know some links other than similar to this :)

My Custom attribute class
C#
namespace AttributeTesting
{
    [AttributeUsage(AttributeTargets.Parameter, Inherited = false)]
    public class CallerMember : Attribute
    {
public string ParameterName { get; set; }
    }
}


Implemented employeeClass
C#
namespace AttributeTesting.Data
{
    [CallerMember]    
public class EmployeeData : INotifyPropertyChanged
    {
        private string _name;
        
        public string Name
        {
            get { return _name; }
            set { _name = value; OnPropertyChanged(); }
        }


        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;

        private void OnPropertyChanged([CallerMember] string ParameterName = "")
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(ParameterName));
            }
        }

        #endregion
    }
}


try to make it work
C#
namespace AttributeTesting
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            this.Loaded += OnMainWindowLoaded;
        }

        void OnMainWindowLoaded(object sender, RoutedEventArgs e)
        {
            EmployeeData EmployeeData = new EmployeeData { Name = "GoldCoin" };

        }
    }
}


Unable to implement my custom attribute. what should i do next

Note:
I am trying to implement something that .net 4.5 has. The same in my solution which was developed in .net 4.0 :)
Posted
Updated 12-Mar-15 20:48pm
v5
Comments
phil.o 13-Mar-15 6:21am    
Better suffix your class name with 'Attribute'
public class CallerMemberAttribute : Attribute

Then:
private void OnPropertyChanged([CallerMember("yourParameterName")] string whatever = "")

That is called an attribute.
Here is the MSDN page about the CallerMemberNameAttribute:
CallerMemberNameAttribute[^]

You can define your own attribute by deriving from Attribute Class[^].
The class name must be suffixed with 'Attribute' word, but this does not appear when you use it in your code.
Example:
C#
public class MyOwnAttrAttribute : Attribute
{
   // ...
}

[MyOwnAttr]
public class MyDecoratedClass
{
   // ...
}
 
Share this answer
 
Comments
Gold$Coin 13-Mar-15 2:39am    
well, that was not the solution i am looking for. sorry.
phil.o 13-Mar-15 6:18am    
I'm sorry, obviously I misunderstood your question.
Gold$Coin 13-Mar-15 2:46am    
have updated my implementation in my post please take a look and let me know what was wrong in my implementation.
Try changing your implementation to this:
C#
public event PropertyChangedEventHandler PropertyChanged;

protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
    var handler = PropertyChanged;
    if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900