问题描述
我有两个班
public class foo1
{
public int id;
public string image_link;
public string sale_price;
}
和
public class foo2
{
public int Id;
public string ImageLink;
public string SalePrice
}
属性值只能通过下划线和情况不同。我需要这两个类映射。
The property values differ only by underscore and cases. I need to map these two classes.
现在我想是这样,其工作的内容:
For now I am trying something like this and its working:
//var b = object of foo2
var a = new foo1{
a.id = b.Id,
a.image_link = b.ImageLink,
a.sale_price = b.SalePrice
}
我听说AutoMapper,但是我din't有明确我是多么想用那个或者理念是无视案件的选项或者它强调。或者有没有更好的解决办法呢?
I heard of AutoMapper, but I din't have clear idea of how i am going to use that or where is the option of ignoring cases or underscores in it. or is there any better solution to it?
推荐答案
您的代码是罚款和按预期工作。
Your code is fine and works as expected.
我个人建议你的不可以使用automapper。大约有为什么的互联网上,这里是例如,一个很多的解释:
I would personally advise you to not use automapper. There are plenty explanations about why on the Internet, here's for example one: http://www.uglybugger.org/software/post/friends_dont_let_friends_use_automapper
基本上,主要的问题是,你的代码会悄悄地在运行时失败,如果你重新命名一些物业无需修改 foo2的
对象的 foo1
对象。
Basically the major issue is that your code will silently fail at runtime if you rename some property on your foo1
object without modifying your foo2
object.
这篇关于映射在C#两班的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!