Converting values between same structure within different namespace. C#

前端 未结 7 1424
情书的邮戳
情书的邮戳 2021-01-13 17:28

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 getti

相关标签:
7条回答
  • 2021-01-13 17:32

    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.

    0 讨论(0)
  • 2021-01-13 17:32

    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
        }
    }
    
    0 讨论(0)
  • 2021-01-13 17:46

    Use AutoMapper.

    Mapper.CreateMap<My.NS1.Structure, My.NS2.Structure>();
    My.NS1.Structure struct1;
    My.NS2.Structure struct2 = (My.NS2.Structure) Mapper.Map(struct1);
    
    0 讨论(0)
  • 2021-01-13 17:48

    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.

    0 讨论(0)
  • 2021-01-13 17:49

    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

    0 讨论(0)
  • 2021-01-13 17:50

    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;
        }
    
    0 讨论(0)
提交回复
热议问题