开发者

Converting strings in to an expression trees?

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 t开发者_运维知识库he 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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜