Create Class dynamically at runtime

后端 未结 4 1009
陌清茗
陌清茗 2020-12-30 15:51

I have a method like this: (this is a generic method, and DYNAMIC_CLASS_TYPE will be changed in situation to other situation)

Dim res = f.MyMeth         


        
相关标签:
4条回答
  • 2020-12-30 16:03

    There seems to be a bug. I think this line:

    Dim field As FieldBuilder = myType.DefineField("_" + o.Key, GetType(Integer), FieldAttributes.[Private])
    

    should look like this:

    Dim field As FieldBuilder = myType.DefineField("_" + o.Key, o.Value, FieldAttributes.[Private])
    

    Regards.

    0 讨论(0)
  • 2020-12-30 16:08

    This line:

    Dim field As FieldBuilder = myType.DefineField("_" + o.Key, GetType(Integer), FieldAttributes.[Private])
    

    must to be replaced with this:

    Dim field As FieldBuilder = myType.DefineField("_" + o.Key, o.Value, FieldAttributes.[Private])
    
    0 讨论(0)
  • 2020-12-30 16:10

    A much simpler way is to use the System.Activator:

    Activator.CreateInstance(YourType, ParramArrayOfYourVariablesForNEW)
    

    So say I have a class (Test) that takes a single string in the initialization

    Public Class Test
        Public Sub New(byval MyString as string)
    ...
    

    You could create a new instance like this:

    Activator.CreateInstance(gettype(Test),"My String")
    
    0 讨论(0)
  • 2020-12-30 16:21

    I finally succeeded in doing it. My Code is as:

     Public Shared Function CreateClass(ByVal className As String, ByVal properties As Dictionary(Of String, Type)) As Type
    
        Dim myDomain As AppDomain = AppDomain.CurrentDomain
        Dim myAsmName As New AssemblyName("MyAssembly")
        Dim myAssembly As AssemblyBuilder = myDomain.DefineDynamicAssembly(myAsmName, AssemblyBuilderAccess.Run)
    
        Dim myModule As ModuleBuilder = myAssembly.DefineDynamicModule("MyModule")
    
        Dim myType As TypeBuilder = myModule.DefineType(className, TypeAttributes.Public)
    
        myType.DefineDefaultConstructor(MethodAttributes.Public)
    
        For Each o In properties
    
            Dim prop As PropertyBuilder = myType.DefineProperty(o.Key, PropertyAttributes.HasDefault, o.Value, Nothing)
    
            Dim field As FieldBuilder = myType.DefineField("_" + o.Key, o.Value, FieldAttributes.[Private])
    
            Dim getter As MethodBuilder = myType.DefineMethod("get_" + o.Key, MethodAttributes.[Public] Or MethodAttributes.SpecialName Or MethodAttributes.HideBySig, o.Value, Type.EmptyTypes)
            Dim getterIL As ILGenerator = getter.GetILGenerator()
            getterIL.Emit(OpCodes.Ldarg_0)
            getterIL.Emit(OpCodes.Ldfld, field)
            getterIL.Emit(OpCodes.Ret)
    
            Dim setter As MethodBuilder = myType.DefineMethod("set_" + o.Key, MethodAttributes.[Public] Or MethodAttributes.SpecialName Or MethodAttributes.HideBySig, Nothing, New Type() {o.Value})
            Dim setterIL As ILGenerator = setter.GetILGenerator()
            setterIL.Emit(OpCodes.Ldarg_0)
            setterIL.Emit(OpCodes.Ldarg_1)
            setterIL.Emit(OpCodes.Stfld, field)
            setterIL.Emit(OpCodes.Ret)
    
            prop.SetGetMethod(getter)
            prop.SetSetMethod(setter)
    
        Next
    
        Return myType.CreateType()
    
    End Function
    

    The return value of the function is a Type of my Custom Class.

    0 讨论(0)
提交回复
热议问题