Merge pull request #5406 from kumpera/fix_12157
[mono.git] / mono / tests / bug-Xamarin-5278.cs
1 //
2 // bug-Xamarin-5278.cs
3 //
4 //  Tests for System.Reflection.Binder class that require an unmanaged COM object
5 //  (Xamarin bug 5278)
6 //
7 using System;
8 using System.Reflection;
9 using System.Runtime.CompilerServices;
10 using System.Runtime.InteropServices;
11
12 public class Tests
13 {
14         [DllImport ("libtest")]
15         public static extern int mono_test_marshal_com_object_create (out IntPtr pUnk);
16
17         [DllImport("libtest")]
18         public static extern bool mono_cominterop_is_supported ();
19
20         #region Definition of COM object
21         [ComImport ()]
22         [Guid ("00000000-0000-0000-0000-000000000001")]
23         [InterfaceType (ComInterfaceType.InterfaceIsIUnknown)]
24         public interface ITest
25         {
26                 // properties need to go first since mcs puts them there
27                 ITest Test {
28                         [return: MarshalAs (UnmanagedType.Interface)]
29                         [MethodImpl (MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime), DispId (5242884)]
30                         get;
31                 }
32
33                 [MethodImplAttribute (MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)]
34                 void SByteIn (sbyte val);
35
36                 [MethodImplAttribute (MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)]
37                 void ByteIn (byte val);
38
39                 [MethodImplAttribute (MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)]
40                 void ShortIn (short val);
41
42                 [MethodImplAttribute (MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)]
43                 void UShortIn (ushort val);
44
45                 [MethodImplAttribute (MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)]
46                 void IntIn (int val);
47
48                 [MethodImplAttribute (MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)]
49                 void UIntIn (uint val);
50
51                 [MethodImplAttribute (MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)]
52                 void LongIn (long val);
53
54                 [MethodImplAttribute (MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)]
55                 void ULongIn (ulong val);
56
57                 [MethodImplAttribute (MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)]
58                 void FloatIn (float val);
59
60                 [MethodImplAttribute (MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)]
61                 void DoubleIn (double val);
62
63                 [MethodImplAttribute (MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)]
64                 void ITestIn ([MarshalAs (UnmanagedType.Interface)]ITest val);
65
66                 [MethodImplAttribute (MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)]
67                 void ITestOut ([MarshalAs (UnmanagedType.Interface)]out ITest val);
68         }
69
70         [System.Runtime.InteropServices.GuidAttribute ("00000000-0000-0000-0000-000000000002")]
71         [System.Runtime.InteropServices.ComImportAttribute ()]
72         [System.Runtime.InteropServices.ClassInterfaceAttribute (ClassInterfaceType.None)]
73         public class _TestClass : ITest
74         {
75                 // properties need to go first since mcs puts them there
76                 public virtual extern ITest Test {
77                         [return: MarshalAs (UnmanagedType.Interface)]
78                         [MethodImpl (MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime), DispId (5242884)]
79                         get;
80                 }
81
82                 [MethodImplAttribute (MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)]
83                 public virtual extern void SByteIn (sbyte val);
84
85                 [MethodImplAttribute (MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)]
86                 public virtual extern void ByteIn (byte val);
87
88                 [MethodImplAttribute (MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)]
89                 public virtual extern void ShortIn (short val);
90
91                 [MethodImplAttribute (MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)]
92                 public virtual extern void UShortIn (ushort val);
93
94                 [MethodImplAttribute (MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)]
95                 public virtual extern void IntIn (int val);
96
97                 [MethodImplAttribute (MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)]
98                 public virtual extern void UIntIn (uint val);
99
100                 [MethodImplAttribute (MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)]
101                 public virtual extern void LongIn (long val);
102
103                 [MethodImplAttribute (MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)]
104                 public virtual extern void ULongIn (ulong val);
105
106                 [MethodImplAttribute (MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)]
107                 public virtual extern void FloatIn (float val);
108
109                 [MethodImplAttribute (MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)]
110                 public virtual extern void DoubleIn (double val);
111
112                 [MethodImplAttribute (MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)]
113                 public virtual extern void ITestIn ([MarshalAs (UnmanagedType.Interface)]ITest val);
114
115                 [MethodImplAttribute (MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)]
116                 public virtual extern void ITestOut ([MarshalAs (UnmanagedType.Interface)]out ITest val);
117         }
118
119         [System.Runtime.InteropServices.GuidAttribute ("00000000-0000-0000-0000-000000000002")]
120         public class TestClass : _TestClass
121         {
122                 static TestClass ()
123                 {
124                         ExtensibleClassFactory.RegisterObjectCreationCallback (new ObjectCreationDelegate (CreateObject));
125                         ;
126                 }
127
128                 private static System.IntPtr CreateObject (System.IntPtr aggr)
129                 {
130                         IntPtr pUnk3;
131                         mono_test_marshal_com_object_create (out pUnk3);
132                         return pUnk3;
133                 }
134         }
135         #endregion
136
137         public class Foo
138         {
139                 public Foo (ITest test)
140                 {
141                 }
142         }
143
144         public static bool CreateInstanceWithComObjectParameter ()
145         {
146                 try {
147                         var testObj = new TestClass ();
148                         var comObject = testObj.Test;
149                         var assembly = Assembly.GetExecutingAssembly ();
150                         var foo = assembly.CreateInstance (typeof(Foo).FullName, false, BindingFlags.Instance | BindingFlags.Public,
151                                 null, new object[] { comObject }, null, null);
152                         return foo != null;
153                 } catch (MissingMethodException) {
154                         return false;
155                 }
156         }
157         
158         public static int Main ()
159         {
160                 bool isWindows = !(((int)Environment.OSVersion.Platform == 4) ||
161                         ((int)Environment.OSVersion.Platform == 128));
162
163                 if (!mono_cominterop_is_supported () && !isWindows)
164                         return 0;
165
166                 if (!CreateInstanceWithComObjectParameter ())
167                         return 1;
168
169                 return 0;
170         }
171 }