c# - 2 个对象,完全相同(命名空间除外)c#

标签 c# namespaces casting

我正在使用第 3 方的网络服务集,但遇到了一个小问题。在我手动制作将每个属性从源复制到目标的方法之前,我想我会在这里寻求更好的解决方案。

我有 2 个对象,一个是 Customer.CustomerParty 类型,另一个是 Appointment.CustomerParty 类型。 CustomerParty 对象实际上是完全相同的属性和子对象。但是我不能从 1 转换到另一个。

所以,我需要从网络服务中找到某个人。我可以通过调用 Customer.FindCustomer(customerID) 来做到这一点,它会返回一个 Customer.CustomerParty 对象。

我需要找到我找到的那个人,然后在“CreateAppointment”请求中使用他们几行。 Appointment.CreateAppointment 采用约会对象,约会对象包含 CustomerParty 对象。

但是,它想要的 CustomerParty 对象实际上是 Appointment.CustomerParty。我有一个 Customer.CustomerParty。

明白我的意思了吗?有什么建议吗?

最佳答案

你为什么不使用AutoMapper ?然后你可以这样做:

TheirCustomerPartyClass source = WebService.ItsPartyTime();

YourCustomerPartyClass converted = 
    Mapper.Map<TheirCustomerPartyClass, YourCustomerPartyClass>(source);

TheirCustomerPartyClass original = 
    Mapper.Map<YourCustomerPartyClass, TheirCustomerPartyClass>(converted);

只要属性相同,您就可以像这样创建一个非常简单的 map :

Mapper.CreateMap<TheirCustomerPartyClass, YourCustomerPartyClass>();
Mapper.CreateMap<YourCustomerPartyClass, TheirCustomerPartyClass>();

关于c# - 2 个对象,完全相同(命名空间除外)c#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3788827/

相关文章:

c++ - 我如何在 void* 和 boost shared_ptr 之间进行转换

c# - 为什么我的查询不运行?

C# WinForm 上下拖动点击

c# - 将 java 项目转换为 .Net 后如何访问 System.getProperties()

python - 实例化时如何引用传递给我的类的对象?

ruby-on-rails - 在命名空间模型上添加一对多关联

c# - ODP.NET连接池: How to tell if a connection has been used

php - Phalcon:命名空间类声明错误

c++ - 当两个链接的 static_cast 可以完成它的工作时,为什么我们在 C++ 中有 reinterpret_cast?

java - 为什么这个方法需要类型转换?