5

I have a method, serializetoxml, that accepts an object of type obj1 as its parameter:

class Program
{
    static void Main(string[] args)
    {
        obj1 myobj = new obj1();
        serializetoxml(myobj);    
    }
}

public class obj1
{
    public string a { get; set; }
    public int b { get; set; }
    public bool c { get; set; }
}

public static void serializetoxml(obj1 myobj)
{
    XmlSerializer myserializer = new XmlSerializer(typeof(obj1));
    TextWriter mywriter = new StreamWriter("C:\\my.xml");
    myserializer.Serialize(mywriter, myobj);
    mywriter.Close();
}

Now I have a class, obj2, that I want to pass as its parameter

public class obj2
{
    public int a { get; set; }
    public bool b { get; set; }
    public List<string> c { get; set; }
}

How do I reuse the serializetoxml method to be able to accept another type of parameter, so that I will not write the same method again and change the typeof to obj2?

obj2 myobj = new obj2();
serializetoxml(myobj);
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Ali Hasan
  • 1,045
  • 4
  • 16
  • 43

6 Answers6

12

Add a type parameter:

public static void serializetoxml<T>(T myobj)
{
    XmlSerializer myserializer = new XmlSerializer(typeof(T));
    TextWriter mywriter = new StreamWriter("C:\\my.xml");
    myserializer.Serialize(mywriter, myobj);
    mywriter.Close();
}

You would then use it like:

serializetoxml<obj2>(myobj);

or

serializetoxml<obj1>(myobj);

With the magic of type parameter inference, you can omit the type parameter entirely (but it's OK if you want to leave it there):

serializetoxml(myobj);
vcsjones
  • 138,677
  • 31
  • 291
  • 286
10

Try the following

public static void serializetoxml(object myobj) {
    XmlSerializer myserializer = new XmlSerializer(myobj.GetType());
    TextWriter mywriter = new StreamWriter("C:\\my.xml");
    myserializer.Serialize(mywriter, myobj);
    mywriter.Close();
}
JaredPar
  • 733,204
  • 149
  • 1,241
  • 1,454
1

Move the method to a static class, and make it an extension method, like this:

internal static class Extensions {
    public static void SerializeToXml<T>(this T myobj)
        {
            XmlSerializer myserializer = new XmlSerializer(typeof(T));
            TextWriter mywriter = new StreamWriter("C:\\my.xml");
            myserializer.Serialize(mywriter, myobj);
            mywriter.Close();
        }
}

You can now use it as if it were an instance method. You need to put your Extensions class into the same namespace, or add a using directive for it.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
1

Two options:

  • generics: Serialize<T>(T obj) and typeof(T)
  • object: Serialize(object obj) and obj.GetType()

I actually prefer the second for serialization, based on bitter bitter experience, but both are valid.

Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
  • Wow, seems very useful. But to someone that is new to C#, means nothing at all. I've been out of the programming loop for a few years now. But from a C Lang and some Java and even PHP. But all seems dead so I gave it up. But I'm programming C# now because the God's of Evil Apple and decided to banish Adobe Flash and thus AS3 is gone... – ejbytes Nov 15 '20 at 20:33
1
public static void serializetoxml(object myobj)
{
    XmlSerializer myserializer = new XmlSerializer(obj1.GetType());
    TextWriter mywriter = new StreamWriter("C:\\my.xml");
    myserializer.Serialize(mywriter, myobj);
    mywriter.Close();
}
Dmitry Nogin
  • 3,670
  • 1
  • 19
  • 35
0

There's really no problem. Just make your serialization method work on a generic type. See Stack Overflow question Using generics with XmlSerializer.

Community
  • 1
  • 1
em70
  • 6,088
  • 6
  • 48
  • 80