| 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 MethodBuilderSetParameters |
| 12 |
{ |
| 13 |
public static IEnumerable<object[]> SetParameters_TestData() |
| 14 |
{ |
| 15 |
yield return new object[] { new Type[0], new string[] { "T" } }; |
| 16 |
yield return new object[] { new Type[0], new string[] { "T", "U" } }; |
| 17 |
yield return new object[] { new Type[] { typeof(int) }, new string[] { "T" } }; |
| 18 |
} |
| 19 |
|
| 20 |
[Theory] |
| 21 |
[MemberData(nameof(SetParameters_TestData))] |
| 22 |
public void SetParameters(Type[] parameterTypes, string[] typeParamNames) |
| 23 |
{ |
| 24 |
TypeBuilder type = Helpers.DynamicType(TypeAttributes.Abstract); |
| 25 |
MethodBuilder method = type.DefineMethod("TestMethod", MethodAttributes.Public, typeof(void), parameterTypes); |
| 26 |
|
| 27 |
Type[] typeParameters = method.DefineGenericParameters(typeParamNames).Select(a => a.AsType()).ToArray(); |
| 28 |
method.SetParameters(typeParameters); |
| 29 |
|
| 30 |
ILGenerator ilGenerator = method.GetILGenerator(); |
| 31 |
ilGenerator.Emit(OpCodes.Ret); |
| 32 |
|
| 33 |
Type createdType = type.CreateTypeInfo().AsType(); |
| 34 |
MethodInfo createdMethod = createdType.GetMethod(method.Name); |
| 35 |
VerifyParameters(createdMethod.GetParameters(), typeParameters, typeParamNames); |
| 36 |
} |
| 37 |
|
| 38 |
[Fact] |
| 39 |
public void SetParameters_WorksAfterTypeCreated() |
| 40 |
{ |
| 41 |
TypeBuilder type = Helpers.DynamicType(TypeAttributes.Abstract); |
| 42 |
MethodBuilder method = type.DefineMethod("TestMethod", MethodAttributes.Public); |
| 43 |
Type[] typeParameters = method.DefineGenericParameters("T").Select(a => a.AsType()).ToArray(); |
| 44 |
|
| 45 |
method.SetParameters(typeParameters); |
| 46 |
ILGenerator ilGenerator = method.GetILGenerator(); |
| 47 |
ilGenerator.Emit(OpCodes.Ret); |
| 48 |
|
| 49 |
type.CreateTypeInfo().AsType(); |
| 50 |
method.SetParameters(typeParameters); |
| 51 |
} |
| 52 |
|
| 53 |
[Fact] |
| 54 |
public void SetParameters_NullParameterTypes() |
| 55 |
{ |
| 56 |
TypeBuilder type = Helpers.DynamicType(TypeAttributes.Abstract); |
| 57 |
MethodBuilder method = type.DefineMethod("TestMethod", MethodAttributes.Public); |
| 58 |
|
| 59 |
method.SetParameters(null); |
| 60 |
ILGenerator ilGenerator = method.GetILGenerator(); |
| 61 |
ilGenerator.Emit(OpCodes.Ret); |
| 62 |
|
| 63 |
Type createdType = type.CreateTypeInfo().AsType(); |
| 64 |
MethodInfo createdMethod = createdType.GetMethod(method.Name); |
| 65 |
ParameterInfo[] parameters = createdMethod.GetParameters(); |
| 66 |
VerifyParameters(parameters, new Type[0], null); |
| 67 |
} |
| 68 |
|
| 69 |
[Fact] |
| 70 |
public void SetParameters_EmptyParameterTypes() |
| 71 |
{ |
| 72 |
TypeBuilder type = Helpers.DynamicType(TypeAttributes.Abstract); |
| 73 |
MethodBuilder builder = type.DefineMethod("TestMethod", MethodAttributes.Public); |
| 74 |
|
| 75 |
builder.SetParameters(new Type[0]); |
| 76 |
ILGenerator ilGenerator = builder.GetILGenerator(); |
| 77 |
ilGenerator.Emit(OpCodes.Ret); |
| 78 |
|
| 79 |
Type createdType = type.CreateTypeInfo().AsType(); |
| 80 |
|
| 81 |
MethodInfo method = createdType.GetMethod(builder.Name); |
| 82 |
ParameterInfo[] parameters = builder.GetParameters(); |
| 83 |
VerifyParameters(parameters, new Type[0], null); |
| 84 |
} |
| 85 |
|
| 86 |
[Fact] |
| 87 |
public void SetParameters_NoParameterTypes() |
| 88 |
{ |
| 89 |
TypeBuilder type = Helpers.DynamicType(TypeAttributes.Abstract); |
| 90 |
MethodBuilder builder = type.DefineMethod("TestMethod", MethodAttributes.Public); |
| 91 |
|
| 92 |
builder.SetParameters(); |
| 93 |
ILGenerator ilGenerator = builder.GetILGenerator(); |
| 94 |
ilGenerator.Emit(OpCodes.Ret); |
| 95 |
|
| 96 |
Type createdType = type.CreateTypeInfo().AsType(); |
| 97 |
|
| 98 |
MethodInfo method = createdType.GetMethod(builder.Name); |
| 99 |
ParameterInfo[] parameters = method.GetParameters(); |
| 100 |
VerifyParameters(parameters, new Type[0], null); |
| 101 |
} |
| 102 |
|
| 103 |
[Fact] |
| 104 |
public void SetParameters_NullParameter_ThrowsArgumentNullException() |
| 105 |
{ |
| 106 |
TypeBuilder type = Helpers.DynamicType(TypeAttributes.Abstract); |
| 107 |
MethodBuilder builder = type.DefineMethod("TestMethod", MethodAttributes.Public, typeof(void), new Type[] { typeof(int) }); |
| 108 |
Type[] typeParameters = builder.DefineGenericParameters("T").Select(a => a.AsType()).ToArray(); |
| 109 |
|
| 110 |
Type[] parameterTypes = new Type[typeParameters.Length + 1]; |
| 111 |
for (int i = 0; i < typeParameters.Length; ++i) |
| 112 |
{ |
| 113 |
parameterTypes[i] = typeParameters[i]; |
| 114 |
} |
| 115 |
parameterTypes[typeParameters.Length] = null; |
| 116 |
|
| 117 |
builder.SetParameters(parameterTypes); |
| 118 |
ILGenerator ilGenerator = builder.GetILGenerator(); |
| 119 |
ilGenerator.Emit(OpCodes.Ret); |
| 120 |
|
| 121 |
AssertExtensions.Throws<ArgumentNullException>("argument", () => type.CreateTypeInfo().AsType()); |
| 122 |
} |
| 123 |
|
| 124 |
private void VerifyParameters(ParameterInfo[] parameters, Type[] parameterTypes, string[] parameterName) |
| 125 |
{ |
| 126 |
if (parameterTypes == null) |
| 127 |
{ |
| 128 |
Assert.Null(parameters); |
| 129 |
} |
| 130 |
else |
| 131 |
{ |
| 132 |
Assert.NotNull(parameters); |
| 133 |
for (int i = 0; i < parameters.Length; ++i) |
| 134 |
{ |
| 135 |
ParameterInfo parameter = parameters[i]; |
| 136 |
if (parameter.Name != null) |
| 137 |
{ |
| 138 |
Assert.Equal(parameterName[i], parameter.Name); |
| 139 |
} |
| 140 |
else |
| 141 |
{ |
| 142 |
Assert.Equal(parameterName[i], parameter.ParameterType.Name); |
| 143 |
} |
| 144 |
|
| 145 |
Assert.Equal(i, parameter.Position); |
| 146 |
} |
| 147 |
} |
| 148 |
} |
| 149 |
} |
| 150 |
} |