ibennz

Dynamic shit

Apr 7th, 2014
234
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 2.03 KB | None | 0 0
  1. Imports System.Reflection
  2. Imports System.Reflection.Emit
  3.  
  4. Public Class Form1
  5.  
  6.    
  7.     Function List()
  8.  
  9.         For Each fk In GetType(test).GetFields
  10.  
  11.  
  12.             Dim asmbN As New AssemblyName With {.Name = "tmpASMB"}
  13.             Dim asmb As AssemblyBuilder = AppDomain.CurrentDomain().DefineDynamicAssembly(asmbN, AssemblyBuilderAccess.RunAndSave)
  14.             Dim modA As ModuleBuilder = asmb.DefineDynamicModule("tmpMod", asmbN.Name & ".dll")
  15.             Dim typeA As TypeBuilder = modA.DefineType("tmpType", TypeAttributes.Public And TypeAttributes.Class)
  16.             typeA.DefineDefaultConstructor(MethodAttributes.Public)
  17.             Dim fld As FieldBuilder = typeA.DefineField("tmpProp", fk.FieldType, FieldAttributes.Public)
  18.  
  19.             Dim prop As PropertyBuilder = typeA.DefineProperty("mprop", PropertyAttributes.HasDefault, fk.FieldType, Nothing)
  20.             Dim Attr As MethodAttributes = MethodAttributes.Public Or MethodAttributes.SpecialName Or MethodAttributes.HideBySig
  21.             Dim getBody As MethodBuilder = typeA.DefineMethod("Get", Attr, fk.FieldType, Type.EmptyTypes)
  22.  
  23.             Dim ILGet As ILGenerator = getBody.GetILGenerator()
  24.             ILGet.Emit(OpCodes.Ldarg_0)
  25.             ILGet.Emit(OpCodes.Ldfld, fld)
  26.             ILGet.Emit(OpCodes.Ret)
  27.  
  28.             Dim setBody As MethodBuilder = typeA.DefineMethod("Set", Attr, Nothing, New Type() {fk.FieldType})
  29.  
  30.             Dim ILSet As ILGenerator = setBody.GetILGenerator()
  31.  
  32.             ILSet.Emit(OpCodes.Ldarg_0)
  33.             ILSet.Emit(OpCodes.Ldarg_1)
  34.             ILSet.Emit(OpCodes.Stfld, fld)
  35.             ILSet.Emit(OpCodes.Ret)
  36.  
  37.             prop.SetGetMethod(getBody)
  38.             prop.SetSetMethod(setBody)
  39.  
  40.  
  41.             Dim f As Type = typeA.CreateType()
  42.             asmb.Save(asmbN.Name & ".dll")
  43.         Next
  44.  
  45.         Return 0
  46.     End Function
  47.  
  48.     Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
  49.         List()
  50.     End Sub
  51. End Class
  52.  
  53. Public Class test
  54.     Public clr As Color = Color.Red
  55. End Class
Advertisement
Add Comment
Please, Sign In to add comment