Debian Codegraph @

external/corefx/src/System.Reflection.Emit/tests/ConstructorBuilder/ConstructorBuilderSetCustomAttribute.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 ConstructorBuilderSetCustomAttribute
11 {
12 [Fact]
13 public void SetCustomAttribute_ConstructorBuilder_ByteArray()
14 {
15 TypeBuilder type = Helpers.DynamicType(TypeAttributes.Public);
16 ConstructorBuilder constructor = type.DefineConstructor(MethodAttributes.Public, CallingConventions.Standard, new Type[] { typeof(int) });
17
18 ConstructorInfo attributeConstructor = typeof(IntAllAttribute).GetConstructor(new Type[] { typeof(int) });
19 constructor.SetCustomAttribute(attributeConstructor, new byte[] { 1, 0, 5, 0, 0, 0 });
20 constructor.GetILGenerator().Emit(OpCodes.Ret);
21
22 Type createdType = type.CreateTypeInfo().AsType();
23 ConstructorInfo createdConstructor = createdType.GetConstructor(new Type[] { typeof(int) });
24 Attribute[] attributes = createdConstructor.GetCustomAttributes().ToArray();
25 IntAllAttribute attribute = Assert.IsType<IntAllAttribute>(attributes[0]);
26 Assert.Equal(5, attribute._i);
27 }
28
29 [Fact]
30 public void SetCustomAttribute_ConstructorBuilder_ByteArray_NullConstructorBuilder_ThrowsArgumentNullException()
31 {
32 TypeBuilder type = Helpers.DynamicType(TypeAttributes.Public);
33 ConstructorBuilder constructor = type.DefineConstructor(MethodAttributes.Public, CallingConventions.Standard, new Type[0]);
34 AssertExtensions.Throws<ArgumentNullException>("con", () => constructor.SetCustomAttribute(null, new byte[0]));
35 }
36
37 [Fact]
38 public void SetCustomAttribute_CustomAttributeBuilder()
39 {
40 TypeBuilder type = Helpers.DynamicType(TypeAttributes.Public);
41 ConstructorBuilder constructor = type.DefineConstructor(MethodAttributes.Public, CallingConventions.Standard, new Type[0]);
42 ILGenerator ilGenerator = constructor.GetILGenerator();
43 ilGenerator.Emit(OpCodes.Ldarg_1);
44
45 ConstructorInfo attributeConstructor = typeof(IntAllAttribute).GetConstructor(new Type[] { typeof(int) });
46 CustomAttributeBuilder attributeBuilder = new CustomAttributeBuilder(attributeConstructor, new object[] { 2 });
47
48 constructor.SetCustomAttribute(attributeBuilder);
49 Type createdType = type.CreateTypeInfo().AsType();
50
51 ConstructorInfo createdConstructor = createdType.GetConstructor(new Type[0]);
52 Attribute[] customAttributes = (Attribute[])CustomAttributeExtensions.GetCustomAttributes(createdConstructor, true).ToArray();
53
54 Assert.Equal(1, customAttributes.Length);
55 Assert.Equal(2, ((IntAllAttribute)customAttributes[0])._i);
56 }
57
58 [Fact]
59 public void SetCustomAttribute_NullCustomAttributeBuilder_ThrowsArgumentNullException()
60 {
61 TypeBuilder type = Helpers.DynamicType(TypeAttributes.Public);
62 ConstructorBuilder constructor = type.DefineConstructor(MethodAttributes.Public, CallingConventions.Standard, new Type[0]);
63 ILGenerator ilGenerator = constructor.GetILGenerator();
64 ilGenerator.Emit(OpCodes.Ldarg_1);
65
66 AssertExtensions.Throws<ArgumentNullException>("customBuilder", () => constructor.SetCustomAttribute(null));
67 }
68
69 [Fact]
70 public void GetCustomAttributes_ThrowsNotSupportedException()
71 {
72 TypeBuilder type = Helpers.DynamicType(TypeAttributes.Public);
73 ConstructorBuilder constructor = type.DefineConstructor(MethodAttributes.Public, CallingConventions.Standard, new Type[0]);
74 Assert.Throws<NotSupportedException>(() => constructor.GetCustomAttributes());
75 }
76
77 [Fact]
78 public void IsDefined_ThrowsNotSupportedException()
79 {
80 TypeBuilder type = Helpers.DynamicType(TypeAttributes.Public);
81 ConstructorBuilder constructor = type.DefineConstructor(MethodAttributes.Public, CallingConventions.Standard, new Type[0]);
82 Assert.Throws<NotSupportedException>(() => constructor.IsDefined(typeof(IntAllAttribute)));
83 }
84 }
85 }