我想添加属于字段的自定义属性
目标是获取以下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?
答案 0 :(得分:2)
看起来你想重新发明轮子。如果您尝试导出代码文档,我建议您使用内置功能:
https://msdn.microsoft.com/en-us/library/b2s063f7.aspx
然后生成XML文档文件,您甚至可以在intellisense中使用它们
XMLSerializer将只存储实例的内容。
答案 1 :(得分:1)
使用xml Linq
{{1}}