将字符串转换为表达式树?
我们需要为类型提供用户友好的描述。我们创建了一个资源文件,它将类型映射到描述。
将实例的全名中的点替换为下划线作为键。 描述是一个字符串,包含引用实例中属性的模板。
当我们获取一个实例时,我们获取它的类型,获取键,并使用它来查找资源值。然后使用正则表达式提取这些模板属性。然后使用反射来实际获取属性的值。
例如。 该实例可能是地址 键是 MyNameSpace_MyPublicTypes_Address(假设全名是“MyNameSpace.MyPublicTypes.Address”)
描述可以是“用户停留在 {Country} 中的 {State.City}”——State 和 Country 是 Address 类的属性。国家有一个财产城市。
是否有可能有类似的东西 'obj=>obj.State.City' 或 'obj=>obj.Country' ?或者某种表达方式?
我使用 obj 因为它是反射实例。
感谢任何帮助。不确定以前是否有人问过这个问题。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这几乎就是动态 LINQ 库(.NET 3.5 示例之一)所做的事情。来源全部可用,或用于使用参见此处< /a>.您应该能够跟踪将字符串解析为
表达式
的代码。当然,对.
进行拆分并手动组装并不难;我有一个动态OrderBy
实现这里来执行此操作。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
Expression
s. Of course, it isn't hard to split on.
and assemble it manually; I have a dynamicOrderBy
implementation here that does this.