Click here to Skip to main content
15,905,874 members
Home / Discussions / C#
   

C#

 
QuestionDynamic Enum Creation Pin
MamthaLalith21-Jan-09 3:07
MamthaLalith21-Jan-09 3:07 
AnswerRe: Dynamic Enum Creation Pin
#realJSOP21-Jan-09 3:09
professional#realJSOP21-Jan-09 3:09 
GeneralRe: Dynamic Enum Creation Pin
MamthaLalith21-Jan-09 3:19
MamthaLalith21-Jan-09 3:19 
GeneralRe: Dynamic Enum Creation Pin
Ben Fair21-Jan-09 3:34
Ben Fair21-Jan-09 3:34 
AnswerRe: Dynamic Enum Creation Pin
DaveyM6921-Jan-09 3:26
professionalDaveyM6921-Jan-09 3:26 
AnswerRe: Dynamic Enum Creation Pin
Dave Kreskowiak21-Jan-09 3:31
mveDave Kreskowiak21-Jan-09 3:31 
GeneralRe: Dynamic Enum Creation Pin
MamthaLalith21-Jan-09 3:41
MamthaLalith21-Jan-09 3:41 
GeneralRe: Dynamic Enum Creation Pin
Gideon Engelberth21-Jan-09 3:53
Gideon Engelberth21-Jan-09 3:53 
Dave Kreskowiak wrote:
If you think you have to do this, you really need to rethink your design and use an appropriate collection instead.


I disagree with that statement. I have an utility application that talks to several different tools that all use the same protocol. Rather than having to write custom code for each new tool that may be created, I made a text file format that describes the commands for each tool. Since some of the command parameters are best represented as choices in a ComboBox, dynamic enum creation was the way to go.

To the OP, when making dynamic types you can put spaces and other special characters in the type names, property names, enum names that a standard compiler would not allow, which may prevent the need for the Description attributes on the enum values. Below is the snippet from the utility described above. The prefix was a string like Enum.CommandName.PropertyName and the ParamDef class contained enum type name and the list of enum name/value pairs in the Values property. _module is a ModuleBuilder created earlier to contain all the dynamically created code.

Private Shared flagsCon As ConstructorInfo = GetType(FlagsAttribute).GetConstructor(Type.EmptyTypes)
Private Function CreateEnum(ByVal prefix As String, ByVal param As ParamDef) As Type
    Dim eb As EnumBuilder
    eb = _module.DefineEnum(prefix & param.Name, TypeAttributes.Public, GetType(Byte))
    For Each ev As EnumValue In param.Values
        eb.DefineLiteral(ev.Name, ev.Value)
    Next
    If param.IsFlags Then
        eb.SetCustomAttribute(New CustomAttributeBuilder(flagsCon, New Object() {}))
    End If
    'save the type
    Return eb.CreateType
End Function

GeneralRe: Dynamic Enum Creation Pin
Dave Kreskowiak21-Jan-09 4:45
mveDave Kreskowiak21-Jan-09 4:45 
AnswerRe: Dynamic Enum Creation Pin
PIEBALDconsult21-Jan-09 5:24
mvePIEBALDconsult21-Jan-09 5:24 
GeneralRe: Dynamic Enum Creation Pin
Ennis Ray Lynch, Jr.21-Jan-09 6:07
Ennis Ray Lynch, Jr.21-Jan-09 6:07 
GeneralRe: Dynamic Enum Creation Pin
PIEBALDconsult21-Jan-09 6:58
mvePIEBALDconsult21-Jan-09 6:58 
GeneralRe: Dynamic Enum Creation Pin
Ennis Ray Lynch, Jr.21-Jan-09 7:10
Ennis Ray Lynch, Jr.21-Jan-09 7:10 
GeneralRe: Dynamic Enum Creation Pin
PIEBALDconsult21-Jan-09 7:15
mvePIEBALDconsult21-Jan-09 7:15 
GeneralRe: Dynamic Enum Creation Pin
Ennis Ray Lynch, Jr.21-Jan-09 7:16
Ennis Ray Lynch, Jr.21-Jan-09 7:16 
GeneralRe: Dynamic Enum Creation Pin
PIEBALDconsult21-Jan-09 7:50
mvePIEBALDconsult21-Jan-09 7:50 
QuestionDrawing only one rectangle, when i moving the mouse on a pictureBox Pin
tschmid8521-Jan-09 3:03
tschmid8521-Jan-09 3:03 
AnswerRe: Drawing only one rectangle, when i moving the mouse on a pictureBox Pin
DaveyM6921-Jan-09 3:39
professionalDaveyM6921-Jan-09 3:39 
AnswerRe: Drawing only one rectangle, when i moving the mouse on a pictureBox Pin
musefan21-Jan-09 5:15
musefan21-Jan-09 5:15 
QuestionManaging senesitive data Pin
Dushan12321-Jan-09 2:41
Dushan12321-Jan-09 2:41 
AnswerRe: Managing senesitive data Pin
musefan21-Jan-09 2:52
musefan21-Jan-09 2:52 
AnswerRe: Managing senesitive data Pin
#realJSOP21-Jan-09 3:12
professional#realJSOP21-Jan-09 3:12 
AnswerRe: Managing senesitive data Pin
Ben Fair21-Jan-09 3:15
Ben Fair21-Jan-09 3:15 
QuestionWebBrowser Control C# (VS 2003) Pin
Member 231211721-Jan-09 2:02
Member 231211721-Jan-09 2:02 
AnswerRe: WebBrowser Control C# (VS 2003) Pin
Eddy Vluggen21-Jan-09 3:38
professionalEddy Vluggen21-Jan-09 3:38 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.