CodeDom and Enums

  • Thread starter Thread starter ThisBytes5
  • Start date Start date
T

ThisBytes5

I am trying to generate:

ShortCut.None

ShortCut is an enumeration and None the value I want. I can't seem to
find anything that will allow me to use enums in the codedom. Anyone
have a suggestion?
 
ShortCut is an enumeration and None the value I want. I can't seem to
find anything that will allow me to use enums in the codedom. Anyone
have a suggestion?

If you're creating the type declaration, for instance,

public enum ShortCut
{
None = 0,
// ...
}

You'd conjure up a CodeTypeDeclaration like this in the CodeDOM,

CodeTypeDeclaration tdeclShortCut = new CodeTypeDeclaration( "ShortCut");
tdeclShortCut.IsEnum = true;
tdeclShortCut.Attributes = MemberAttributes.Public;

and then add CodeMemberFields to its CodeTypeMembersCollection with initialization
expressions to set them to explicit values.

OTOH, if you're assigning an instance of this ShortCut type to a variable reference, x,
for instance, then you'd use a CodeAssignStatement like this,

CodeAssignStatement stmt1 = new CodeAssignStatement(
new CodeVariableReferenceExpression( "x"),
new CodeFieldReferenceExpression(
new CodeTypeReferenceExpression( "ShortCut"), // use fully qualified type name if you've created it inside of a
namespace.
"None"
)
);


Derek Harmon
 
Back
Top