Hi coders, I am planning to create an enum at runtime and set it as a property of a class. That is,
Class MyClass
Public Property Type As (New Created Enum)
....
End Class
For creating an enum at runtime I have used the following code
Public Function CreateEnum() As \[Enum\]
Dim domain As AppDomain
domain = Threading.Thread.GetDomain
Dim name As New System.Reflection.AssemblyName
name.Name = "EnumCounters"
Dim asmBuilder As System.Reflection.emit.AssemblyBuilder
asmBuilder = domain.DefineDynamicAssembly(name, Reflection.Emit.AssemblyBuilderAccess.Run)
Dim modBuilder As ModuleBuilder
modBuilder = asmBuilder.DefineDynamicModule("EnumModule")
Dim enumBuilder As EnumBuilder
enumBuilder = modBuilder.DefineEnum("EnumCounters", TypeAttributes.Public, GetType(Integer))
'----- HERE IS WHERE I ADD THE ITEMS
For i As Integer = 0 To DiagManager.Counters.Count - 1
enumBuilder.DefineLiteral(CType(DiagManager.Counters(i), Counter).Caption, i)
Next
'-----
Dim enumType As Type = enumBuilder.CreateType
Dim enumObj As [Enum] = System.Activator.CreateInstance(enumType)
Return enumObj
End Function
Then, I set my property's value as the returned value of the function. For full code please see (http://emreyazici.com/CodeFormatter.htm -- site does not have banners or advertisement. I only wrote the code) Normally it would work. But when I assign the 'selectedobject' property of 'property grid' to the created instance of class, it does only display the FIRST item of enum in the property grid http://emreyazici.com/res.jpg But I want it to show all items How may I correct it? Thanks...
Best Regards Emre YAZICI