3

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;
}
mihai
  • 2,746
  • 3
  • 35
  • 56

1 Answers1

6
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

heringer
  • 2,698
  • 1
  • 20
  • 33
  • mm, this is only possible for `CodeTypeDeclaration` (the class), but NOT for `CodeMemberProperty` (the property/method)... :( – mihai Apr 25 '14 at 12:55
  • may you use CodeTypeMember.Comments Property? [link](http://msdn.microsoft.com/en-us/library/vstudio/system.codedom.codetypemember.comments%28v=vs.90%29) – heringer Apr 25 '14 at 16:41
  • 1
    Thanks. Based on your idea I found the following solution: Cast the `CodeMemberProperty` to it's base `CodeTypeMember`. Add Comments to this base `CodeTypeMember` and use it :) – mihai Apr 29 '14 at 05:58