将类型转换为具有相同属性的另一个 [英] Type casting to another with the same properties

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

问题描述

在我的应用程序的一部分中,我使用从xsd方案生成的类型-我有2个版本的架构2008和2009-我使用的类型是DatumType-在每个方案中此类型都包含相同的属性-它们是精确的,除了名称空间。
有什么方法可以将DatumType(2008)转换为DatumType(2009),这样我只能在一种类型中使用我的应用程序,而不是两种类型?

In one section of my application, I use type generated from xsd scheme - I have 2 version of schemas 2008 and 2009 - type I use is DatumType - in every scheme this type contain the same properties - they are exact, except namespaces. Is there any way how to cast DatumType (2008) to DatumType (2009) so I can work in my application only with one type, instead of two?

我正在使用c#并赢得表格,谢谢!

I am working with c# and win forms, thanks!

推荐答案

不,由于编译器所知,这是没有关联的方法,因为它们是两种不相关的类型。

No, there is no way to cast one to the other, because these are two unrelated types, as far as the compiler knows.

如果目标类型的字段是可分配的,您可以编写一个使用反射来复制字段的简短方法。

If the fields of the target type are assignable, you can write a short method that uses reflection to copy the fields.

您还可以构建代码,将源类型的对象保存为XML,然后读取将XML转换为目标类型的对象。这有点脆弱,因为它依赖于相同字段的存在以及它们以相同方式转换为XML的事实。

You could also build code that saves objects of the source type to XML, and reads that XML into the objects of the target type. This is slightly more fragile, because it relies on the presence of identical fields and the fact that they are converted to XML in the same way.

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

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