开发者

How to access property summary in code?

开发者 https://www.devze.com 2023-01-26 23:01 出处:网络
I have a class, with few properties, where some of the have an XML comment (summary). What I want to do is display the summary info for the user in the application.

I have a class, with few properties, where some of the have an XML comment (summary). What I want to do is display the summary info for the user in the application. So I need to access summary text in code, to be able to do : Label1.Text = ....... how do I do that ?

 public class MyObject
        {
            public int ID { get; set; }
            /// <summary>
            /// very very very very extensive information about the city
            /// </summary>
            public string City { get; set; }
            public DateTime Date { get; set; }
            public int Value { get; set; }
            public int DiffToPrev { get; set; }
        }



 class Program
    开发者_StackOverflow社区{

        static void Main()
        {
            var a = new MyObject();
            var t = a.GetType().GetProperty("City");
            Console.WriteLine(t....................


Note that XML comments are not included in resulting exe/dll file, so you'll need to enable xml file generation and distribute them too. Consder using attributes to provide run-time available information on your properties/methods/classes/etc, as XML comments probably were not designed for what you are trying to do.

Anyway, XML comments have following format:

<?xml version="1.0"?>
<doc>
    <assembly>
        <name><!-- assembly name here --></name>
    </assembly>
    <members>
        <!-- ... -->
        <member name="M:Full.Type.Name.PropertyName">
            <summary> 
            <!-- property summary here -->
            </summary>
        </member>
        <!-- ... -->
    </memebers>
</doc>

So if you still want it, you need to load your XML comments file and find XML node, describing your property (untested code, just to show approach):

var a = new MyObject();
var t = a.GetType().GetProperty("City");
string xmlMemberName = "M:" + a.FullName + t.Name;
var xmlDoc = new XmlDocument();
xmlDoc.Load("you_xml_comments_file.xml");
var membersNode = xmlDoc.DocumentElement["members"];
string summary = "";
if(membersNode != null)
{
    foreach(XmlNode memberNode in membersNode.ChildNodes)
    {
        if(memberNode.Attributes["name"].Value == xmlMemberName)
        {
            summary = memberNode["summary"].InnerText;
            break;
        }
    }
}
Console.WriteLine(summary);

Update: You can also include your XML comments file as an embedded resource so you'll never forget to distribute it, or even write a small tool, which transforms XML comments file into .resx XML resources file.

Incuding XML comments file as an embedded resource:

  1. enable XML file generation in project properties
  2. set XML output file path to project directory (instead of bin/Release or bin/Debug)
  3. compile project
  4. in project explorer enable "show all files" and inlude generated xml file
  5. open file properties and set build action to "Embedded Resource"

Now your xml comments are included in assembly file as resource an can be loaded this way:

XmlDocument doc;
using(var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream(
    "your_default_namespace" + "." + "your_xml_file_name.xml"))
{
    doc.Load(stream);
}

To generate .resx from comments XML file:

.resx format:

<root>
    <!-- some header stuff which can be copy-pasted from any other valid .resx -->
    <!-- ... -->
    <data name="Your_Object_Full_Name_PropertyName" xml:space="preserve">
        <value><!-- summary here --></value>
    </data>
    <!-- ... -->
</root>

Strings can be loaded from this .resx using ResourceManager class.


Although max has provided a very comprehensive answer, I just thought I would add a blog post that I compiled in relation to this question. I offer a solution that uses extension methods on MemberInfo to read the XML comments. My implementation uses XDocument and XPath queries to return the text. It works on methods, properties, types, events, fields and method parameters.

See here: http://www.brad-smith.info/blog/archives/220


You can check a flag in the project options to generate an XML-Documentation.

You can parse the generated file with the usual XML tools.

Depending on the use-case attributes might be a better fit. But in your case it seems to lead to useless redundancy.

But both are a bit problematic with localization. You typically want localizable strings not in the source, but in separate files.


this is not possible because comments do not get compiled


You can't access XML comment informations in your code. As said, these are comments, and are not processed by the compilator.

However they can be used to generate automatic documentation.

If you want to annotate your properties, use Attributes like this one included in the MVC framework

0

精彩评论

暂无评论...
验证码 换一张
取 消