2007-12-06 Jb Evain <jbevain@novell.com>
[mono.git] / mono / tests / bug-47295.cs
blob4bb18ac0a300905a1d495e0dac142e2633347701
1 //
2 // bug-47295.cs:
3 //
4 // Regression test for bug #47295.
5 //
6 // Test from Marcus Urban (mathpup@mylinuxisp.com)
7 //
9 using System;
10 using System.Reflection;
11 using System.Reflection.Emit;
12 using System.Runtime.InteropServices;
15 public class Testing
17 public static void Method(int value)
19 Console.WriteLine( "Method( {0} )", value );
23 [StructLayout(LayoutKind.Sequential)]
24 internal struct DelegateList
26 internal Delegate del;
30 public static void Main()
32 // Create a dynamic assembly and module to contain the
33 // subclass of MulticastDelegate that we will create
35 AssemblyName asmName = new AssemblyName();
36 asmName.Name = "DynamicAssembly";
38 AssemblyBuilder asmBuilder =
39 AppDomain.CurrentDomain.DefineDynamicAssembly(
40 asmName, AssemblyBuilderAccess.Run );
42 ModuleBuilder modBuilder = asmBuilder.DefineDynamicModule
43 ( "DynamicModule" );
45 TypeBuilder typeBuilder = modBuilder.DefineType( "MyType",
46 TypeAttributes.Public | TypeAttributes.Class | TypeAttributes.Sealed,
47 typeof( System.MulticastDelegate ) );
49 ConstructorBuilder cb = typeBuilder.DefineConstructor(
50 MethodAttributes.Public | MethodAttributes.HideBySig |
51 MethodAttributes.RTSpecialName | MethodAttributes.SpecialName,
52 CallingConventions.Standard,
53 new Type[] { typeof(Object), typeof (IntPtr) } );
55 cb.SetImplementationFlags( MethodImplAttributes.Runtime |
56 MethodImplAttributes.Managed );
58 MethodBuilder mb = typeBuilder.DefineMethod(
59 "Invoke",
60 MethodAttributes.Public | MethodAttributes.Virtual | MethodAttributes.
61 HideBySig,
62 typeof(void),
63 new Type[] { typeof(int) } );
65 mb.SetImplementationFlags( MethodImplAttributes.Runtime |
66 MethodImplAttributes.Managed );
67 ParameterBuilder pb = mb.DefineParameter (1, ParameterAttributes.HasFieldMarshal, "foo");
68 pb.SetMarshal (UnmanagedMarshal.DefineUnmanagedMarshal (UnmanagedType.I2));
70 // Create an instance of the delegate type and invoke it -- just to test
72 Type myDelegateType = typeBuilder.CreateType();
73 Delegate d = Delegate.CreateDelegate( myDelegateType, typeof
74 ( Testing ), "Method" );
75 d.DynamicInvoke( new object[] { 8 } );
77 DelegateList delegateList = new DelegateList();
78 delegateList.del = d;
79 IntPtr ptr = Marshal.AllocHGlobal( Marshal.SizeOf( delegateList ) );
81 // The execption seems to occur at this statement:
82 Marshal.StructureToPtr( delegateList, ptr, false );