Converting values between same structure within different namespace. C#

前端 未结 7 1556
情书的邮戳
情书的邮戳 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: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 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;
        }
    

提交回复
热议问题