Creating enum at runtime and showing it on a property grid
-
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 ClassFor 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
-
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 ClassFor 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
Why are you creating an Enum at runtime?? Since an Enum is pretty much only used inside your code, I fail to see the point. You might want to check to see if all the enum values are actually being created. It's possible that your only successfully creating one of them.
Dave Kreskowiak Microsoft MVP - Visual Basic
-
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 ClassFor 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
I think the problem is that your property returns a general object. Take this code for example. You'll see that the property works fine when I specifically declare it as my enumerator. However, things don't work as well if I make a simple change of declaring the property as an object. I'm not sure how to accomplish what you want but I do know this is why your having a problem.
'Requires a propertygrid
Public Class Form1
Dim test As New TestEnumPrivate Sub Form1\_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load PropertyGrid1.SelectedObject = test End Sub
End Class
Public Class TestEnum
Dim _TestMyEnum As MyEnum'This works just fine because we have declared the property as MyEnum Public Property TestMyEnum() As MyEnum Get Return \_TestMyEnum End Get Set(ByVal value As MyEnum) \_TestMyEnum = value End Set End Property 'This returns the exact same enumerator but has been declared as a general object 'The same problem occurs here as you are seeing in your code Public Property TestMyEnumObject() As Object Get Return \_TestMyEnum End Get Set(ByVal value As Object) \_TestMyEnum = value End Set End Property Public Enum MyEnum Test0 Test1 Test2 End Enum
End Class