Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>(edit - I misread the original question)</p> <p>You cannot add actual attributes (they are burned into the IL); however, with <code>XmlSerializer</code> you don't have to - you can supply additional attributes in the constructor to the <code>XmlSerializer</code>. You do, however, need to be a little careful to cache the <code>XmlSerializer</code> instance if you do this, as otherwise it will create an additional assembly per instance, which is a bit leaky. (it doesn't do this if you use the simple constructor that just takes a <code>Type</code>). Look at <a href="http://msdn.microsoft.com/en-us/library/system.xml.serialization.xmlattributeoverrides.aspx" rel="noreferrer"><code>XmlAttributeOverrides</code></a>.</p> <p>For an example:</p> <pre><code>using System; using System.Xml.Serialization; public class Person { static void Main() { XmlAttributeOverrides overrides = new XmlAttributeOverrides(); XmlAttributes attribs = new XmlAttributes(); attribs.XmlIgnore = false; attribs.XmlElements.Add(new XmlElementAttribute("personName")); overrides.Add(typeof(Person), "Name", attribs); XmlSerializer ser = new XmlSerializer(typeof(Person), overrides); Person person = new Person(); person.Name = "Marc"; ser.Serialize(Console.Out, person); } private string name; [XmlElement("name")] [XmlIgnore] public string Name { get { return name; } set { name = value; } } } </code></pre> <p>Note also; if the xml attributes were just illustrative, then there is a <em>second</em> way to add attributes for things related to data-binding, by using <code>TypeDescriptor.CreateProperty</code> and either <code>ICustomTypeDescriptor</code> or <code>TypeDescriptionProvider</code>. Much more complex than the xml case, I'm afraid - and doesn't work for all code - just code that uses the component-model.</p>
 

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