将字符串转换为表达式树?

发布于 2024-08-19 08:50:56 字数 506 浏览 3 评论 0原文

我们需要为类型提供用户友好的描述。我们创建了一个资源文件,它将类型映射到描述。

将实例的全名中的点替换为下划线作为键。 描述是一个字符串,包含引用实例中属性的模板。

当我们获取一个实例时,我们获取它的类型,获取键,并使用它来查找资源值。然后使用正则表达式提取这些模板属性。然后使用反射来实际获取属性的值。

例如。 该实例可能是地址 键是 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

可是我不能没有你 2024-08-26 08:50:56

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文