Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. Visual Basic
  4. How to Create typeconverter dynamically by reflection to use in a PropertyGrid?

How to Create typeconverter dynamically by reflection to use in a PropertyGrid?

Scheduled Pinned Locked Moved Visual Basic
helpdatabasetutorialquestion
4 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Mohamed Ali Jinnah
    wrote on last edited by
    #1

    Hi, I am using the propertygrid control to get the application level parameters from the users. The object assigned to the propertygrid is created dynamically with help of reflection.emit. i achieved the functionalities of the propertygrid like Categories and Description of a property by using the SetCustomAttribute method of a property builder. I want to achieve the combo box functionality for a property (User can select a value from the combobox for the property). I know that we can achieve this by setting the TypeConverterAttribute for the property. My problem is, each property will have to show different values in the combo(Values are coming from a Database), so dynamically i have to create a TypeConverter which is inherited from StringConverter. This typeconverter should be assingned to the property by TypeConverterAttribute. My problem is how we have to create a TypeConverter which is inherited from StringConverter with help of Reflection.Emit. Please give you suggestions. Please find the code below. It is not working. I confused to write the reflection related codes. Please help me on this. Public Shared Function GetDynamicTypeConverted(ByRef foModuleBuilder As ModuleBuilder) As Type Dim oTypeBuilder As TypeBuilder = Nothing Dim oMethodBuilder As MethodBuilder = Nothing Dim oILGenerator As ILGenerator = Nothing Dim oConstructorInfo As ConstructorInfo = Nothing Dim oParams() As Type Try oTypeBuilder = foModuleBuilder.DefineType("EngStatusConverter", TypeAttributes.Class, GetType(StringConverter)) oMethodBuilder = oTypeBuilder.DefineMethod("GetStandardValuesSupported", MethodAttributes.Public, GetType(Boolean), New Type() {GetType(ITypeDescriptorContext)}) oILGenerator = oMethodBuilder.GetILGenerator oILGenerator.Emit(OpCodes.Ldarg_0, CType(True, Object)) oILGenerator.Emit(OpCodes.Ret) oMethodBuilder = oTypeBuilder.DefineMethod("GetStandardValues", MethodAttributes.Public, GetType(StandardValuesCollection), New Type() {GetType(ITypeDescriptorContext)}) oILGenerator = oMethodBuilder.GetILGenerator oParams = New Type() {GetType(String)} oConstructorInfo = GetType(StandardValuesCollection).GetConstructor(oParams) oILGenerator.Emit(OpCodes.Ldarg_0, oConstructorInfo) oILGenerator.Emit(OpCodes.Ret) oMet

    J 1 Reply Last reply
    0
    • M Mohamed Ali Jinnah

      Hi, I am using the propertygrid control to get the application level parameters from the users. The object assigned to the propertygrid is created dynamically with help of reflection.emit. i achieved the functionalities of the propertygrid like Categories and Description of a property by using the SetCustomAttribute method of a property builder. I want to achieve the combo box functionality for a property (User can select a value from the combobox for the property). I know that we can achieve this by setting the TypeConverterAttribute for the property. My problem is, each property will have to show different values in the combo(Values are coming from a Database), so dynamically i have to create a TypeConverter which is inherited from StringConverter. This typeconverter should be assingned to the property by TypeConverterAttribute. My problem is how we have to create a TypeConverter which is inherited from StringConverter with help of Reflection.Emit. Please give you suggestions. Please find the code below. It is not working. I confused to write the reflection related codes. Please help me on this. Public Shared Function GetDynamicTypeConverted(ByRef foModuleBuilder As ModuleBuilder) As Type Dim oTypeBuilder As TypeBuilder = Nothing Dim oMethodBuilder As MethodBuilder = Nothing Dim oILGenerator As ILGenerator = Nothing Dim oConstructorInfo As ConstructorInfo = Nothing Dim oParams() As Type Try oTypeBuilder = foModuleBuilder.DefineType("EngStatusConverter", TypeAttributes.Class, GetType(StringConverter)) oMethodBuilder = oTypeBuilder.DefineMethod("GetStandardValuesSupported", MethodAttributes.Public, GetType(Boolean), New Type() {GetType(ITypeDescriptorContext)}) oILGenerator = oMethodBuilder.GetILGenerator oILGenerator.Emit(OpCodes.Ldarg_0, CType(True, Object)) oILGenerator.Emit(OpCodes.Ret) oMethodBuilder = oTypeBuilder.DefineMethod("GetStandardValues", MethodAttributes.Public, GetType(StandardValuesCollection), New Type() {GetType(ITypeDescriptorContext)}) oILGenerator = oMethodBuilder.GetILGenerator oParams = New Type() {GetType(String)} oConstructorInfo = GetType(StandardValuesCollection).GetConstructor(oParams) oILGenerator.Emit(OpCodes.Ldarg_0, oConstructorInfo) oILGenerator.Emit(OpCodes.Ret) oMet

      J Offline
      J Offline
      J4amieC
      wrote on last edited by
      #2

      Im not sure i totally understand your problem, but TypeConverter's are created by a call to TypeDescriptor.GetConverter[^]

      M 1 Reply Last reply
      0
      • J J4amieC

        Im not sure i totally understand your problem, but TypeConverter's are created by a call to TypeDescriptor.GetConverter[^]

        M Offline
        M Offline
        Mohamed Ali Jinnah
        wrote on last edited by
        #3

        Hi, Thanks for your reply. I have to attach a TypeConverterAttribute to a property. Please see the following code. Property definition ------------------------------------ _ Public Property DefaultFileName() As String Get Return _defaultFileName End Get Set(ByVal Value As String) _defaultFileName = Value End Set End Property ------------------------------------ File Name Converter ------------------------------------ Public Class FileNameConverter Inherits StringConverter Public Overloads Overrides Function GetStandardValuesSupported( _ ByVal context As ITypeDescriptorContext) As Boolean Return True End Function Public Overloads Overrides Function GetStandardValues( _ ByVal context As ITypeDescriptorContext) _ As StandardValuesCollection Return New StandardValuesCollection(New String() {"New File", _ "File1", _ "Document1"}) End Function Public Overloads Overrides Function GetStandardValuesExclusive( _ ByVal context As ITypeDescriptorContext) As Boolean Return False End Function End Class My problem is, I have to create the FileNameConverter type dynamically with help of reflection. So I can return the dynamic values from GetStandardValues method. I need a way to create a type like filenameconverter thru reflection. Please give your suggesstion.

        D 1 Reply Last reply
        0
        • M Mohamed Ali Jinnah

          Hi, Thanks for your reply. I have to attach a TypeConverterAttribute to a property. Please see the following code. Property definition ------------------------------------ _ Public Property DefaultFileName() As String Get Return _defaultFileName End Get Set(ByVal Value As String) _defaultFileName = Value End Set End Property ------------------------------------ File Name Converter ------------------------------------ Public Class FileNameConverter Inherits StringConverter Public Overloads Overrides Function GetStandardValuesSupported( _ ByVal context As ITypeDescriptorContext) As Boolean Return True End Function Public Overloads Overrides Function GetStandardValues( _ ByVal context As ITypeDescriptorContext) _ As StandardValuesCollection Return New StandardValuesCollection(New String() {"New File", _ "File1", _ "Document1"}) End Function Public Overloads Overrides Function GetStandardValuesExclusive( _ ByVal context As ITypeDescriptorContext) As Boolean Return False End Function End Class My problem is, I have to create the FileNameConverter type dynamically with help of reflection. So I can return the dynamic values from GetStandardValues method. I need a way to create a type like filenameconverter thru reflection. Please give your suggesstion.

          D Offline
          D Offline
          Danilo Corallo
          wrote on last edited by
          #4

          Hi, the answer to your need, is use an Attribute to pass the dynamic list to your TypeConverter. Please check my recent article here: http://www.codeproject.com/useritems/PropertyGridEx.asp Here is my TypeConverter ;) :

          Public Class CustomChoices
          Inherits ArrayList

          Public Sub New(ByVal array As ArrayList, Optional ByVal IsSorted As Boolean = False)
              Me.AddRange(array)
              If IsSorted Then Me.Sort()
          End Sub
          
          Public Sub New(ByVal array() As String, Optional ByVal IsSorted As Boolean = False)
              Me.AddRange(array)
              If IsSorted Then Me.Sort()
          End Sub
          
          Public Sub New(ByVal array() As Integer, Optional ByVal IsSorted As Boolean = False)
              Me.AddRange(array)
              If IsSorted Then Me.Sort()
          End Sub
          
          Public Sub New(ByVal array() As Double, Optional ByVal IsSorted As Boolean = False)
              Me.AddRange(array)
              If IsSorted Then Me.Sort()
          End Sub
          
          Public Sub New(ByVal array() As Object, Optional ByVal IsSorted As Boolean = False)
              Me.AddRange(array)
              If IsSorted Then Me.Sort()
          End Sub
          
          Public ReadOnly Property Items() As ArrayList
              Get
                  Return Me
              End Get
          End Property
          
          Public Class CustomChoicesTypeConverter
              Inherits TypeConverter
              Private oChoices As CustomChoicesAttributeList = Nothing
              Public Overrides Function GetStandardValuesSupported(ByVal context As System.ComponentModel.ITypeDescriptorContext) As Boolean
                  Dim Choices As CustomChoicesAttributeList = context.PropertyDescriptor.Attributes(GetType(CustomChoicesAttributeList))
          
                  If Not Choices Is Nothing Then
                      oChoices = Choices
                      GetStandardValuesSupported = True
                  Else
                      GetStandardValuesSupported = False
                  End If
              End Function
              Public Overrides Function GetStandardValuesExclusive(ByVal context As System.ComponentModel.ITypeDescriptorContext) As Boolean
                  Dim Choices As CustomChoicesAttributeList = context.PropertyDescriptor.Attributes(GetType(CustomChoicesAttributeList))
          
                  If Not Choices Is Nothing Then
                      oChoices = Choices
                      GetStandardValuesExclusive = True
                  Else
                      GetStandardValuesExclusive = False
                  End If
              End Function
              Public Overrides Function GetStandardValues(ByVal context As System.ComponentModel.ITypeDescriptorContext) As System.
          
          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups