Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>As you updated your question, you need Generic logic for converting Type1 to Type2. </p> <p>Try with below solution,</p> <p>For both type classes (Type1 &amp; Type2) I assumed that both have same properties with same name.</p> <p>Lets say we have two class Book &amp; Book1.</p> <pre><code> public class Book { public string Title { get; set; } [XmlAttribute()] public string Publisher { get; set; } [XmlAttribute()] public string Year { get; set; } } public class Book1 { public string Title { get; set; } [XmlAttribute()] public string Publisher { get; set; } [XmlAttribute()] public string Year { get; set; } } </code></pre> <p>ConvertType1ToType2 Function:</p> <pre><code> public static Type2 ConvertType1ToType2&lt;Type1, Type2&gt;(Type1 type1) { using (System.IO.MemoryStream objectStream = new System.IO.MemoryStream()) { XmlRootAttribute root = new XmlRootAttribute("Book"); //Get All MemberInfo of Type1 Type objType1 = type1.GetType(); System.Reflection.MemberInfo[] objType1Member = objType1.GetMembers(); List&lt;Type&gt; extraTypesForType1 = new List&lt;Type&gt;(); //Type1 =&gt; XmlAttributeOverrides XmlAttributeOverrides Type1overrides = new XmlAttributeOverrides(); foreach (System.Reflection.MemberInfo m in objType1Member) { if (m.MemberType.Equals( System.Reflection.MemberTypes.Property)) { XmlAttributes attributes = new XmlAttributes(); attributes.XmlElements.Add(new XmlElementAttribute(m.Name)); Type1overrides.Add(typeof(Type1), m.Name, attributes); extraTypesForType1.Add(m.MemberType.GetType()); } } XmlSerializer type1Serializer = new XmlSerializer(typeof(Type1), Type1overrides, extraTypesForType1.ToArray(),root,""); //Type2 =&gt; XmlAttributeOverrides Type objType2 = type1.GetType(); System.Reflection.MemberInfo[] objType2Member = objType1.GetMembers(); List&lt;Type&gt; extraTypesForType2 = new List&lt;Type&gt;(); XmlAttributeOverrides Type2overrides = new XmlAttributeOverrides(); foreach (System.Reflection.MemberInfo m in objType2Member) { if (m.MemberType.Equals(System.Reflection.MemberTypes.Property)) { XmlAttributes attributes = new XmlAttributes(); attributes.XmlElements.Add(new XmlElementAttribute(m.Name)); Type2overrides.Add(typeof(Type2), m.Name, attributes); extraTypesForType2.Add(m.MemberType.GetType()); } } XmlSerializer type2Deserializer = new XmlSerializer(typeof(Type2), Type2overrides, extraTypesForType2.ToArray(),root,""); type1Serializer.Serialize(objectStream, type1); objectStream.Position = 0; Type2 t = (Type2)type2Deserializer.Deserialize(objectStream); return t; } </code></pre> <p>Now create Book class Object and call function ConvertType1ToType2.</p> <pre><code>Book book = new Book(); book.Title = "Introduction to c#"; book.Publisher = "John Smith"; book.Year = "2012"; Book1 book1 = ConvertType1ToType2&lt;Book, Book1&gt;(book); </code></pre> <p>Hope this solution may work.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload