开发者

How cast 2 classes with same attribits(type,number,..) but with diffrent name

开发者 https://www.devze.com 2023-02-01 15:17 出处:网络
Hi everybody i\'am working in a catalogmanager. i have 2 classes : First one : got from xsd.exe generator of XMl file

Hi everybody i'am working in a catalogmanager.

i have 2 classes :

First one : got from xsd.exe generator of XMl file 2nd : gor from WebService开发者_Go百科 proxy class

thes 2 classes are the same (same attributs), the only thing is that they are devloped by 2 differents team : example :

public partial class CatalogPackageAMSApp_Data {

    private string appField;

    private byte nameField;

    private string valueField;
}

public partial class AppData {

    private string appField;

    private string nameField;

    private string valueField;
}

how can i cast, affect, ... those 2 classes each other.

Best regards

I'am avialable for any further explanation


They are both partial classes. Just attach the same interface to each with the appropriate properties, and then they can both cast to the interface type.

You can't cast between them, because they have no relation to each other. There has to be a commonality in either a base class or an interface.


You can't, basically - they're not the same class, and an instance of CatalogPackageAMSApp_Data is not an instance of AppData or vice versa.

What you could do is write methods/constructors to convert between the two - and possibly implement conversion operators. Personally I'd prefer the method approach as it makes it more obvious when you're performing a conversion.

Do you really have to have both classes though? If you really need two classes, could they both use composition to contain an instance of a third, common class which contains the actual data?


If they are exactly the same, is there any reason you can't change one to inherit the other? Then casting between types is simple. Since both are auto-generated, I would modify the class that is less frequently auto-generated.


I'm not sure if this applies to your situation, but I believe that the web service proxy generator (svcutil.exe if you're using WCF, otherwise wsdl.exe) has the ability to reuse existing types when generating a web service proxy.

http://blogs.msdn.com/b/lifenglu/archive/2007/05/09/type-sharing-in-wcf-service-reference.aspx

Other links on how to generate proxies that share types:

svcutil.exe redundant proxy classes

http://msdn.microsoft.com/en-us/library/aa347733.aspx

If you're using .NET 2.0:

wsdl.exe /sharetypes

http://msdn.microsoft.com/en-us/library/7h3ystb6%28v=vs.80%29.aspx


I'd say a common interface between the two would be the correct solution. If that's not an option, you could use Automapper (http://automapper.codeplex.com/) to handle the mapping for you.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号