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