1

Is there a way (in c# .Net 4.0) to provide object property name in strongly type manner? For instance, if we have an object Person

Public class Person{
    public string Name { get; set; }
    public int Age { get; set; }
    }

And I want to sent to some other method the parameters Name, Age property name but not as string but strongly type:

SomeMethodThatWantToKnowKeys(Person.Name,Person.Age);

What I want to achieve is that if someone change the property name, he will have to change they property names he is sending to 'SomeMethodThatWantToKnowKeys'. Maybe reflection? A better way will be without updating the object itsef or creating an instance of it.

Igal
  • 1,084
  • 2
  • 13
  • 33
  • please be more specific and refine your question, it's hard to follow what you're after. – Alex Nov 13 '13 at 22:57
  • Are you saying you want to have `Method(string name)` to become `Method(string firstname)` when you decide you now send firstnames instead of names? – Jeroen Vannevel Nov 13 '13 at 22:58
  • Yes, there is a way to do this. Search for "strongly typed INotifyPropertyChanged" or similar to see the technique. – Jon Nov 13 '13 at 22:58
  • 1
    By the way, it's `string Name`, not `Name string`. – dcastro Nov 13 '13 at 22:58
  • See if one of my questions helps out: http://stackoverflow.com/questions/16154172/how-to-decompose-expression-to-satisfy-generic-property-change-method ... this allowed me to pass in () => p.Name. – IAbstract Nov 13 '13 at 22:59
  • possible duplicate of [Getting sub property names strongly typed](http://stackoverflow.com/questions/17882930/getting-sub-property-names-strongly-typed) – IAbstract Nov 13 '13 at 23:05
  • Sorry for my bad English, seems that I fount something helpful here: http://stackoverflow.com/questions/17882930/getting-sub-property-names-strongly-typed – Igal Nov 13 '13 at 23:01
  • I've seen this question in many variances, so I've encapsulated it in a Q&A post, here - http://stackoverflow.com/questions/32310767/c-sharp-how-to-get-the-name-of-an-interface-property-func-action-in-a-strongl – ShloEmi Aug 31 '15 at 13:36

2 Answers2

3

If I understand what you want, you could use expressions:

void SomeMethod<T>(Expression<Func<T>> expr)
{
    var memberExpr = expr.Body as MemberExpression;
    Console.WriteLine("{0}: {1}", memberExpr.Member.Name, expr.Compile()());
}

var person = new { Name = "John Doe", Age = 10 };
SomeMethod(() => person.Name); // prints "Name: John Doe"
SomeMethod(() => person.Age);  // prints "Age: 10"
p.s.w.g
  • 146,324
  • 30
  • 291
  • 331
  • I've seen this question in many variances, so I've encapsulated it in a Q&A post, here - http://stackoverflow.com/questions/32310767/c-sharp-how-to-get-the-name-of-an-interface-property-func-action-in-a-strongl Also, I've used your answer for 'GetPropertyName' and gave the credit to you.. – ShloEmi Aug 31 '15 at 13:36
3

Okay so while there are some ugly hacks there is a much better and clearner solution. This problem is because the abstraction is not in place.

What is a name? A String? It could be an int, double, char, float....anything...you do not really care about the underlying type. You care about the concept or notion of a name. I know this is a bit more deep, but this experience will help you with good design.

Here is what I suggest for now, but personally I might go do more.

public class PersonName
{
    public String Name { get; set; }
}

public class PersonAge
{
    public int Age {get;set;}
}

public class Person
{ 
    public PersonName PersonName {get;set;}
    public PersonAge PersonAge {get;set;}
}

So now your method signature is:

    SomeMethodThatWantToKnowKeys(PersonName,PersonAge);

Type safety is an amazing thing!