-
Notifications
You must be signed in to change notification settings - Fork 3.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
2010-07-12 Rodrigo Kumpera <rkumpera@novell.com>
* bug-389886-3.cs: New regression test. svn path=/trunk/mono/; revision=160259
- Loading branch information
Showing
3 changed files
with
116 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,110 @@ | |||
using System; | |||
using System.Threading; | |||
using System.Reflection; | |||
using System.Reflection.Emit; | |||
|
|||
namespace TestApp | |||
{ | |||
class Program | |||
{ | |||
static AssemblyBuilder assembly; | |||
static ModuleBuilder module; | |||
static Type[] args; | |||
static ConstructorBuilder delegate_ctor; | |||
static MethodBuilder invoke_mb; | |||
static string ASSEMBLY_NAME = "MonoTests.System.Reflection.Emit.TypeBuilderTest"; | |||
|
|||
static void SetUp () | |||
{ | |||
AssemblyName assemblyName = new AssemblyName (); | |||
assemblyName.Name = ASSEMBLY_NAME; | |||
|
|||
assembly = | |||
Thread.GetDomain ().DefineDynamicAssembly ( | |||
assemblyName, AssemblyBuilderAccess.RunAndSave, "."); | |||
|
|||
module = assembly.DefineDynamicModule ("module1", "bla.exe"); | |||
} | |||
|
|||
static TypeBuilder DefineDelegate () { | |||
TypeBuilder typeBuilder = module.DefineType( "MyDelegate", | |||
TypeAttributes.Public | TypeAttributes.Class | TypeAttributes.Sealed, | |||
typeof (object) ); | |||
args = typeBuilder.DefineGenericParameters ("TIn", "TOut"); | |||
|
|||
delegate_ctor = typeBuilder.DefineConstructor( | |||
MethodAttributes.Public | MethodAttributes.HideBySig | | |||
MethodAttributes.RTSpecialName | MethodAttributes.SpecialName, | |||
CallingConventions.Standard, | |||
new Type[] { typeof(Object), typeof (IntPtr) } ); | |||
|
|||
delegate_ctor.SetImplementationFlags( MethodImplAttributes.Runtime | MethodImplAttributes.Managed ); | |||
|
|||
invoke_mb = typeBuilder.DefineMethod( | |||
"Invoke", | |||
MethodAttributes.Public | MethodAttributes.Virtual | MethodAttributes.HideBySig, | |||
args [1], | |||
new Type[] { args [0] } ); | |||
|
|||
invoke_mb.SetImplementationFlags( MethodImplAttributes.Runtime | MethodImplAttributes.Managed ); | |||
|
|||
MethodBuilder mb = typeBuilder.DefineMethod( | |||
"BeginInvoke", | |||
MethodAttributes.Public | MethodAttributes.Virtual | MethodAttributes.HideBySig, | |||
typeof (IAsyncResult), | |||
new Type[] { args [0], typeof (AsyncCallback), typeof (object) } ); | |||
|
|||
mb.SetImplementationFlags( MethodImplAttributes.Runtime | MethodImplAttributes.Managed ); | |||
|
|||
mb = typeBuilder.DefineMethod( | |||
"EndInvoke", | |||
MethodAttributes.Public | MethodAttributes.Virtual | MethodAttributes.HideBySig, | |||
args [1], | |||
new Type[] { typeof (IAsyncResult) } ); | |||
|
|||
mb.SetImplementationFlags( MethodImplAttributes.Runtime | MethodImplAttributes.Managed ); | |||
|
|||
return typeBuilder; | |||
} | |||
|
|||
static int Main() | |||
{ | |||
SetUp (); | |||
|
|||
TypeBuilder tb = DefineDelegate (); | |||
TypeBuilder main = module.DefineType ("Main", TypeAttributes.Public); | |||
/* >>>move this to after the SetParent call and things will work<<< */ | |||
Type inst = tb.MakeGenericType (new Type[] {typeof (double), typeof(int)}); | |||
|
|||
tb.SetParent (typeof( System.MulticastDelegate)); | |||
|
|||
ConstructorBuilder ctor = main.DefineDefaultConstructor (MethodAttributes.Public); | |||
|
|||
MethodBuilder foo_mb = main.DefineMethod("Foo", MethodAttributes.Public, typeof (int), new Type[] { typeof (double) } ); | |||
ILGenerator ig = foo_mb.GetILGenerator (); | |||
ig.Emit (OpCodes.Ldc_I4_0); | |||
ig.Emit (OpCodes.Ret); | |||
|
|||
|
|||
MethodBuilder main_mb = main.DefineMethod ("Main", MethodAttributes.Public | MethodAttributes.Static, typeof (int), Type.EmptyTypes); | |||
ig = main_mb.GetILGenerator (); | |||
|
|||
ig.Emit (OpCodes.Newobj, ctor); | |||
ig.Emit (OpCodes.Ldftn, foo_mb); | |||
ig.Emit (OpCodes.Newobj, TypeBuilder.GetConstructor (inst, delegate_ctor)); | |||
ig.Emit (OpCodes.Ldc_R8, 2.2); | |||
ig.Emit (OpCodes.Callvirt, TypeBuilder.GetMethod (inst, invoke_mb)); | |||
|
|||
ig.Emit (OpCodes.Ret); | |||
|
|||
tb.CreateType (); | |||
|
|||
Type t = main.CreateType (); | |||
|
|||
MethodInfo method = t.GetMethod ("Main"); | |||
method.Invoke (null, null); | |||
|
|||
return 0; | |||
} | |||
} | |||
} |