Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way of making C# binding work statically?

This probably applies to other places, but in WinForms, when I use binding I find many methods want to take the name of the property to bind to. Something like:

class Person
{
    public String Name { get { ... } set { ... } }
    public int Age { get { ... } set { ... } }
}

class PersonView
{
    void Bind(Person p)
    {
        nameControl.Bind(p,"Name");
        ageControl.Bind(p,"Age");
    }
}

The big problem I keep having with this is that "Name" and "Age" are specified as strings. This means the compiler is no help if someone renames one of Person's properties. The code will compile fine, but the bindings will be broken.

Is there a standard way of solving this that I've missed? It feels like I need some keyword, maybe called stringof to match the existing typeof. You could use it something like:

ageControl.Bind(p,stringof(p.Age).Name);

stringof could return some class that has properties for getting the full path, part of the path, or the string so you can parse it up yourself.

Is something like this already do-able?

like image 683
Scott Langham Avatar asked Sep 23 '09 15:09

Scott Langham


1 Answers

Have a look at this code snippet I've posted in another question, it can help you! (But only, if you are using .NET 3.5)

Best Regards
Oliver Hanappi

like image 152
Oliver Hanappi Avatar answered Nov 02 '22 19:11

Oliver Hanappi