2

We have a requirement to provide user friendly descriptions for types. We have a created a resource file which maps the type to a description.

The full name of the instance with the dots replaced with underscores is used as the key. The description is a string and contains templates that refer to property in the instance.

When we get an instance, we get its type, get the key, and use it to find the resource value. Then use regex to pull out those template properties. Then use reflection to actually get the value of the property.

for eg. The instance may be Address the key would be MyNameSpace_MyPublicTypes_Address(say the full name is'MyNameSpace.MyPublicTypes.Address ')

The description can be 'User stays in {State.City} in {Country}' -- State and Country are properties on the Address class. The State has a property City.

Is it possible to have something like 'obj=>obj.State.City' or 'obj=>obj.Country' ? or some sort of expression ?

I am using obj because it is the reflected instance.

Appreciate any help. Not sure if this question has been asked before.

Bart Kiers
  • 153,868
  • 34
  • 276
  • 272
AlwaysAProgrammer
  • 2,821
  • 2
  • 29
  • 40
  • 1
    have you seen this question? http://stackoverflow.com/questions/159017/named-string-formatting-in-c – Jimmy Jan 22 '10 at 18:51

1 Answers1

1

This is pretty-much what the dynamic LINQ library (one of the .NET 3.5 samples) does. The source is all available, or for usage see here. You should be able to trace the code that parses the strings into Expressions. Of course, it isn't hard to split on . and assemble it manually; I have a dynamic OrderBy implementation here that does this.

Community
  • 1
  • 1
Marc Gravell
  • 927,783
  • 236
  • 2,422
  • 2,784
  • Thanks Marc .I haven't seen the dynamic LINQ library yet. If it helps me to find properties on reflected objects, that would be awesome!! – AlwaysAProgrammer Jan 22 '10 at 21:36