Fake enum type

Topics: User Forum
Aug 6, 2014 at 3:30 PM
We have a proprietary tool that generates constants in C# source code, somewhat like this (comments omitted):
public static class FinancialConceptId
{
    public const int Loan = 2284;
    public const int Account = 2947;
    public const int TaxRate = 3502;
}
When I run Sandcastle Help File Builder 1.9.8.0 to generate a VS2005-style web site from that, it emits a page for the class itself, a page for the list of members, and a page for each member. However, none of those pages shows the values of the constants, which is unfortunate. Also, the per-member pages are not useful in practice, because the constants have no documentation beyond the summary that already appears in the list of members. The documentation build takes quite a long time and reducing the number of pages would perhaps speed it up.

If we changed the proprietary tool to generate this instead:
public enum FinancialConceptId
{
    Loan = 2284,
    Account = 2947,
    TaxRate = 3502,
}
then Sandcastle would generate only a page for the enum type itself and omit the member pages, and it would also show the values of the constants in the list of members. This would be better overall. However, due to the way these constants are used, we'd then have to add many casts, as in (int)FinancialConceptId.Loan.

Is it possible to keep the FinancialConceptId type as a class but somehow configure Sandcastle to treat it as an enum? I assume that Sandcastle has no such feature yet and that other users would not be interested in it, so I'm thinking of perhaps implementing it as a custom build step. It would search for classes tagged with a specific attribute and transform the reflection file as if those classes had been enum types in the first place. Does that seem reasonable?
Coordinator
Aug 6, 2014 at 8:05 PM
A plug-in that runs after the GenerateReflectionInfo build step will work. However, MRefBuilder will need updating since it doesn't currently write out the value for literal fields. That can be done by updating the NodeType.Field case in the WriteMember() method in MRefWriter.cs as follows:
case NodeType.Field:
    Field field = (Field)member;

    this.WriteFieldData(field);
    this.WriteReturnValue(field.Type);

    // Write enumeration and literal (constant) field values
    if(field.DeclaringType.NodeType == NodeType.EnumNode)
    {
        this.WriteLiteral(new Literal(field.DefaultValue.Value,
            ((EnumNode)field.DeclaringType).UnderlyingType), false);
    }
    else
        if(field.IsLiteral)
            this.WriteLiteral(new Literal(field.DefaultValue.Value, field.Type), false);
    break;
I'll make that change for the next release.

Eric