2
XML文檔有沒有一種方法,我可以從類型類訪問XML文檔,如:訪問使用Type類
/// <summary>
/// Blah blah blah
/// </summary>
public class Foo{}
Debug.Print(typeOf(Foo).XmlSummary);
會導致Blah blah blah
。
XML文檔有沒有一種方法,我可以從類型類訪問XML文檔,如:訪問使用Type類
/// <summary>
/// Blah blah blah
/// </summary>
public class Foo{}
Debug.Print(typeOf(Foo).XmlSummary);
會導致Blah blah blah
。
不,xml註釋由VS和其他工具解析以生成文檔,但與常規註釋一樣,不包含在生成的程序集中,因此類型的元數據不知道它們。
如果你需要爲你的類做到這一點,你可以使用自定義屬性:
using System;
class SummaryAttribute : Attribute {
public string Value {
get;
private set;
}
public SummaryAttribute(string value) {
Value = value;
}
}
[Summary("Blah")]
class Foo {
}
class Program {
static void Main(string[] args) {
var summaryAttributes = typeof(Foo).GetCustomAttributes(typeof(SummaryAttribute), false);
if (summaryAttributes.Length != 0) {
SummaryAttribute summary = (SummaryAttribute)summaryAttributes[0];
Console.WriteLine(summary.Value);
}
}
}
我有一種感覺,這是事實。應該認識到他們像正常的評論一樣對待。謝謝!! –