简体   繁体   English

输入具有相同属性的另一个

[英]Type casting to another with the same properties

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. 在我的应用程序的一个部分中,我使用从xsd方案生成的类型 - 我有2个版本的模式2008和2009 - 我使用的类型是DatumType - 在每个方案中这个类型包含相同的属性 - 它们是精确的,除了名称空间。 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? 有没有办法如何将DatumType(2008)转换为DatumType(2009)所以我可以在我的应用程序中只使用一种类型而不是两种类型?

I am working with c# and win forms, thanks! 我正在使用c#并赢取表格,谢谢!

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. 如果目标类型的字段是可分配的,则可以编写一个使用反射来复制字段的简短方法。

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. 您还可以构建将源类型的对象保存到XML的代码,并将该XML读取到目标类型的对象中。 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. 这稍微脆弱一点,因为它依赖于相同字段的存在以及它们以相同方式转换为XML的事实。

It seems to me the easiest thing to do would be to build a small method to convert one type to the other (since they all share properties), or, if you have access to the source, implement an interface so that you can use the two classes as that interface. 在我看来,最简单的方法是构建一个小方法将一种类型转换为另一种类型(因为它们共享属性),或者,如果您有权访问源,则实现一个接口,以便您可以使用两个类作为该接口。

In other words, if we have two classes, B and C, which inherit interface A (which contains all the properties we're interested in), we can typecast any object of those two classes as an A. 换句话说,如果我们有两个类,B和C,它们继承了接口A(包含我们感兴趣的所有属性),我们可以将这两个类的任何对象类型转换为A.

You can pre-process your XML file with a simple XSLT that corrects the namespace differences. 您可以使用简单的XSLT预处理XML文件,该XSLT可以纠正命名空间差异。

The part 1 of 2 of Identity explain how to do it. Identity的第1部分解释了如何做到这一点。 Basically a transform has templates that matches elements and give an output for each matched element. 基本上,转换具有匹配元素的模板,并为每个匹配的元素提供输出。 The trick is to have a specific template to match the Datum elements and transform them, and a generic transform that matches all kind of elements and simply copy them. 诀窍是拥有一个特定的模板来匹配Datum元素并对它们进行转换,以及一个匹配所有元素的通用转换并简单地复制它们。

If you don't have experience with XSLT, don't be afraid. 如果您没有使用XSLT的经验,请不要害怕。 It's easier to learn than you can expect. 它比你想象的更容易学习。 You can use a tutorial like XSLT tutorial which will allow you to understand the 'Identity' explanation. 您可以使用像XSLT教程这样的教程来了解“身份”说明。

You can use XslCompiledTransform Class to apply the transform. 您可以使用XslCompiledTransform类来应用转换。

You can use Visual Studio to test and debug your XSLT file. 您可以使用Visual Studio来测试和调试XSLT文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM