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
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.
Use AutoMapper.
Mapper.CreateMap<My.NS1.Structure, My.NS2.Structure>();
My.NS1.Structure struct1;
My.NS2.Structure struct2 = (My.NS2.Structure) Mapper.Map(struct1);
I ran into this same problem when consuming multiple web services from an external provider through WCF. There is a very large tree of nested objects, which is just painful for doing mapping between classes, especially since this tree has several instances of holding an abstract base class as a member. Since these classes are all defined with the XML serialization attributes needed to transmit to a web service, I opted to use the serializer to do the conversion for me.
TOutput ConvertEquivalentTypes<TInput, TOutput>(TInput structure)
where TInput : class
where TOutput : class
{
TOutput result = null;
using (Stream data = new MemoryStream())
{
new XmlSerializer(typeof(TInput)).Serialize(data, structure);
data.Seek(0, SeekOrigin.Begin);
result = (TOutput)new XmlSerializer(typeof(TOutput)).Deserialize(data);
}
return result;
}
An alternative to That Chuck Guy's answer - you could use reflection to get and set the values. No idea what the performance benefits/detriments are.
public static class EquivelantStructureConversion<TInput, TOutput>
where TInput : class
where TOutput : new()
{
public static TOutput Convert(TInput input)
{
var output = new TOutput();
foreach (var inputProperty in input.GetType().GetProperties())
{
var outputProperty = output.GetType().GetProperty(inputProperty.Name);
if (outputProperty != null)
{
var inputValue = inputProperty.GetValue(input, null);
outputProperty.SetValue(output, inputValue, null);
}
}
return output;
}
}
The above example will not throw an exception if the property on the output type doesn't exist, but you could easily add it.
From the description in the question it sounds like you want to have implicit conversion between your types. Whilst you may be able to do this, and in addition to the other suggestions, you might be better using other methods like
- having a class to do the conversion for you
- adding a constructor to each class to generate a type of A from a type of B and vice versa
- adding an extension method to each to act as a factory for producing the other
as all attributes are same, you can e.g. define an interface, describe your structure and implement this interface in your structures. Then cast your structures to the interface to copy values.
regards, O
You can use a union:
public struct A
{
int x, y;
double a, b;
public A(int x, int y, double a, double b)
{
this.x = x;
this.y = y;
this.a = a;
this.b = b;
}
}
public struct B
{
int x, y;
double a, b;
}
[StructLayout(LayoutKind.Explicit)]
public class Union
{
[FieldOffset(0)]
public A a;
[FieldOffset(0)]
public B b;
public Union(A a)
{
this.b = default(B);
this.a = a;
}
}
class Program
{
static void Main(string[] args)
{
A a = new A(5, 10, 0.25, 0.75);
Union union = new Union(a);
B b = union.b; //contains 5,10,0.25,0.75
}
}
精彩评论