gpt4 book ai didi

c# - 使用 Type 类访问 XML 文档

转载 作者:太空宇宙 更新时间:2023-11-03 16:40:15 24 4
gpt4 key购买 nike

有没有一种方法可以从类型类访问 Xml 文档,例如:

/// <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);
}
}
}

关于c# - 使用 Type 类访问 XML 文档,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7804467/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com