简体   繁体   中英

c# iterate through all class properties and methods for document generation

I am making a MSDN style documentation site for the project I am working on. I wonder if there is a way to iterate through all the classes including their public properties/methods and obtain their summary and other properties for the web page generation. Details below:

for Class I need the Name space + summary + inherited class + class definition syntax + public constructors + public properties + public methods

Example:

namespace foo
{
    /// <summary>
    /// ss
    /// </summary>
    public class bar : ClassA, InterfaceA
    {
        public bar(int a, int b)
        {
        }

        public int Property1 { get;set; }

        public int Method1(int a)
        {
            return 1;
        }
    }
}

namespace = foo
summary = ss
inherited class = ClassA
class definition syntax = public class bar : ClassA, InterfaceA
public constructors = bar(int, int)
public properties = Property1
public Methods = Method1(int)

and for all the methods, properties I would need the type, if it is read only ect.

Long story short, basically i want to generate infos that are in MSDN as much as possible. Any help or thoughts is appreciated.

I have built a function long back ago to iterate through my model classes through class PropertyInfo which Discovers the attributes of a property and provides access to property metadata.

Then I generates for me the required SQL insert query without the headache of manually typing all fields

find code below

public static string GenerateInsertQuery(IModel model)
{
    Type myType = model.GetType();
    string query = "INSERT INTO [" + myType.Name + "] ( ";
    IList<PropertyInfo> props = new List<PropertyInfo>(myType.GetProperties());
    string[] nonDBFields = { "isValid", "isChanged", "CREATEDON", "CHANGEDON", "CHANGEDBY" };

    foreach (PropertyInfo prop in props)
    {
        if (prop.Name == nonDBFields[0] || prop.Name == nonDBFields[1] || prop.Name == nonDBFields[3] || prop.Name == nonDBFields[4])
        {
            continue;
        }
        else
            query += "[" + prop.Name + "] , ";
    }

    query.Remove(query.LastIndexOf(","), 1);
    query += " ) VALUES (";

    foreach (PropertyInfo prop in props)
    {
        if (prop.Name == nonDBFields[0] || prop.Name == nonDBFields[1] || prop.Name == nonDBFields[3] || prop.Name == nonDBFields[4])
        {
            continue;
        }
        else
        {
            if (prop.Name == nonDBFields[2])
            {
                query += "GETDATE(),";
                continue;
            }
            else
            {
                query += " @" + prop.Name + ", ";
                continue;
            }
        }
    }

    query.Remove(query.LastIndexOf(','), 1);
    query += " )";

    return query;
}

There is an open source solution for this. http://jimblackler.net/blog/?p=49

Note: It makes a workaround to your solution's XML documentation output.

From blog:

In Visual Studio, set up your own project that includes XML comments.

(Note that the XML documentation output is not enabled by default in Visual Studio. Go to your project properties, select Build, and under the Output section check the box that says XML Documentation File. It is very important that you do not change the location of the XML file or this method will not be able to locate it at run time.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM