Debian Codegraph @

external/corefx/src/System.Reflection.Emit/tests/FieldBuilder/FieldBuilderSetCustomAttribute.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 FieldBuilderSetCustomAttribute
11 {
12 [Fact]
13 public void SetCustomAttribute_ConstructorInfo_ByteArray()
14 {
15 TypeBuilder type = Helpers.DynamicType(TypeAttributes.Abstract);
16 FieldBuilder field = type.DefineField("TestField", typeof(object), FieldAttributes.Public);
17 ConstructorInfo attributeConstructor = typeof(EmptyAttribute).GetConstructor(new Type[0]);
18 byte[] bytes = Enumerable.Range(0, 256).Select(i => (byte)i).ToArray();
19
20 field.SetCustomAttribute(attributeConstructor, bytes);
21 }
22
23 [Fact]
24 public void SetCustomAttribute_ConstructorInfo_ByteArray_NullConstructorInfo_ThrowsArgumentNullException()
25 {
26 TypeBuilder type = Helpers.DynamicType(TypeAttributes.Abstract);
27 FieldBuilder field = type.DefineField("TestField", typeof(object), FieldAttributes.Public);
28 AssertExtensions.Throws<ArgumentNullException>("con", () => field.SetCustomAttribute(null, new byte[256]));
29 }
30
31 [Fact]
32 public void SetCustomAttribute_ConstructorInfo_ByteArray_NullByteArray_ThrowsArgumentNullException()
33 {
34 TypeBuilder type = Helpers.DynamicType(TypeAttributes.Abstract);
35 FieldBuilder field = type.DefineField("TestField", typeof(object), FieldAttributes.Public);
36 ConstructorInfo attributeConstructor = typeof(EmptyAttribute).GetConstructor(new Type[0]);
37 AssertExtensions.Throws<ArgumentNullException>("binaryAttribute", () => field.SetCustomAttribute(attributeConstructor, null));
38 }
39
40 [Fact]
41 public void SetCustomAttribute_ConstructorInfo_ByteArray_TypeCreated_ThrowsInvalidOperationException()
42 {
43 TypeBuilder type = Helpers.DynamicType(TypeAttributes.Abstract);
44 FieldBuilder field = type.DefineField("TestField", typeof(object), FieldAttributes.Public);
45 ConstructorInfo attributeConstructor = typeof(EmptyAttribute).GetConstructor(new Type[0]);
46 byte[] bytes = Enumerable.Range(0, 256).Select(i => (byte)i).ToArray();
47 type.CreateTypeInfo().AsType();
48
49 Assert.Throws<InvalidOperationException>(() => field.SetCustomAttribute(attributeConstructor, bytes));
50 }
51
52 [Fact]
53 public void SetCustomAttribute_CustomAttributeBuilder()
54 {
55 TypeBuilder type = Helpers.DynamicType(TypeAttributes.Abstract);
56 FieldBuilder field = type.DefineField("TestField", typeof(object), FieldAttributes.Public);
57 ConstructorInfo attributeConstructor = typeof(EmptyAttribute).GetConstructor(new Type[0]);
58 CustomAttributeBuilder attribute = new CustomAttributeBuilder(attributeConstructor, new object[0]);
59
60 field.SetCustomAttribute(attribute);
61 }
62
63 [Fact]
64 public void SetCustomAttribute_CustomAttributeBuilder_NullBuilder_ThrowsArgumentNullException()
65 {
66 TypeBuilder type = Helpers.DynamicType(TypeAttributes.Abstract);
67 FieldBuilder field = type.DefineField("TestField", typeof(object), FieldAttributes.Public);
68 AssertExtensions.Throws<ArgumentNullException>("customBuilder", () => field.SetCustomAttribute(null));
69 }
70
71 [Fact]
72 public void SetCustomAttribute_CustomAttributeBuilder_TypeCreated_ThrowsInvalidOperationException()
73 {
74 TypeBuilder type = Helpers.DynamicType(TypeAttributes.Abstract);
75 FieldBuilder field = type.DefineField("TestField", typeof(object), FieldAttributes.Public);
76 ConstructorInfo con = typeof(EmptyAttribute).GetConstructor(new Type[0]);
77 CustomAttributeBuilder attribute = new CustomAttributeBuilder(con, new object[0]);
78 type.CreateTypeInfo().AsType();
79
80 Assert.Throws<InvalidOperationException>(() => { field.SetCustomAttribute(attribute); });
81 }
82 }
83 }