【发布时间】:2018-01-05 21:23:09
【问题描述】:
我试图想办法利用动态类型来提供其成员的自然使用,但它最终源自Dictionary<string, string> 对象。但是,我希望能够定义如何解析给定属性的规则。这可能吗?
我想要这样的东西。
var dynamicCollection = new MyDyanmicType();
dynamicCollection.Build(/*some dictionary*/); // or just implement an implicit operator
dynamicCollection.MemberProperty; // <-- this should let me search the dictionary how I want for some form of the string "MemberProperty"
dynamicCollection.PropertyThatDoesntExist; // <-- this should give me an opportunity to exhaust my resolution and throw back a custom exception that I choose
用例是我有一些由产品管理人员在数据库中动态设置的字段。不幸的是,我需要防止人为错误的波动。鉴于上述情况,有人可能会输入 memberProperty、Member Property 等,因此我需要控制如何查找声明的成员,同时保持对普通 POCO 的干净使用。
我面临的问题是我有大量的样板字典代码我试图摆脱,这将使我摆脱一些相当讨厌和冗长的运行时检查。
【问题讨论】:
标签: c# .net expandoobject