| 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 Xunit; |
| 6 |
|
| 7 |
namespace System.Reflection.Emit.Tests |
| 8 |
{ |
| 9 |
public class ConstructorBuilderDefineParameter |
| 10 |
{ |
| 11 |
private static readonly ParameterAttributes[] s_supportedAttributes = new ParameterAttributes[] |
| 12 |
{ |
| 13 |
ParameterAttributes.None, |
| 14 |
ParameterAttributes.HasDefault, |
| 15 |
ParameterAttributes.HasFieldMarshal, |
| 16 |
ParameterAttributes.In, |
| 17 |
ParameterAttributes.None, |
| 18 |
ParameterAttributes.Optional, |
| 19 |
ParameterAttributes.Out, |
| 20 |
ParameterAttributes.Retval |
| 21 |
}; |
| 22 |
|
| 23 |
[Fact] |
| 24 |
public void DefineParameter_MultipleParameters() |
| 25 |
{ |
| 26 |
Type[] parameterTypes = new Type[] { typeof(int), typeof(string) }; |
| 27 |
|
| 28 |
for (int i = 1; i < s_supportedAttributes.Length; i++) |
| 29 |
{ |
| 30 |
TypeBuilder type = Helpers.DynamicType(TypeAttributes.NotPublic); |
| 31 |
ConstructorBuilder constructor = type.DefineConstructor(MethodAttributes.Public, CallingConventions.Standard, parameterTypes); |
| 32 |
|
| 33 |
constructor.DefineParameter(1, s_supportedAttributes[i - 1], "parameter1" + i); |
| 34 |
constructor.DefineParameter(2, s_supportedAttributes[i], "parameter2" + i); |
| 35 |
|
| 36 |
ILGenerator ilGenerator = constructor.GetILGenerator(); |
| 37 |
ilGenerator.Emit(OpCodes.Ldarg_0); |
| 38 |
ilGenerator.Emit(OpCodes.Call, typeof(object).GetConstructor(new Type[0])); |
| 39 |
ilGenerator.Emit(OpCodes.Ret); |
| 40 |
|
| 41 |
type.CreateTypeInfo().AsType(); |
| 42 |
|
| 43 |
ParameterInfo[] definedParams = constructor.GetParameters(); |
| 44 |
Assert.Equal(2, definedParams.Length); |
| 45 |
} |
| 46 |
} |
| 47 |
|
| 48 |
[Fact] |
| 49 |
public void DefineParameter_SingleParameter() |
| 50 |
{ |
| 51 |
Type[] parameterTypes = new Type[] { typeof(ConstructorBuilderDefineParameter) }; |
| 52 |
|
| 53 |
for (int i = 0; i < s_supportedAttributes.Length; i++) |
| 54 |
{ |
| 55 |
TypeBuilder type = Helpers.DynamicType(TypeAttributes.NotPublic); |
| 56 |
ConstructorBuilder constructor = type.DefineConstructor(MethodAttributes.Public, CallingConventions.Standard, parameterTypes); |
| 57 |
|
| 58 |
constructor.DefineParameter(1, s_supportedAttributes[i], "parameter1" + i); |
| 59 |
|
| 60 |
ILGenerator ilGenerator = constructor.GetILGenerator(); |
| 61 |
ilGenerator.Emit(OpCodes.Ldarg_0); |
| 62 |
ilGenerator.Emit(OpCodes.Call, typeof(object).GetConstructor(new Type[0])); |
| 63 |
ilGenerator.Emit(OpCodes.Ret); |
| 64 |
|
| 65 |
type.CreateTypeInfo().AsType(); |
| 66 |
|
| 67 |
ParameterInfo[] definedParams = constructor.GetParameters(); |
| 68 |
Assert.Equal(1, definedParams.Length); |
| 69 |
} |
| 70 |
} |
| 71 |
|
| 72 |
[Fact] |
| 73 |
public void DefineParameter_SingleParameter_NullParameterName() |
| 74 |
{ |
| 75 |
Type[] parameterTypes = new Type[] { typeof(ConstructorBuilderDefineParameter) }; |
| 76 |
|
| 77 |
for (int i = 0; i < s_supportedAttributes.Length; i++) |
| 78 |
{ |
| 79 |
TypeBuilder type = Helpers.DynamicType(TypeAttributes.NotPublic); |
| 80 |
ConstructorBuilder constructor = type.DefineConstructor(MethodAttributes.Public, CallingConventions.Standard, parameterTypes); |
| 81 |
|
| 82 |
constructor.DefineParameter(1, s_supportedAttributes[i], null); |
| 83 |
|
| 84 |
ILGenerator ilGenerator = constructor.GetILGenerator(); |
| 85 |
ilGenerator.Emit(OpCodes.Ldarg_0); |
| 86 |
ilGenerator.Emit(OpCodes.Call, typeof(object).GetConstructor(new Type[0])); |
| 87 |
ilGenerator.Emit(OpCodes.Ret); |
| 88 |
|
| 89 |
type.CreateTypeInfo().AsType(); |
| 90 |
|
| 91 |
ParameterInfo[] definedParams = constructor.GetParameters(); |
| 92 |
Assert.Equal(1, definedParams.Length); |
| 93 |
} |
| 94 |
} |
| 95 |
|
| 96 |
[Fact] |
| 97 |
public void DefineParameter_MultipleParameters_NullParameterNames() |
| 98 |
{ |
| 99 |
Type[] parameterTypes = new Type[] { typeof(int), typeof(string) }; |
| 100 |
|
| 101 |
for (int i = 1; i < s_supportedAttributes.Length; ++i) |
| 102 |
{ |
| 103 |
TypeBuilder type = Helpers.DynamicType(TypeAttributes.NotPublic); |
| 104 |
ConstructorBuilder constructor = type.DefineConstructor(MethodAttributes.Public, CallingConventions.Standard, parameterTypes); |
| 105 |
|
| 106 |
constructor.DefineParameter(1, s_supportedAttributes[i - 1], null); |
| 107 |
constructor.DefineParameter(2, s_supportedAttributes[i], null); |
| 108 |
|
| 109 |
ILGenerator ilGenerator = constructor.GetILGenerator(); |
| 110 |
ilGenerator.Emit(OpCodes.Ldarg_0); |
| 111 |
ilGenerator.Emit(OpCodes.Call, typeof(object).GetConstructor(new Type[0])); |
| 112 |
ilGenerator.Emit(OpCodes.Ret); |
| 113 |
|
| 114 |
type.CreateTypeInfo().AsType(); |
| 115 |
|
| 116 |
ParameterInfo[] definedParams = constructor.GetParameters(); |
| 117 |
Assert.Equal(2, definedParams.Length); |
| 118 |
} |
| 119 |
} |
| 120 |
|
| 121 |
[Fact] |
| 122 |
public void DefineParameter_ZeroSequence_NoParameters() |
| 123 |
{ |
| 124 |
TypeBuilder type = Helpers.DynamicType(TypeAttributes.NotPublic); |
| 125 |
ConstructorBuilder constructor = type.DefineConstructor(MethodAttributes.Public, CallingConventions.Any, new Type[0]); |
| 126 |
Assert.NotNull(constructor.DefineParameter(0, ParameterAttributes.None, "p")); |
| 127 |
} |
| 128 |
|
| 129 |
[Fact] |
| 130 |
public void DefineParameter_ZeroSequence_SingleParameter() |
| 131 |
{ |
| 132 |
TypeBuilder type = Helpers.DynamicType(TypeAttributes.NotPublic); |
| 133 |
ConstructorBuilder constructor = type.DefineConstructor(MethodAttributes.Public, CallingConventions.Any, new Type[0]); |
| 134 |
Assert.NotNull(constructor.DefineParameter(0, ParameterAttributes.None, "p")); |
| 135 |
} |
| 136 |
|
| 137 |
[Fact] |
| 138 |
public void DefineParameter_NoParameters_InvalidSequence_ThrowsArgumentOutOfRangeException() |
| 139 |
{ |
| 140 |
TypeBuilder type = Helpers.DynamicType(TypeAttributes.NotPublic); |
| 141 |
ConstructorBuilder constructor = type.DefineConstructor(MethodAttributes.Public, CallingConventions.Standard, new Type[0]); |
| 142 |
|
| 143 |
Assert.Throws<ArgumentOutOfRangeException>(() => constructor.DefineParameter(-1, ParameterAttributes.None, "p")); |
| 144 |
Assert.Throws<ArgumentOutOfRangeException>(() => constructor.DefineParameter(1, ParameterAttributes.None, "p")); |
| 145 |
} |
| 146 |
|
| 147 |
[Fact] |
| 148 |
public void DefineParameter_SingleParameter_InvalidSequence_ThrowsArgumentOutOfRangeException() |
| 149 |
{ |
| 150 |
TypeBuilder type = Helpers.DynamicType(TypeAttributes.NotPublic); |
| 151 |
ConstructorBuilder constructor = type.DefineConstructor(MethodAttributes.Public, CallingConventions.Standard, new Type[] { typeof(int) }); |
| 152 |
|
| 153 |
Assert.Throws<ArgumentOutOfRangeException>(() => constructor.DefineParameter(2, ParameterAttributes.None, "p")); |
| 154 |
} |
| 155 |
|
| 156 |
[Fact] |
| 157 |
public void DefineParameter_TypeAlreadyCreated_ThrowsInvalidOperationException() |
| 158 |
{ |
| 159 |
TypeBuilder type = Helpers.DynamicType(TypeAttributes.NotPublic); |
| 160 |
ConstructorBuilder constructor = type.DefineConstructor(MethodAttributes.Public, CallingConventions.Standard, new Type[] { typeof(int) }); |
| 161 |
|
| 162 |
constructor.GetILGenerator().Emit(OpCodes.Ret); |
| 163 |
type.CreateTypeInfo().AsType(); |
| 164 |
|
| 165 |
Assert.Throws<InvalidOperationException>(() => constructor.DefineParameter(1, ParameterAttributes.None, "p")); |
| 166 |
} |
| 167 |
} |
| 168 |
} |