有没有办法将键/值对列表转换为数据传输对象
...除了明显的循环列表和肮脏的伟大案例声明之外!
我在脑海中思考了一些 Linq 查询,但似乎没有任何接近的结果。
这是一个 DTO 示例(如果有帮助的话):
class ClientCompany
{
public string Title { get; private set; }
public string Forenames { get; private set; }
public string Surname { get; private set; }
public string EmailAddress { get; private set; }
public string TelephoneNumber { get; private set; }
public string AlternativeTelephoneNumber { get; private set; }
public string Address1 { get; private set; }
public string Address2 { get; private set; }
public string TownOrDistrict { get; private set; }
public string CountyOrState { get; private set; }
public string PostCode { get; private set; }
}
恐怕我们无法控制以 KV 对形式获取数据的事实。
虽然每个 KV 对到每个属性都有一个有效的映射,而且我确实提前知道密钥,但它们的命名与 DTO 的名称不同。
...apart from the obvious looping through the list and a dirty great case statement!
I've turned over a few Linq queries in my head but nothing seems to get anywhere close.
Here's the an example DTO if it helps:
class ClientCompany
{
public string Title { get; private set; }
public string Forenames { get; private set; }
public string Surname { get; private set; }
public string EmailAddress { get; private set; }
public string TelephoneNumber { get; private set; }
public string AlternativeTelephoneNumber { get; private set; }
public string Address1 { get; private set; }
public string Address2 { get; private set; }
public string TownOrDistrict { get; private set; }
public string CountyOrState { get; private set; }
public string PostCode { get; private set; }
}
We have no control over the fact that we're getting the data in as KV pairs, I'm afraid.
and whilst there is an effective mapping of each KV pair to each property and I do know the keys in advance they're not named the same as the DTO.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这是一个优雅、可扩展、可维护且速度极快的解决方案,用于从字典加载 DTO。
创建一个控制台应用程序并添加这两个文件。其余的都是自我记录的。
要点:
注意:如果您复制了上一个 DynamicProperties.cs,您将需要获取这个。我添加了一个标志以允许生成以前版本中没有的私有设置器。
干杯。
program.cs
DynamicProperties.cs
Here is an elegant, extensible, maintainable and blazingly fast solution for loading DTOs from Dictionaries.
Create a console app and add these two files. The rest is self documenting.
The salient points:
NOTE: If you copied the previous DynamicProperties.cs, you will want to get this one. I added a flag to allow generation of private setters that was not in the previous version.
Cheers.
program.cs
DynamicProperties.cs
如果您可以使数据看起来像
['Title':'Mr', 'Forenames':'John', 'Surname':'Doe',...]
,那么您应该能够将 kvp JSON 反序列化为源对象。If you can get the data to look like
['Title':'Mr', 'Forenames':'John', 'Surname':'Doe',...]
, then you should be able to JSON deserialize the kvp into your source object.在这种情况下,我将使用反射将键值对映射到对象属性。例如,请检查此SO问题的已接受答案
In such a case, I would use reflection to map the Key-Value pairs to the object properties. For an example, check the accepted answer on this SO question
或者,如果您不想进行反射,您可以使用它(这不会工作得非常快):
Alternatively if you don't want to go with reflection you could use this (this wont' work blazingly fast):