[runtime] Don't insta-fail when a faulty COM type is encountered. (#5616)
[mono.git] / mono / tests / bug-8477.cs
1 // This test is meant to make sure Mono doesn't fail when invalid COM invocations are present but never reached.
2 // See https://bugzilla.xamarin.com/show_bug.cgi?id=8477 for details.
3
4 using System;
5 using System.Runtime.InteropServices;
6 using System.Runtime.CompilerServices;
7
8 [ComImport, Guid("06A82D35-8946-4E2E-AE71-DADDE8341F5D")]
9 class COMponent
10 {
11     [MethodImpl(MethodImplOptions.InternalCall)] public static extern void InCOMplete1 ();
12     [MethodImpl(MethodImplOptions.InternalCall)] public extern void InCOMplete2 ();
13 }
14
15 class Test
16 {
17     static void COMmunicate (COMponent c)
18     {
19         if (c != null)
20             c.InCOMplete2 ();
21     }
22
23     static int Main()
24     {
25         // Check #1: An invocation of a ComImport class method w/o a corresponding interface method must lead to an exception.
26         try
27         {
28             COMponent.InCOMplete1();
29             // No exception has been thrown, something is wrong.
30             return 1;
31         }
32         catch (InvalidOperationException)
33         {
34             // An exception has been thrown and caught correctly.
35         }
36
37         // Check #2: Same as #1, but the method is not executed (i.e. it's located in a "cold" basic block). No exception should be thrown.
38         COMmunicate (null);
39
40         return 0;
41     }
42 }