我要添加屬於場C#XmlSerializer的添加屬性到現場
的目標是讓下面的XML定製屬性:
<?xml version="1.0"?>
<doc>
<assembly>
<name>class1</name>
</assembly>
<members>
<member name="P:class1.clsPerson.isAlive">
<Element>
isAlive
</Element>
<Description>
Whether the object is alive or dead
</Description>
<StandardValue>
false
</StandardValue>
</member>
</members>
</doc>
我目前有:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Serialization;
namespace class1
{
public class clsPerson
{
[XmlElement(ElementName="isAlive")]
[Description("Whether the object is alive or dead")]
[StandardValue(false)]
public bool isAlive { get; set; }
}
class Program
{
static void Main(string[] args)
{
clsPerson p = new clsPerson();
p.isAlive = true;
System.Xml.Serialization.XmlSerializer x = new System.Xml.Serialization.XmlSerializer(p.GetType());
x.Serialize(Console.Out, p);
Console.WriteLine();
Console.ReadLine();
}
}
}
我現在的註解類:
using System;
namespace class1
{
internal class StandardValueAttribute : Attribute
{
public readonly object DefaultValue;
public StandardValueAttribute(Object defaultValue)
{
this.DefaultValue = defaultValue;
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace class1
{
internal class DescriptionAttribute : Attribute
{
private string v;
public DescriptionAttribute(string v)
{
this.v = v;
}
}
}
如何將我的自定義屬性(如Description和StandardValue)添加到XMLSerializer?
我真的很想去,但是我有其他限制不容許我用/// featues。 –