Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

reating a dynamic assembly for Form and display Form created through Reflection.Emit Class and AssemblyBuilder Class

4 views
Skip to first unread message

atulmanvar

unread,
Jan 20, 2003, 2:48:43 AM1/20/03
to
Using System.Reflection and System.Reflection.Emit creating a dynamic
assembly for System.Windows.Forms.Form.

The Dynamic Assmebly is Save on Disk as "FormsDll.dll" which contain a
Form To Display.
From FormsDll.dll I want to load a FormNew or to Display a FormNew.

********* Code To Load a Form From FormsDll As Below
*******************************
Dim asm As [Assembly]
Dim ObjType As Type
Dim FormObj As Object
Dim i As Integer = 0
asm = [Assembly].LoadFrom("FormsDll.dll")
ObjType = asm.GetType("FormsDll.FormNew")
FormObj = Activator.CreateInstance(ObjType)
FormObj.Show()
****************************************************************************************

******* Code For Creating FormsDll.dll and FormNew Inside it
*****************************


I Creating a Dynamic Assembly Using "AssemblyBuilder Class".

Code For Generating a FormsDll.Dll and Form inside it as
Follows.................................

Imports System.Reflection
Imports System.Reflection.Emit

'crate a descriptive info for dynamic assembly
Dim asmNew As AssemblyName = New AssemblyName()
asmNew.Name = "FormsDll"
asmNew.Version = New Version("1.1.0.0")

'create the dynamic assembly class in memory
Dim asmBuild As AssemblyBuilder =
CurDomain.DefineDynamicAssembly(asmNew, AssemblyBuilderAccess.Save)

'module under which form class reside
Dim mainModule As ModuleBuilder

mainModule = asmBuild.DefineDynamicModule("FormsDll",
"FormsDll.dll")

'Define a form type to generate using TypeBuilder class

Dim FormNewClass As TypeBuilder
Dim parentType As System.Type
Dim instruGene As ILGenerator

Dim a As Form
a = New System.Windows.Forms.Form()
parentType = a.GetType
FormNewClass = mainModule.DefineType("FormsDll.FormNew",
TypeAttributes.Public, parentType)

'Define a constructor for form
Dim constructor As ConstructorBuilder =
FormNewClass.DefineConstructor(MethodAttributes.Public,
CallingConventions.Standard, Nothing)
' Generate IL for the method. The constructor calls its
superclass constructor.
Dim constructorIL As ILGenerator =
constructor.GetILGenerator()
Dim superConstructor As ConstructorInfo =
GetType(Object).GetConstructor(Type.EmptyTypes)
constructorIL.Emit(OpCodes.Call, superConstructor)
constructorIL.Emit(OpCodes.Ret)

'Define a intializecomponet method required for adding
customization for Form
Dim initcompo As MethodBuilder =
FormNewClass.DefineMethod("InitializeComponent",
MethodAttributes.Public, Nothing, Nothing)
instruGene = initcompo.GetILGenerator()
'instruGene.EmitWriteLine("Me.SuspendLayout()")
'instruGene.EmitWriteLine("Me.Text=" & "Form Caption")
'instruGene.EmitWriteLine("Me.ResumeLayout(False)")
instruGene.Emit(OpCodes.Ret)
FormNewClass.CreateType()
' Save the assembly to disk..
asmBuild.Save("FormsDll.dll")
********************************************************************************************************************************

***** Problem ******

(1) When i displaying a FormNew It Will Just Display a Blank Form ,
Without customization applied in
InitializeComponent For FormNew i.e Setting
FormNew Caption and other FormNew Properties.
So,How to call InitializeComponent Method from constructor.
i.e Public Sub New()
MyBase.New()
InitializeCompoent()
End Sub
(2) When I Invoke InitializeCompoent directly from Code
To Display FormNew it will give error
System.Reflection.TargetInvocationException
ObjType.InvokeMember("InitializeComponent",
BindingFlags.InvokeMethod, Nothing, FormObj, Nothing)

(3) and How to adding Controls(Textbox,Labels...etc.)
in FormNew which is in dynamic assembly.

0 new messages