| 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 Xunit; |
| 7 |
|
| 8 |
namespace System.Reflection.Emit.Tests |
| 9 |
{ |
| 10 |
public class MethodBuilderDefineGenericParameters |
| 11 |
{ |
| 12 |
public static IEnumerable<object[]> DefineGenericParameters_TestData() |
| 13 |
{ |
| 14 |
yield return new object[] { new string[] { "T" } }; |
| 15 |
yield return new object[] { new string[] { "T", "U" } }; |
| 16 |
yield return new object[] { new string[] { "T1", "T2", "T3" } }; |
| 17 |
} |
| 18 |
|
| 19 |
[Theory] |
| 20 |
[MemberData(nameof(DefineGenericParameters_TestData))] |
| 21 |
public void DefineGenericParameters_TwoTypeParameters(string[] names) |
| 22 |
{ |
| 23 |
TypeBuilder type = Helpers.DynamicType(TypeAttributes.NotPublic); |
| 24 |
MethodBuilder method = type.DefineMethod("TestMethod", MethodAttributes.Public); |
| 25 |
|
| 26 |
GenericTypeParameterBuilder[] parameters = method.DefineGenericParameters(names); |
| 27 |
Assert.True(method.IsGenericMethod); |
| 28 |
Assert.True(method.IsGenericMethodDefinition); |
| 29 |
Assert.Equal(names.Length, parameters.Length); |
| 30 |
|
| 31 |
for (int i = 0; i < names.Length; i++) |
| 32 |
{ |
| 33 |
GenericTypeParameterBuilder parameter = parameters[i]; |
| 34 |
Assert.Equal(method, parameter.DeclaringMethod); |
| 35 |
Assert.Equal(names[i], parameters[i].Name); |
| 36 |
Assert.Equal(i, parameters[i].GenericParameterPosition); |
| 37 |
} |
| 38 |
} |
| 39 |
|
| 40 |
[Fact] |
| 41 |
public void DefineGenericParameter_MultipleParameters() |
| 42 |
{ |
| 43 |
TypeBuilder type = Helpers.DynamicType(TypeAttributes.Public); |
| 44 |
MethodBuilder method = type.DefineMethod("TestMethod", MethodAttributes.Public, typeof(int), new Type[0]); |
| 45 |
method.DefineGenericParameters(new string[] { "T", "U", "V" }); |
| 46 |
|
| 47 |
ILGenerator ilGenerator = method.GetILGenerator(); |
| 48 |
ilGenerator.Emit(OpCodes.Ldc_I4, 100); |
| 49 |
ilGenerator.Emit(OpCodes.Ret); |
| 50 |
|
| 51 |
Type resultType = type.CreateTypeInfo().AsType(); |
| 52 |
Type[] typeArguments = { typeof(int), typeof(string), typeof(object) }; |
| 53 |
MethodInfo constructedMethod = resultType.GetMethod("TestMethod").MakeGenericMethod(typeArguments); |
| 54 |
Assert.Equal(typeArguments, constructedMethod.GetGenericArguments()); |
| 55 |
} |
| 56 |
|
| 57 |
[Fact] |
| 58 |
public void DefineGenericParameters_SingleTypeParameter_SetImplementationFlagsCalled_ThrowsInvalidOperationException() |
| 59 |
{ |
| 60 |
TypeBuilder type = Helpers.DynamicType(TypeAttributes.NotPublic); |
| 61 |
MethodBuilder method = type.DefineMethod("TestMethod", MethodAttributes.Public); |
| 62 |
method.SetImplementationFlags(MethodImplAttributes.Managed); |
| 63 |
Assert.Throws<InvalidOperationException>(() => method.DefineGenericParameters("T")); |
| 64 |
} |
| 65 |
|
| 66 |
[Fact] |
| 67 |
public void DefineGenericParameters_TwoTypeParameters_SetImplementationFlagsCalled_ThrowsInvalidOperationException() |
| 68 |
{ |
| 69 |
TypeBuilder type = Helpers.DynamicType(TypeAttributes.NotPublic); |
| 70 |
MethodBuilder method = type.DefineMethod("TestMethod", MethodAttributes.Public | MethodAttributes.Static); |
| 71 |
|
| 72 |
method.SetImplementationFlags(MethodImplAttributes.IL | MethodImplAttributes.Managed | MethodImplAttributes.Synchronized | MethodImplAttributes.NoInlining); |
| 73 |
|
| 74 |
Assert.Throws<InvalidOperationException>(() => method.DefineGenericParameters("T", "U")); |
| 75 |
} |
| 76 |
|
| 77 |
[Fact] |
| 78 |
public void DefineGenericParameters_SingleTypeParameter_AlreadyDefined_ThrowsInvalidOperationException() |
| 79 |
{ |
| 80 |
TypeBuilder type = Helpers.DynamicType(TypeAttributes.NotPublic); |
| 81 |
MethodBuilder method = type.DefineMethod("TestMethod", MethodAttributes.Public); |
| 82 |
|
| 83 |
method.DefineGenericParameters("T"); |
| 84 |
Assert.Throws<InvalidOperationException>(() => method.DefineGenericParameters("T")); |
| 85 |
} |
| 86 |
|
| 87 |
[Fact] |
| 88 |
public void DefineGenericParameters_TwoTypeParameters_AlreadyDefined_ThrowsInvalidOperationException() |
| 89 |
{ |
| 90 |
TypeBuilder type = Helpers.DynamicType(TypeAttributes.NotPublic); |
| 91 |
MethodBuilder method = type.DefineMethod("TestMethod", MethodAttributes.Public | MethodAttributes.Static); |
| 92 |
|
| 93 |
method.DefineGenericParameters("T", "U"); |
| 94 |
Assert.Throws<InvalidOperationException>(() => method.DefineGenericParameters("M", "K")); |
| 95 |
} |
| 96 |
|
| 97 |
[Theory] |
| 98 |
[InlineData(TypeAttributes.NotPublic, MethodAttributes.Public)] |
| 99 |
[InlineData(TypeAttributes.Public, MethodAttributes.Public | MethodAttributes.Static)] |
| 100 |
public void DefineGenericParameters_NullNames_ThrowsArgumentNullException(TypeAttributes typeAttributes, MethodAttributes methodAttributes) |
| 101 |
{ |
| 102 |
TypeBuilder type = Helpers.DynamicType(TypeAttributes.NotPublic); |
| 103 |
MethodBuilder method = type.DefineMethod("TestMethod", methodAttributes); |
| 104 |
AssertExtensions.Throws<ArgumentNullException>("names", () => method.DefineGenericParameters(null)); |
| 105 |
} |
| 106 |
|
| 107 |
[Theory] |
| 108 |
[InlineData(TypeAttributes.NotPublic, MethodAttributes.Public)] |
| 109 |
[InlineData(TypeAttributes.Public, MethodAttributes.Public | MethodAttributes.Static)] |
| 110 |
public void DefineGenericParameters_NamesContainsNull_ThrowsArgumentNullException(TypeAttributes typeAttributes, MethodAttributes methodAttributes) |
| 111 |
{ |
| 112 |
TypeBuilder type = Helpers.DynamicType(TypeAttributes.NotPublic); |
| 113 |
MethodBuilder method = type.DefineMethod("Test", methodAttributes); |
| 114 |
string[] typeParamNames = new string[] { "T", null, "U" }; |
| 115 |
AssertExtensions.Throws<ArgumentNullException>("names", () => method.DefineGenericParameters(typeParamNames)); |
| 116 |
} |
| 117 |
|
| 118 |
[Theory] |
| 119 |
[InlineData(TypeAttributes.NotPublic, MethodAttributes.Public)] |
| 120 |
[InlineData(TypeAttributes.Public, MethodAttributes.Public | MethodAttributes.Static)] |
| 121 |
public void DefineGenericParameters_EmptyNames_ThrowsArgumentException(TypeAttributes typeAttributes, MethodAttributes methodAttributes) |
| 122 |
{ |
| 123 |
TypeBuilder type = Helpers.DynamicType(TypeAttributes.NotPublic); |
| 124 |
MethodBuilder builder = type.DefineMethod("TestMethod", methodAttributes); |
| 125 |
AssertExtensions.Throws<ArgumentException>("names", () => builder.DefineGenericParameters()); |
| 126 |
} |
| 127 |
|
| 128 |
[Fact] |
| 129 |
public void DefineGenericParameters_TypeCreated_ThrowsInvalidOperationException() |
| 130 |
{ |
| 131 |
TypeBuilder type = Helpers.DynamicType(TypeAttributes.NotPublic); |
| 132 |
MethodBuilder method = type.DefineMethod("method1", MethodAttributes.Public | MethodAttributes.Static); |
| 133 |
method.GetILGenerator().Emit(OpCodes.Ret); |
| 134 |
|
| 135 |
Type resultType = type.CreateTypeInfo().AsType(); |
| 136 |
Assert.Throws<InvalidOperationException>(() => method.DefineGenericParameters("T", "U")); |
| 137 |
} |
| 138 |
} |
| 139 |
} |