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 a class inherited from StringConverter by Reflection.Emit (Dynamically)

How to create a class inherited from StringConverter by Reflection.Emit (Dynamically)

Scheduled Pinned Locked Moved Visual Basic
helptutorial
1 Posts 1 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 all, Please can you help me on this. I want to create a class dynamically which is inherited from StringConverter with help of Reflection.Emit. Class --------------------------------- Public Class EngStatusConverter 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() {"RUNNING", "IDLE"}) 'Return New StandardValuesCollection(sStdValues) End Function Public Overloads Overrides Function GetStandardValuesExclusive(ByVal context As ITypeDescriptorContext) As Boolean Return True End Function End Class Using Reflection.Emit ---------------------------- 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) oMethodBuilder = oTypeBuilder.DefineMethod("GetStandardValuesExclusive", MethodAttributes.Public, GetType(Boolean), New Type() {GetType(ITypeDescriptorContext)}) oILGenerator = oMethodBuilder.GetILGenerator oILGenerator.Emit(OpCodes.Ldarg_0

    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