| 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 PropertyBuilderTest2 |
| 10 |
{ |
| 11 |
[Theory] |
| 12 |
[InlineData(MethodAttributes.Public, CallingConventions.HasThis, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance)] |
| 13 |
[InlineData(MethodAttributes.Private, CallingConventions.HasThis, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance)] |
| 14 |
[InlineData(MethodAttributes.Public | MethodAttributes.Static, CallingConventions.Any, BindingFlags.Public | BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.Instance)] |
| 15 |
public void AddOtherMethod(MethodAttributes attributes, CallingConventions callingConventions, BindingFlags bindingFlags) |
| 16 |
{ |
| 17 |
Type[] paramTypes = new Type[] { typeof(int) }; |
| 18 |
|
| 19 |
TypeBuilder type = Helpers.DynamicType(TypeAttributes.Class | TypeAttributes.Public); |
| 20 |
PropertyBuilder property = type.DefineProperty("TestProperty", PropertyAttributes.None, typeof(int), new Type[0]); |
| 21 |
MethodBuilder method = type.DefineMethod("TestMethod", attributes, callingConventions, typeof(int), paramTypes); |
| 22 |
|
| 23 |
ILGenerator methodILGenerator = method.GetILGenerator(); |
| 24 |
methodILGenerator.Emit(OpCodes.Ldarg_0); |
| 25 |
methodILGenerator.Emit(OpCodes.Ldarg_1); |
| 26 |
methodILGenerator.Emit(OpCodes.Ret); |
| 27 |
|
| 28 |
property.AddOtherMethod(method); |
| 29 |
Type createdType = type.CreateTypeInfo().AsType(); |
| 30 |
PropertyInfo createdProperty = createdType.GetProperty("TestProperty", bindingFlags); |
| 31 |
|
| 32 |
MethodInfo[] actualMethods = createdProperty.GetAccessors(true); |
| 33 |
Assert.Equal(1, actualMethods.Length); |
| 34 |
Assert.Equal(method.Name, actualMethods[0].Name); |
| 35 |
} |
| 36 |
|
| 37 |
[Fact] |
| 38 |
public void AddOtherMethod_NullMethodBuilder_ThrowsArgumentNullExceptio() |
| 39 |
{ |
| 40 |
TypeBuilder type = Helpers.DynamicType(TypeAttributes.Class | TypeAttributes.Public); |
| 41 |
PropertyBuilder property = type.DefineProperty("TestProperty", PropertyAttributes.None, typeof(int), new Type[0]); |
| 42 |
|
| 43 |
AssertExtensions.Throws<ArgumentNullException>("mdBuilder", () => property.AddOtherMethod(null)); |
| 44 |
} |
| 45 |
|
| 46 |
[Fact] |
| 47 |
public void AddOtherMethod_TypeAlreadyCreated_ThrowsInvalidOperationException() |
| 48 |
{ |
| 49 |
Type[] paramTypes = new Type[] { typeof(int) }; |
| 50 |
|
| 51 |
TypeBuilder type = Helpers.DynamicType(TypeAttributes.Class | TypeAttributes.Public); |
| 52 |
PropertyBuilder property = type.DefineProperty("TestProperty", PropertyAttributes.None, typeof(int), new Type[0]); |
| 53 |
MethodBuilder method = type.DefineMethod("TestMethod", MethodAttributes.Public, CallingConventions.HasThis, typeof(int), paramTypes); |
| 54 |
ILGenerator methodILGenerator = method.GetILGenerator(); |
| 55 |
|
| 56 |
methodILGenerator.Emit(OpCodes.Ldarg_0); |
| 57 |
methodILGenerator.Emit(OpCodes.Ldarg_1); |
| 58 |
methodILGenerator.Emit(OpCodes.Ret); |
| 59 |
|
| 60 |
type.CreateTypeInfo().AsType(); |
| 61 |
Assert.Throws<InvalidOperationException>(() => property.AddOtherMethod(method)); |
| 62 |
} |
| 63 |
} |
| 64 |
} |
| 65 |
|