Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Imports System.Reflection
- Imports System.Reflection.Emit
- Public Class Form1
- Function List()
- For Each fk In GetType(test).GetFields
- Dim asmbN As New AssemblyName With {.Name = "tmpASMB"}
- Dim asmb As AssemblyBuilder = AppDomain.CurrentDomain().DefineDynamicAssembly(asmbN, AssemblyBuilderAccess.RunAndSave)
- Dim modA As ModuleBuilder = asmb.DefineDynamicModule("tmpMod", asmbN.Name & ".dll")
- Dim typeA As TypeBuilder = modA.DefineType("tmpType", TypeAttributes.Public And TypeAttributes.Class)
- typeA.DefineDefaultConstructor(MethodAttributes.Public)
- Dim fld As FieldBuilder = typeA.DefineField("tmpProp", fk.FieldType, FieldAttributes.Public)
- Dim prop As PropertyBuilder = typeA.DefineProperty("mprop", PropertyAttributes.HasDefault, fk.FieldType, Nothing)
- Dim Attr As MethodAttributes = MethodAttributes.Public Or MethodAttributes.SpecialName Or MethodAttributes.HideBySig
- Dim getBody As MethodBuilder = typeA.DefineMethod("Get", Attr, fk.FieldType, Type.EmptyTypes)
- Dim ILGet As ILGenerator = getBody.GetILGenerator()
- ILGet.Emit(OpCodes.Ldarg_0)
- ILGet.Emit(OpCodes.Ldfld, fld)
- ILGet.Emit(OpCodes.Ret)
- Dim setBody As MethodBuilder = typeA.DefineMethod("Set", Attr, Nothing, New Type() {fk.FieldType})
- Dim ILSet As ILGenerator = setBody.GetILGenerator()
- ILSet.Emit(OpCodes.Ldarg_0)
- ILSet.Emit(OpCodes.Ldarg_1)
- ILSet.Emit(OpCodes.Stfld, fld)
- ILSet.Emit(OpCodes.Ret)
- prop.SetGetMethod(getBody)
- prop.SetSetMethod(setBody)
- Dim f As Type = typeA.CreateType()
- asmb.Save(asmbN.Name & ".dll")
- Next
- Return 0
- End Function
- Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
- List()
- End Sub
- End Class
- Public Class test
- Public clr As Color = Color.Red
- End Class
Advertisement
Add Comment
Please, Sign In to add comment