Click here to Skip to main content
15,884,628 members
Articles / Programming Languages / C#

Once More About Macros in C#

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
11 May 2010Apache 17.2K   1   5
About macros in C#

I am writing a WPF app. In WPF, they like very much all kinds of notifications about property changes. Thus, I’ve got a class with a bunch of properties similar to this:

C#
class Person
{
    void OnPropertyChanged(string propertyName)
    {
    …
    }

    public string LastName
    {
        get { return _lastName; }

        set
        {
            if (_lastName != value)
            {
                _lastName = value;
                OnPropertyChanged("LastName");
            }
        }
    }
    string _lastName;

    public int Age
    {
        get { return _age; }

        set
        {
            if (_age != value)
            {
                _age = value;
                OnPropertyChanged("Age");
            }
        }
    }
    int _age;
}

This is long, boring, and bloated. Of course, I created a code snippet for generating the properties, but it works only once, at the time of writing, the code is still bloated, and all the changes must be done by hand. Besides, it is unable to change case automatically, so that Age becomes _age.

I would like to see something like:

C#
class MyClass
{
    public Observable<string> LastName;
    public Observable<int> Age;
}

property Observable<T>
{
    T _value;
    get { return _value; }
    set
    {
        if (_value != value)
        {
            _value = value;
            OnPropertyChanged(#PropertyName);
        }
    }
}

Of course, the syntax is approximate. Unfortunately, nothing of the sort exists. Generics are of no help here, and there are no macros in C#.

This article was originally posted at http://www.ikriv.com/blog?p=445

License

This article, along with any associated source code and files, is licensed under The Apache License, Version 2.0


Written By
Technical Lead Thomson Reuters
United States United States
Ivan is a hands-on software architect/technical lead working for Thomson Reuters in the New York City area. At present I am mostly building complex multi-threaded WPF application for the financial sector, but I am also interested in cloud computing, web development, mobile development, etc.

Please visit my web site: www.ikriv.com.

Comments and Discussions

 
GeneralAn implementation Pin
Leung Yat Chun12-May-10 20:08
Leung Yat Chun12-May-10 20:08 
GeneralRe: An implementation Pin
Ivan Krivyakov13-May-10 8:25
Ivan Krivyakov13-May-10 8:25 
GeneralRe: An implementation Pin
Leung Yat Chun13-May-10 21:13
Leung Yat Chun13-May-10 21:13 
GeneralRe: An implementation Pin
Ivan Krivyakov14-May-10 9:11
Ivan Krivyakov14-May-10 9:11 
AnswerRe: An implementation Pin
Leung Yat Chun5-Jun-10 9:28
Leung Yat Chun5-Jun-10 9:28 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.