| 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 PropertyBuilderTest12 |
| 11 |
{ |
| 12 |
[Fact] |
| 13 |
public void SetCustomAttribute() |
| 14 |
{ |
| 15 |
Type returnType = typeof(int); |
| 16 |
Type[] ctorParamTypes = new Type[] { typeof(int) }; |
| 17 |
|
| 18 |
int expectedValue = 10; |
| 19 |
object[] ctorParamValues = new object[] { expectedValue }; |
| 20 |
CustomAttributeBuilder customAttrBuilder = new CustomAttributeBuilder(typeof(IntPropertyAttribute).GetConstructor(ctorParamTypes), ctorParamValues); |
| 21 |
|
| 22 |
TypeBuilder type = Helpers.DynamicType(TypeAttributes.Class | TypeAttributes.Public); |
| 23 |
|
| 24 |
PropertyBuilder property = type.DefineProperty("TestProperty", PropertyAttributes.HasDefault, returnType, null); |
| 25 |
property.SetCustomAttribute(customAttrBuilder); |
| 26 |
|
| 27 |
MethodAttributes getMethodAttributes = MethodAttributes.Public | MethodAttributes.SpecialName | MethodAttributes.HideBySig; |
| 28 |
MethodBuilder method = type.DefineMethod("TestMethod", getMethodAttributes, returnType, new Type[0]); |
| 29 |
|
| 30 |
ILGenerator methodILGenerator = method.GetILGenerator(); |
| 31 |
methodILGenerator.Emit(OpCodes.Ldarg_0); |
| 32 |
methodILGenerator.Emit(OpCodes.Ret); |
| 33 |
|
| 34 |
property.SetGetMethod(method); |
| 35 |
|
| 36 |
BindingFlags bindingFlags = BindingFlags.Public | BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Static; |
| 37 |
Type createdType = type.CreateTypeInfo().AsType(); |
| 38 |
PropertyInfo createdProperty = createdType.GetProperty("TestProperty", bindingFlags); |
| 39 |
object[] attributes = createdProperty.GetCustomAttributes(false).ToArray(); |
| 40 |
|
| 41 |
Assert.Equal(1, attributes.Length); |
| 42 |
Assert.True(attributes[0] is IntPropertyAttribute); |
| 43 |
Assert.Equal(expectedValue, (attributes[0] as IntPropertyAttribute).Value); |
| 44 |
} |
| 45 |
|
| 46 |
[Fact] |
| 47 |
public void SetCustomAttribute_CustomAttributeBuilder_NullBuilder_ThrowsArgumentNullException() |
| 48 |
{ |
| 49 |
TypeBuilder type = Helpers.DynamicType(TypeAttributes.Class | TypeAttributes.Public); |
| 50 |
PropertyBuilder property = type.DefineProperty("TestProperty", PropertyAttributes.HasDefault, typeof(int), null); |
| 51 |
|
| 52 |
AssertExtensions.Throws<ArgumentNullException>("customBuilder", () => property.SetCustomAttribute(null)); |
| 53 |
} |
| 54 |
|
| 55 |
[Fact] |
| 56 |
public void SetCustomAttribute_CustomAttributeBuilder_TypeNotCreated_ThrowsInvalidOperationException() |
| 57 |
{ |
| 58 |
TypeBuilder type = Helpers.DynamicType(TypeAttributes.Class | TypeAttributes.Public); |
| 59 |
PropertyBuilder property = type.DefineProperty("TestProperty", PropertyAttributes.HasDefault, typeof(int), null); |
| 60 |
|
| 61 |
Type[] ctorParamTypes = new Type[] { typeof(int) }; |
| 62 |
object[] ctorParamValues = new object[] { 10 }; |
| 63 |
CustomAttributeBuilder customAttrBuilder = new CustomAttributeBuilder(typeof(IntPropertyAttribute).GetConstructor(ctorParamTypes), ctorParamValues); |
| 64 |
|
| 65 |
type.CreateTypeInfo().AsType(); |
| 66 |
Assert.Throws<InvalidOperationException>(() => property.SetCustomAttribute(customAttrBuilder)); |
| 67 |
} |
| 68 |
|
| 69 |
[Fact] |
| 70 |
public void SetCustomAttribute_ConstructorInfo_ByteArray() |
| 71 |
{ |
| 72 |
Type returnType = typeof(int); |
| 73 |
Type[] ctorParamTypes = new Type[] { typeof(int) }; |
| 74 |
|
| 75 |
int expectedValue = 10; |
| 76 |
byte[] binaryAttribute = new byte[6]; |
| 77 |
ConstructorInfo con = typeof(IntPropertyAttribute).GetConstructor(ctorParamTypes); |
| 78 |
binaryAttribute[0] = 01; |
| 79 |
binaryAttribute[1] = 00; |
| 80 |
for (int i = 0; i < binaryAttribute.Length - 2; ++i) |
| 81 |
{ |
| 82 |
binaryAttribute[i + 2] = (byte)(expectedValue >> (8 * i) & 0xff); |
| 83 |
} |
| 84 |
|
| 85 |
TypeBuilder type = Helpers.DynamicType(TypeAttributes.Class | TypeAttributes.Public); |
| 86 |
|
| 87 |
PropertyBuilder property = type.DefineProperty("TestProperty", PropertyAttributes.HasDefault, returnType, null); |
| 88 |
property.SetCustomAttribute(con, binaryAttribute); |
| 89 |
|
| 90 |
MethodAttributes getMethodAttributes = MethodAttributes.Public | MethodAttributes.SpecialName | MethodAttributes.HideBySig; |
| 91 |
MethodBuilder method = type.DefineMethod("TestMethod", getMethodAttributes, returnType, new Type[0]); |
| 92 |
ILGenerator methodILGenerator = method.GetILGenerator(); |
| 93 |
methodILGenerator.Emit(OpCodes.Ldarg_0); |
| 94 |
methodILGenerator.Emit(OpCodes.Ret); |
| 95 |
|
| 96 |
property.SetGetMethod(method); |
| 97 |
|
| 98 |
BindingFlags bindingFlags = BindingFlags.Public | BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Static; |
| 99 |
Type createdType = type.CreateTypeInfo().AsType(); |
| 100 |
PropertyInfo createdProperty = createdType.GetProperty("TestProperty", bindingFlags); |
| 101 |
object[] attributes = createdProperty.GetCustomAttributes(false).ToArray(); |
| 102 |
|
| 103 |
Assert.Equal(1, attributes.Length); |
| 104 |
Assert.True(attributes[0] is IntPropertyAttribute); |
| 105 |
Assert.Equal(expectedValue, (attributes[0] as IntPropertyAttribute).Value); |
| 106 |
} |
| 107 |
|
| 108 |
[Fact] |
| 109 |
public void SetCustomAttribute_ConstructorInfo_ByteArray_NullConstructorInfo_ThrowsArgumentNullException() |
| 110 |
{ |
| 111 |
TypeBuilder type = Helpers.DynamicType(TypeAttributes.Class | TypeAttributes.Public); |
| 112 |
PropertyBuilder property = type.DefineProperty("TestProperty", PropertyAttributes.HasDefault, typeof(int), null); |
| 113 |
|
| 114 |
AssertExtensions.Throws<ArgumentNullException>("con", () => property.SetCustomAttribute(null, new byte[6])); |
| 115 |
} |
| 116 |
|
| 117 |
|
| 118 |
[Fact] |
| 119 |
public void SetCustomAttribute_ConstructorInfo_ByteArray_TypeAlreadyCreated_ThrowsInvalidOperationException() |
| 120 |
{ |
| 121 |
TypeBuilder type = Helpers.DynamicType(TypeAttributes.Class | TypeAttributes.Public); |
| 122 |
PropertyBuilder property = type.DefineProperty("TestProperty", PropertyAttributes.HasDefault, typeof(int), null); |
| 123 |
|
| 124 |
ConstructorInfo con = typeof(IntPropertyAttribute).GetConstructor(new Type[] { typeof(int) }); |
| 125 |
|
| 126 |
type.CreateTypeInfo().AsType(); |
| 127 |
Assert.Throws<InvalidOperationException>(() => property.SetCustomAttribute(con, new byte[6])); |
| 128 |
} |
| 129 |
} |
| 130 |
|
| 131 |
[AttributeUsage(AttributeTargets.All, AllowMultiple = false)] |
| 132 |
public class IntPropertyAttribute : Attribute |
| 133 |
{ |
| 134 |
private int _value; |
| 135 |
public int Value { get { return _value; } } |
| 136 |
|
| 137 |
public IntPropertyAttribute(int value) |
| 138 |
{ |
| 139 |
_value = value; |
| 140 |
} |
| 141 |
|
| 142 |
public IntPropertyAttribute() : this(0) { } |
| 143 |
} |
| 144 |
} |