Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# WPF How to set Property setter method dynamically?

I've been searching around but I just can't seem to find what I'm looking for, so I'll give it a go here.

Situation: I have the class MainWindow and MainWindowData. In MainWindowData are only public properties defined with the attribute UpdateGUI.

public class UpdateGUI : Attribute { }

public class MainWindowData
{
    [UpdateGUI]
    public string TESTVAR { get; set; }
}

Now I want to add a method to each property's setter method in MainWindowData. More specific:

void OnPropertyChanged(String PropertyName);

I figured I'd fetch all UpdateGUI properties in the MainWindow constructor, and then somehow add another method to it, but this is where I'm stuck. I use this code to fetch all properties, which works:

List<PropertyInfo> properties = (from pi in typeof(MainWindowData).GetProperties()
                                 where pi.GetCustomAttributes(typeof(UpdateGUI), false).Any()
                                 select pi).ToList();

This gives me a nice list of all properties that I have to update.

So the question is: how can I make it so that the properties dynamically get transformed from:

[UpdateGUI]
public string TESTVAR { get; set; }

to:

[UpdateGUI]
private string _TESTVAR;
public string TESTVAR { 
    get {
        return _TESTVAR;
    }
    set {
        _TESTVAR = value;
        OnPropertyChanged("TESTVAR");
    }
}

Thank you for any help! It will be highly appreciated :)

Greetings

like image 948
Rik De Peuter Avatar asked Jun 20 '11 08:06

Rik De Peuter


People also ask

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.

What do you mean by C?

C is a structured, procedural programming language that has been widely used both for operating systems and applications and that has had a wide following in the academic community. Many versions of UNIX-based operating systems are written in C.

What is C language used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...


1 Answers

What you are looking for has been solved in the concept of Aspect Oriented Programming (AOP).

One example is in PostSharp, (Also, see details here) which lets you write your data/viewmodel classes like this:

[NotifyPropertyChanged]
public class Shape
{
    public double X { get; set; }
    public double Y { get; set; }
}

public class Rectangle : Shape
{
    public double Width { get; set; }
    public double Height { get; set; }
}

If you don't like PostSharp, I'm sure the other AOP frameworks out there has similar functionality.

EDIT

I just found NotifyPropertyWeaver which does this for you without requiring a full AOP framework.

It uses the Mono.Cecil stuff to inject notification code during compilation and is installable either through NuGet (this is what I did) or from the project web site.

By default, it doesn't even require attributes, (it automatically figures out which properties and classes need change notification) but you can be explicit also, like so:

[NotifyProperty]
public int FooBar { get; set; }

One nice feature I found in it was the possibility to declare dependencies between properties. In this case, RaisePropertyChanged("FoobarTimesTwo") will be called whenever FooBar changes.

[DependsOn("FooBar")]
public int FoobarTimesTwo
{
    get { return FooBar * 2; }
}
like image 123
Isak Savo Avatar answered Sep 30 '22 01:09

Isak Savo