Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting values between same structure within different namespace. C#

I have two similar[all attributes are same.] structures within different namespaces.

Now when i try to copy values between the objects of these structures i am getting errors.

How can i make it possible to copy values between objects of two similar structures residing in different namespaces?

Thanks in advance.

Regards,

John

like image 255
logeeks Avatar asked Dec 01 '22 08:12

logeeks


2 Answers

You can't, automatically, just using the framework's built-in conversions.

The short names (i.e. within the namespace) are entirely irrelevant here - as far as the CLR is concerned, A.B.C.SomeType and A.B.C1.SomeType are as different as X.Y.Foo and A.B.Bar.

You should either write your own conversion routines, or (preferrably) avoid having two different types in the first place, if they do the same thing. Alternatively you could use a reflection-based approach to perform the conversion... but that's still not getting the runtime to do it.

like image 135
Jon Skeet Avatar answered Dec 02 '22 21:12

Jon Skeet


Use AutoMapper.

Mapper.CreateMap<My.NS1.Structure, My.NS2.Structure>();
My.NS1.Structure struct1;
My.NS2.Structure struct2 = (My.NS2.Structure) Mapper.Map(struct1);
like image 23
Aliostad Avatar answered Dec 02 '22 22:12

Aliostad