Hello,
I have a function like this. Works well, but why?
public static string ToXML<T>(this T obj)
I call it without parameter! I think the reason is this
What is this mechanism called?
How can I see if this exists, perhaps via IntelliSense?
var xmlBody = message.ToXML();
Console.WriteLine("xml:" + xmlBody);
AtnSWO
using System;
using System.Xml.Serialization;
using System.Collections.Generic;
using myTest;
namespace myTest
{
public class Program
{
public static void Main()
{
var invoice = new Invoice(Guid.NewGuid(), 123) { Name = "Jürgen Bauer"};
var body = new Body();
body.AddInvoice(invoice);
var message = new Message() { Body = body };
var xmlBody = message.ToXML();
Console.WriteLine("xml:" + xmlBody);
}
}
[XmlType("invoice")]
public class Invoice
{
public Invoice()
{
this.EventId = Guid.NewGuid();
}
public Invoice(Guid eventId, System.Int64 number)
{
this.EventId = eventId;
this.Number = number;
}
[XmlAttribute("eventid")]
public System.Guid EventId
{
get;
set;
}
[XmlAttribute("number")]
public System.Int64 Number
{
get;
set;
}
[XmlElement("name")]
public string Name
{
get;
set;
}
// Other Code //
}
public static class XMLHelper
{
public static string ToXML<T>(this T obj)
{
using (var stringwriter = new System.IO.StringWriter())
{
var serializer = new System.Xml.Serialization.XmlSerializer(typeof(T));
serializer.Serialize(stringwriter, obj);
return stringwriter.ToString();
}
}
}
[XmlType("message")]
public class Message
{
[XmlElement("body")]
public Body Body {get; set;}
}
public class Body
{
public Body()
{
this.SaveInvoices = new List<Invoice>();
}
[XmlArray("saveInvoice")]
public List<Invoice> SaveInvoices {get; private set;}
public void AddInvoice (Invoice invoice)
{
this.SaveInvoices.Add(invoice);
}
}
}