铸造一个对象到另一个 [英] Cast one object to another
问题描述
我有哪些是相同的,只是名称不同,例如这两个代码的用户定义的CLR对象:
I have got two user defined CLR objects which are identical and only differ in name, for example this code:
public class Person
{
public string Name { get; set; }
public string Address { get; set; }
public string LastName { get; set; }
public string ETC { get; set; }
}
public class PersonData
{
public string Name { get; set; }
public string Address { get; set; }
public string LastName { get; set; }
public string ETC { get; set; }
}
我知道,这可以通过以下任一创建对象来完成CLR的,比通过所有属性逐个它。
I know that this can be done by creating an object from either of these CLR's and than pass all properties one by one to it.
但有另一种方式?我获得了一些CLR的是相当大的,15岁以上的属性。
But is there another way? I got a few CLR's that are pretty big, 15+ properties.
一些更多的上下文。这些类的地方已经存在。我生成从数据库中一个模型中使用的EntityFramework
。该数据库具有几乎相同结构的类。
Some more context. The classes where already there. I generated a model from the database using EntityFramework
. The database has almost the exact same structure as the classes.
此外,这是一个很大的代码已经在那里了。这些类还从多个接口继承等重构,现在是不是一种选择,所以我正在寻找一个简单的办法解决了。
Also, it's a lot of code that was already there. These classes also inherit from several interfaces etc. Refactoring now is not an option, so i'm looking for an easy fix for now.
推荐答案
Assumming你不想这两个类从一个接口继承,你可以尝试 Automapper 它是自动映射相似的类的库。
Assumming you don't want both classes to inherit from an interface you can try Automapper it's a library for automatically mapping similar classes.
这篇关于铸造一个对象到另一个的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!