Debian Codegraph @

external/corefx/src/System.Reflection.Emit/tests/MethodBuilder/MethodBuilderDefineParameter.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.Linq;
6 using Xunit;
7
8 namespace System.Reflection.Emit.Tests
9 {
10 public class MethodBuilderDefineParameter
11 {
12 private const MethodAttributes TestMethodAttributes = MethodAttributes.Public | MethodAttributes.Abstract | MethodAttributes.Virtual;
13
14 [Fact]
15 public void DefineParameter_TwoParameters()
16 {
17 TypeBuilder type = Helpers.DynamicType(TypeAttributes.Public);
18 Type[] paramTypes = new Type[] { typeof(string), typeof(object) };
19 Type returnType = typeof(int);
20
21 MethodBuilder method = type.DefineMethod("TestMethod", MethodAttributes.Public | MethodAttributes.Static, returnType, paramTypes);
22
23 method.DefineParameter(0, ParameterAttributes.None, "returnName");
24 method.DefineParameter(1, ParameterAttributes.In, "Param1");
25 method.DefineParameter(2, ParameterAttributes.Out, "Param2");
26
27 int expectedReturn = 3;
28 ILGenerator ilGenerator = method.GetILGenerator();
29 ilGenerator.Emit(OpCodes.Ldc_I4, expectedReturn);
30 ilGenerator.Emit(OpCodes.Ret);
31
32 Type resultType = type.CreateTypeInfo().AsType();
33
34 ParameterInfo[] parameters = method.GetParameters();
35 Assert.Equal("System.String Param1", parameters[0].ToString());
36 Assert.Equal("System.Object Param2", parameters[1].ToString());
37
38 // Invoke the method to verify it works correctly
39 MethodInfo resultMethod = resultType.GetMethod("TestMethod");
40 Assert.Equal(expectedReturn, resultMethod.Invoke(null, new object[] { "hello", new object() }));
41 }
42
43 [Fact]
44 public void DefineParameter_HasDefaultAttribute()
45 {
46 TypeBuilder type = Helpers.DynamicType(TypeAttributes.Abstract);
47 Type[] paramTypes = new Type[] { typeof(int) };
48 MethodBuilder method = type.DefineMethod("TestMethod", TestMethodAttributes, typeof(void), paramTypes);
49
50 ParameterBuilder parameter = method.DefineParameter(1, ParameterAttributes.HasDefault, "TestParam");
51 VerifyParameterBuilder(parameter, "TestParam", ParameterAttributes.HasDefault, 1);
52 }
53
54 [Fact]
55 public void DefineParameter_AllAttributes()
56 {
57 ParameterAttributes[] attributes = new ParameterAttributes[]
58 {
59 ParameterAttributes.HasDefault,
60 ParameterAttributes.HasFieldMarshal,
61 ParameterAttributes.In,
62 ParameterAttributes.None,
63 ParameterAttributes.Optional,
64 ParameterAttributes.Out,
65 ParameterAttributes.Retval
66 };
67
68 TypeBuilder type = Helpers.DynamicType(TypeAttributes.Abstract);
69 Type[] paramTypes = Enumerable.Repeat(typeof(int), attributes.Length).ToArray();
70 MethodBuilder method = type.DefineMethod("TestMethod", TestMethodAttributes, typeof(void), paramTypes);
71
72 for (int i = 1; i < attributes.Length; ++i)
73 {
74 ParameterBuilder parameter = method.DefineParameter(i, attributes[i], "TestParam");
75 VerifyParameterBuilder(parameter, "TestParam", attributes[i], i);
76 }
77 }
78
79 [Fact]
80 public void DefineParameter_AllAttributesCombined()
81 {
82 TypeBuilder type = Helpers.DynamicType(TypeAttributes.Abstract);
83 Type[] paramTypes = new Type[] { typeof(int) };
84 MethodBuilder method = type.DefineMethod("TestMethod", TestMethodAttributes, typeof(void), paramTypes);
85
86 ParameterAttributes attribute =
87 ParameterAttributes.HasDefault |
88 ParameterAttributes.HasFieldMarshal |
89 ParameterAttributes.In |
90 ParameterAttributes.None |
91 ParameterAttributes.Optional |
92 ParameterAttributes.Out |
93 ParameterAttributes.Retval;
94 ParameterBuilder parameter = method.DefineParameter(1, attribute, "TestParam");
95 VerifyParameterBuilder(parameter, "TestParam", attribute, 1);
96 }
97
98 [Fact]
99 public void DefineParameter_NegativeAttribute()
100 {
101 TypeBuilder type = Helpers.DynamicType(TypeAttributes.Abstract);
102 Type[] paramTypes = new Type[] { typeof(int) };
103 MethodBuilder method = type.DefineMethod("TestMethod", TestMethodAttributes, typeof(void), paramTypes);
104 ParameterBuilder parameter = method.DefineParameter(1, (ParameterAttributes)(-1), "TestParam");
105 }
106
107 [Fact]
108 public void DefineParameter_TypeAlreadyCreated_ThrowsInvalidOperationException()
109 {
110 TypeBuilder type = Helpers.DynamicType(TypeAttributes.Public);
111 Type returnType = typeof(int);
112 Type[] paramTypes = new Type[] { typeof(string), typeof(object) };
113
114 MethodBuilder method = type.DefineMethod("TestMethod", MethodAttributes.Public, returnType, paramTypes);
115 ILGenerator ilGenerator = method.GetILGenerator();
116 ilGenerator.Emit(OpCodes.Ret);
117
118 Type resultType = type.CreateTypeInfo().AsType();
119
120 Assert.Throws<InvalidOperationException>(() => method.DefineParameter(1, ParameterAttributes.Retval, "param1"));
121 }
122
123 [Fact]
124 public void DefineParameter_SingleParameter_TypeAlreadyCreated_ThrowsInvalidOperationException()
125 {
126 TypeBuilder type = Helpers.DynamicType(TypeAttributes.Abstract);
127 MethodBuilder builder = type.DefineMethod("TestMethod", TestMethodAttributes, typeof(void), new Type[] { typeof(int) });
128
129 type.CreateTypeInfo().AsType();
130
131 Assert.Throws<InvalidOperationException>(() => builder.DefineParameter(1, ParameterAttributes.HasDefault, "TestParam"));
132 }
133
134 [Theory]
135 [InlineData(TypeAttributes.Public, MethodAttributes.Public, ParameterAttributes.None, new Type[] { typeof(string), typeof(object) }, typeof(int))]
136 [InlineData(TypeAttributes.Abstract, MethodAttributes.Public | MethodAttributes.Abstract | MethodAttributes.Virtual, ParameterAttributes.HasDefault, new Type[] { typeof(int) }, typeof(void))]
137 public void DefineParameter_InvalidPosition_ThrowsArgumentOutOfRangeException(TypeAttributes typeAttributes, MethodAttributes methodAttributes, ParameterAttributes parameterAttributes, Type[] paramTypes, Type returnType)
138 {
139 TypeBuilder type = Helpers.DynamicType(typeAttributes);
140 MethodBuilder method = type.DefineMethod("TestMethod", methodAttributes, returnType, paramTypes);
141
142 Assert.Throws<ArgumentOutOfRangeException>(() => method.DefineParameter(-1, parameterAttributes, "Param1"));
143 Assert.Throws<ArgumentOutOfRangeException>(() => method.DefineParameter(paramTypes.Length + 1, parameterAttributes, "Param1"));
144 }
145
146 [Theory]
147 [InlineData(TypeAttributes.Public, MethodAttributes.Public, ParameterAttributes.None)]
148 [InlineData(TypeAttributes.Abstract, MethodAttributes.Public | MethodAttributes.Abstract | MethodAttributes.Virtual, ParameterAttributes.HasDefault)]
149 public void DefineParameter_NoParameters_NonZeroPosition_ThrowsArgumentOutOfRangeException(TypeAttributes typeAttributes, MethodAttributes methodAttributes, ParameterAttributes parameterAttributes)
150 {
151 TypeBuilder type = Helpers.DynamicType(typeAttributes);
152 MethodBuilder method = type.DefineMethod("TestMethod", methodAttributes);
153
154 Assert.Throws<ArgumentOutOfRangeException>(() => method.DefineParameter(1, parameterAttributes, "Param1"));
155 Assert.Throws<ArgumentOutOfRangeException>(() => method.DefineParameter(-1, parameterAttributes, "Param1"));
156 }
157
158 private static void VerifyParameterBuilder(ParameterBuilder parameter, string expectedName, ParameterAttributes expectedAttributes, int expectedPosition)
159 {
160 // This constant maps to ParameterAttributes.ReservedMask that is not available in the contract.
161 const int ReservedMaskParameterAttribute = 0xF000;
162 Assert.Equal(expectedName, parameter.Name);
163
164 int removedReservedAttributes = (int)expectedAttributes & ~ReservedMaskParameterAttribute;
165 Assert.Equal(removedReservedAttributes, (parameter.Attributes & removedReservedAttributes));
166 Assert.Equal(expectedPosition, parameter.Position);
167 }
168 }
169 }