blob: f2c6f6b615cdbe2efc738ed473e2cc59ee00b4f6 (
plain)
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
|
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System;
using System.Runtime.CompilerServices;
namespace Sample
{
public sealed class C1 : I1<string>, I2
{
}
class Program
{
static int Main(string[] args)
{
CallOpcodeNonGenericInterface();
CallOpcodeNonGenericInterfaceGenericMethod();
CallOpcodeGenericInterface();
CallOpcodeGenericInterfaceGenericMethod();
CallVirtOpcodeNonGenericInterface();
CallVirtOpcodeNonGenericInterfaceGenericMethod();
CallVirtOpcodeGenericInterface();
CallVirtOpcodeGenericInterfaceGenericMethod();
return 100;
}
private static void CallOpcodeNonGenericInterface()
{
Console.WriteLine("Testing call opcode for calling DIM on non-generic interface non-generic method");
if (((I2)new C1()).GetItemTypeNonGeneric(typeof(string)) != typeof(string))
throw new Exception("CallOpcodeGenericInterface failed");
}
private static void CallOpcodeNonGenericInterfaceGenericMethod()
{
Console.WriteLine("Testing call opcode for calling DIM on non-generic interface non-generic method");
if (((I2)new C1()).GetItemTypeGeneric<object>() != typeof(object))
throw new Exception("CallOpcodeGenericInterface failed");
}
private static void CallOpcodeGenericInterface()
{
Console.WriteLine("Testing call opcode for calling DIM on generic interface");
if (((I1<string>)new C1()).GetItemType() != typeof(string))
throw new Exception("CallOpcodeGenericInterface failed");
}
private static void CallOpcodeGenericInterfaceGenericMethod()
{
Console.WriteLine("Testing call opcode for calling generic method on DIM on generic interface");
if (((I1<string>)new C1()).GetItemTypeMethod<object>() != typeof(object))
throw new Exception("CallOpcodeGenericInterface failed");
}
private static void CallVirtOpcodeNonGenericInterface()
{
Console.WriteLine("Testing callvirt opcode for calling DIM on non-generic interface non-generic method");
I2 c1 = new C1();
if (c1.GetItemTypeNonGeneric(typeof(string)) != typeof(string))
throw new Exception("CallOpcodeGenericInterface failed");
}
private static void CallVirtOpcodeNonGenericInterfaceGenericMethod()
{
Console.WriteLine("Testing callvirt opcode for calling DIM on non-generic interface non-generic method");
I2 c1 = new C1();
if (c1.GetItemTypeGeneric<object>() != typeof(object))
throw new Exception("CallOpcodeGenericInterface failed");
}
private static void CallVirtOpcodeGenericInterface()
{
Console.WriteLine("Testing callvirt opcode for calling DIM on generic interface");
I1<string> c1 = new C1();
if (c1.GetItemType() != typeof(string))
throw new Exception("CallVirtOpcodeGenericInterface failed");
}
private static void CallVirtOpcodeGenericInterfaceGenericMethod()
{
Console.WriteLine("Testing callvirt opcode for calling generic method on DIM on generic interface");
I1<string> c1 = new C1();
if (c1.GetItemTypeMethod<object>() != typeof(object))
throw new Exception("CallVirtOpcodeGenericInterfaceGenericMethod failed");
}
}
}
public interface I1<T>
{
[MethodImpl(MethodImplOptions.NoInlining)]
sealed Type GetItemType() => typeof(T);
[MethodImpl(MethodImplOptions.NoInlining)]
sealed Type GetItemTypeMethod<U>() => typeof(U);
}
public interface I2
{
[MethodImpl(MethodImplOptions.NoInlining)]
sealed Type GetItemTypeNonGeneric(Type t) => t;
[MethodImpl(MethodImplOptions.NoInlining)]
sealed Type GetItemTypeGeneric<U>() => typeof(U);
}
|