Debian Codegraph @

external/corefx/src/System.Reflection.Emit/tests/MethodBuilder/MethodBuilderMakeGenericMethod.cs

raw
1 // Licensed to the .NET Foundation under one or more agreements.
2 // The .NET Foundation licenses this file to you under the MIT license.
3 // See the LICENSE file in the project root for more information.
4
5 using System.Collections.Generic;
6 using System.Linq;
7 using Xunit;
8
9 namespace System.Reflection.Emit.Tests
10 {
11 public class MethodBuilderMakeGenericMethod
12 {
13 public static IEnumerable<object[]> MakeGenericMethod_TestData()
14 {
15 yield return new object[] { new string[] { "T" }, new Type[] { typeof(string) }, typeof(void) };
16 yield return new object[] { new string[] { "T", "U" }, new Type[] { typeof(string), typeof(int) }, typeof(int) };
17 }
18
19 [Theory]
20 [MemberData(nameof(MakeGenericMethod_TestData))]
21 public void MakeGenericMethod(string[] names, Type[] typeArguments, Type returnType)
22 {
23 TypeBuilder type = Helpers.DynamicType(TypeAttributes.Abstract);
24
25 MethodBuilder builder = type.DefineMethod("TestMethod", MethodAttributes.Public | MethodAttributes.Static);
26 Type[] typeParameters = builder.DefineGenericParameters(names).Select(a => a.AsType()).ToArray();
27 builder.SetParameters(typeParameters);
28 builder.SetReturnType(returnType);
29
30 MethodInfo methodInfo = builder.MakeGenericMethod(typeArguments);
31 VerifyMethodInfo(methodInfo, builder, returnType);
32 }
33
34 [Fact]
35 public void TestNotThrowsExceptionOnNull()
36 {
37 Type returnType = typeof(void);
38 TypeBuilder type = Helpers.DynamicType(TypeAttributes.Abstract);
39 MethodBuilder builder = type.DefineMethod("TestMethod", MethodAttributes.Public | MethodAttributes.Static);
40 Type[] typeParameters = builder.DefineGenericParameters(new string[] { "T" }).Select(a => a.AsType()).ToArray();
41 builder.SetParameters(typeParameters);
42 builder.SetReturnType(returnType);
43
44 MethodInfo methodInfo = builder.MakeGenericMethod(null);
45 }
46
47 [Fact]
48 public void TestNotThrowsExceptionOnEmptyArray1()
49 {
50 Type returnType = typeof(void);
51
52 TypeBuilder type = Helpers.DynamicType(TypeAttributes.Abstract);
53 MethodBuilder builder = type.DefineMethod("TestMethod", MethodAttributes.Public | MethodAttributes.Static);
54 Type[] typeParameters = builder.DefineGenericParameters(new string[] { "T" }).Select(a => a.AsType()).ToArray();
55 builder.SetParameters(typeParameters);
56 builder.SetReturnType(returnType);
57
58 MethodInfo methodInfo = builder.MakeGenericMethod(new Type[0]);
59 }
60
61 [Fact]
62 public void TestNotThrowsExceptionOnEmptyArray2()
63 {
64 Type returnType = typeof(void);
65
66 TypeBuilder type = Helpers.DynamicType(TypeAttributes.Abstract);
67 MethodBuilder builder = type.DefineMethod("Test", MethodAttributes.Public | MethodAttributes.Static);
68 Type[] typeParameters = builder.DefineGenericParameters(new string[] { "T" }).Select(a => a.AsType()).ToArray();
69 builder.SetParameters(typeParameters);
70 builder.SetReturnType(returnType);
71
72 MethodInfo methodInfo = builder.MakeGenericMethod();
73 }
74
75 private void VerifyMethodInfo(MethodInfo methodInfo, MethodBuilder builder, Type returnType)
76 {
77 if (methodInfo == null)
78 {
79 Assert.Null(builder);
80 }
81 else
82 {
83 Assert.Equal(methodInfo.Name, builder.Name);
84 Assert.Equal(methodInfo.ReturnType, returnType);
85 }
86 }
87 }
88 }