In C#, is there a way to access xml comments (the tags in VS) in the source code at runtime?

StackOverflow https://stackoverflow.com/questions/6334406

  •  27-10-2019
  •  | 
  •  

سؤال

In Visual Studio, xml comments can be added to methods/classes to provide documentation. See http://msdn.microsoft.com/en-us/magazine/cc302121.aspx.

Visual Studio generates files from these xml comments and exports them, so they are accessible for sure.

My question is, is there a class of some sort that let's the program access the xml text at runtime? (I think the brute force way is to parse the exported files, but that's not a neat solution)

The reason I want to do this is because I'm making a kind of a helper program for a larger program, so I want to output the xml comments to screen. I have the property name through reflection, so is there a way to feed the name back in to some class and get the xml documentation?

I tried searching already, but I couldn't find anything. I might be wording it badly. Is this possible?

هل كانت مفيدة؟

المحلول

As far as I now, the XML comments are not saved as metadata in the assembly binary file (.exe or .dll). So the only option is to directly parse the XML file generated by Visual Studio.

نصائح أخرى

XML comments get stripped out of the executable - that's why there is a special compiler flag to generate the XML documentation from them, so they don't get lost.

There are tools that help you generate HTML help and other formats from these XML files.

Try DocsByReflection.

// From type.
var typeDoc = DocsService.GetXmlFromType(typeof(Stub));

// From property.
var propertyInfo = typeof(Stub).GetProperty("PropertyWithDoc");
var propertyDoc = DocsService.GetXmlFromMember(propertyInfo);

// From method.
var methodInfo = typeof(Stub).GetMethod("MethodWithGenericParameter");
var methodDoc = DocsService.GetXmlFromMember(methodInfo);

// From assembly.
var assemblyDoc = DocsService.GetXmlFromAssembly(typeof(Stub).Assembly);
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top