views:

173

answers:

5

Hi,

I know its unlikely but I was wondering if there is any way to get the comments (i.e. the bits after the ''') of a class or property..? I have managed to get a list of properties of a class using the PropertyInfo class but I cant find a way to get the comments / description.. I need it for a guide I am writing for the administrators of my site - it would be great if it could automatically update if new properties are added, so there is no need to worry about updating it in the future too much. Anyone know how to do this? Thanks in advance.

Regards,

Richard

+2  A: 

I think you are talking about the XML comments, right?

In that case, there are some third-party tools to turn those into various formats, including compiled help files and full-MSDN-style websites.

Jon Skeet just posted a blog about this last week:
http://msmvps.com/blogs/jon_skeet/archive/2010/04/10/documentation-with-sandcastle-a-notebook.aspx

John Gietzen
I mean the comments where you put "'" 3 times on one line and visual web developer then puts lots of extra lines in the same place such as <summary> or <remarks>.. I think they are xml comments so yeah I think we are talking about the same thing.. Thanks.
ClarkeyBoy
A: 

No, you can't do this using reflection.

What you can do is configure Visual Studio (or whatever build process you use) to create an XML file at build time containing the comments, which you could then distribute.

LukeH
A: 

You can't access normal comments via reflection.
However, if you use xml-comments (I think that's in VB ''') there can be a documentation file created (in my C# project under project properties -> build -> output path/ xml documentation file) which you can access. But you don't need reflection for that.

Here and here are some descriptions for xml comments in VBasic.

tanascius
+1  A: 

Once a class is compiled the doc comments are long gone. If you compile with the /doc switch you will get an XML file with them though that you could use.

Ryan Rinaldi
+2  A: 

You can use property attributes to decorate properties, and use reflection to read the data in the attributes:


[Description('This is my property description')]
public String MyStringProp { get; set; }

Then use

FieldInfo.GetCustomAttributes
to read the description.

CDSO1
hey, yeah this looks like a more viable solution, so long as you can define lots of custom attributes (i.e. Description, Required, Regex for validating that it is in the correct format and that the string is of the correct length and so on).. is this an accepted use for custom attributes??
ClarkeyBoy
If you are using the data just for documentation, then sure. It will bloat your class still, but it does add value. If these are going to be used regularly from code, I would advise against it.Here is the reference for creating attributes. You can create however many you want:http://msdn.microsoft.com/en-us/library/sw480ze8%28VS.80%29.aspx
CDSO1
hmm it doesnt appear to be available in VB, or no example is available (to quote msdn).. I have created a class called DevDescription which takes a description as a string and a boolean value for whether it is required. I have set the custom attributes for this using <DevDescription("Description", True)> _ - I cant find a way to get to it however..
ClarkeyBoy
Think I may have got it - I have it outputting a list of property names and descriptions now! Thanks for the help..
ClarkeyBoy