Question

I want to generate C# code documentation with CodeDOM.

Code without Documentation:

public class MyType {                 
    public static BitmapImage File {
        get { return GetFile("..."); }
    }
}

Code with Documentation:

/// <summary> Gets the File from the location </summary>
public class MyType {                 
    public static BitmapImage File {
        get { return GetFile("..."); }
    }
}

or

/// <summary>
/// Gets the File from the location 
/// </summary>
/// <param name="path"></param>
/// <returns></returns>
public class MyType {                 
    public static BitmapImage File {
        get { return GetFile("..."); }
    }
}

I'm able to generate the Class, the Member, I can Add some attributes and decorators, But how to generate documentation - unfortunately no.

How I generate the members:

private CodeTypeDeclaration CreateType() {
    var classType = new CodeTypeDeclaration("MyType") {
        Attributes = MemberAttributes.Public | MemberAttributes.Static
    };

    var properties = CreateMembers();
    classType.Members.AddRange(properties);

    return classType;
}

private CodeTypeMember[] CreateMembers() {
    var members = _members.Where(x => IsMy(x.Name));
    var props = members.Select(CreateProperty).ToArray();
    return props;
}

private CodeTypeMember CreateProperty(X x) {
    var name = Path.GetFileNameWithoutExtension(x.Name);     

    var property = new CodeMemberProperty {
        Name = name,
        HasGet = true,
        Attributes = MemberAttributes.Public | MemberAttributes.Static,
        Type = new CodeTypeReference(typeof(BitmapImage)),
    };

    var targetObject = new CodeTypeReferenceExpression(typeof(Y));
    var method = Return(new CodeMethodInvokeExpression(targetObject, "Getx", Primitive(x.Name)));
    property.GetStatements.Add(method);
    return property;
}

private bool IsMy(string path) {
    var extension = Path.GetExtension(path.ToLower());
    var isMy = Regex.IsMatch(extension, @"\.(jpg)$");
    return isMy;
}

Edit:
Add Implementation:

private CodeTypeMember CreateProperty(X x) {
    var name = Path.GetFileNameWithoutExtension(x.Name);     

    var property = new CodeMemberProperty {
        Name = name,
        HasGet = true,
        Attributes = MemberAttributes.Public | MemberAttributes.Static,
        Type = new CodeTypeReference(typeof(BitmapImage)),
    };

    var targetObject = new CodeTypeReferenceExpression(typeof(Y));
    var method = Return(new CodeMethodInvokeExpression(targetObject, "Getx", Primitive(x.Name)));
    property.GetStatements.Add(method);

   //because CodeMemberProperty.Comments is readonly I cast it to CodeTypeMember, which has read/write Comments

    var docStart = new CodeCommentStatement("<summary>", true);
    var fileSystemName = string.Format("File system Name: {0}", x.Name);
    var docContent = new CodeCommentStatement(fileSystemName, true);
    var docEnd = new CodeCommentStatement("</summary>", true);

    var result = property as CodeTypeMember;
    result.Comments.AddRange(new CodeCommentStatementCollection {
        docStart,
        docContent,
        docEnd
    });
    return result;
}
Was it helpful?

Solution

CodeTypeDeclaration class1 = new CodeTypeDeclaration("Class1");
class1.Comments.Add(new CodeCommentStatement("<summary>", true));
class1.Comments.Add(new CodeCommentStatement("Create a Hello World application.", true));
class1.Comments.Add(new CodeCommentStatement("</summary>", true));

How to: Create an XML Documentation File Using CodeDOM

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top