将ClassA的对象转换为ClassB,具有相同的属性和方法 [英] Converting a object of ClassA to ClassB, same properties and methods

查看:150
本文介绍了将ClassA的对象转换为ClassB,具有相同的属性和方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我有两个具有完全相同的属性和方法的类,基本上已经复制了源代码,是的,我知道那是设计问题;)

我不能整夜翻动整个设计,对我来说,问题听起来很有趣;)

正如我已经提到的,类是相同的,只是名称不同.是否有可能将ClassA的对象转换"为ClassB.

尝试过LinFu.Reflection.DynamicObject-> CreateDuck已经存在,但是它失去了所有属性(我做错什么了吗?).

.NET 4.0关键字动态功能也无济于事.

问候,
Kjartan

Hi,

i''ve got two classes with exactly the same properties and methods, basically the source code is copied and yes, i know thats the design problem ;)

I cannot flip the whole design over night and the questions sounds pretty interesting to me ;)

Is it possible to "cast" a object of ClassA to ClassB, as i already mentioned the classes are the same, only the name is different.

Tryed LinFu.Reflection.DynamicObject -> CreateDuck already but it looses all properties (am i doing something wrong?).

The .NET 4.0 Keyword dynamic doesn''t help me either.

Regards,
Kjartan

推荐答案

如果它们确实相同,那么是的,我会完全放弃其中之一.

如果出于某种原因而不能解决问题,那么我将不愿意创建一个包含所有属性和方法的新的第三类巫婆,而不是永久解决该问题,然后从中派生先前的两个属性和方法.它们中的每一个都可以在不丢失任何信息的情况下被向下"转换为源,并且当两者结合在一起时,在所有情况下都使用源类.

然后慢慢地,可以用源替换两个单独的类并删除它们.
If they are truly identical, then yes, I would ditch one of them completely.

If for whatever reasons you can''t, then rather than perpetuate the problem I would be tempted to create a new third class witch contains all the properties and methods, then derive both of the previous ones from that. Each of them could be cast "down" to the source without losing any info, and the source class used in all situations when the two come together.

Then slowly, the two separate classes could be replaced with the source and deleted.


我刚刚从我的团队那里得到了问题,事实证明我们根本无法触及这些类.由于它们都来自网络服务,因此我们没有来源.

所以出现了问题:两个Web服务在不同的命名对象中返回相同的数据,而我想要一种自动的机制将一个对象转换为另一个对象.
考虑过XML(反)序列化及其间的一些操作,可以尝试一下;)
I''ve just got the problem from my team and as it turned out we cannot touch these classes at all as they both come from webservices we dont have sources from.

So the problem: two webservices, returning the same data in different named objects and i want an automated meachanism to cast one object into the other.
Thought about XML (de)serialization and some manipulation in between already, gonna give it a try ;)


这篇关于将ClassA的对象转换为ClassB,具有相同的属性和方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆