Debian Codegraph @

external/corefx/src/System.Reflection.Emit/tests/ModuleBuilder/ModuleBuilderSetCustomAttribute.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 ModuleBuilderSetCustomAttribute
11 {
12 [Fact]
13 public void SetCustomAttribute_ConstructorInfo_ByteArray()
14 {
15 ModuleBuilder module = Helpers.DynamicModule();
16 ConstructorInfo attributeConstructor = typeof(IntAllAttribute).GetConstructor(new Type[] { typeof(int) });
17 module.SetCustomAttribute(attributeConstructor, new byte[] { 01, 00, 05, 00, 00, 00 });
18
19 object[] attributes = module.GetCustomAttributes().ToArray();
20 Assert.Equal(1, attributes.Length);
21 Assert.True(attributes[0] is IntAllAttribute);
22 Assert.Equal(5, ((IntAllAttribute)attributes[0])._i);
23 }
24
25 [Fact]
26 public void SetCustomAttribute_ConstructorInfo_ByteArray_NullConstructor_ThrowsArgumentNullException()
27 {
28 ModuleBuilder module = Helpers.DynamicModule();
29 AssertExtensions.Throws<ArgumentNullException>("con", () => module.SetCustomAttribute(null, new byte[0]));
30 }
31
32 [Fact]
33 public void SetCustomAttribute_ConstructorInfo_ByteArray_NullBinaryAttribute_ThrowsArgumentNullException()
34 {
35 ModuleBuilder module = Helpers.DynamicModule();
36 ConstructorInfo constructor = typeof(IntAllAttribute).GetConstructor(new Type[] { typeof(int) });
37 AssertExtensions.Throws<ArgumentNullException>("binaryAttribute", () => module.SetCustomAttribute(constructor, null));
38 }
39
40 [Fact]
41 public void SetCustomAttribute_CustomAttributeBuilder()
42 {
43 ModuleBuilder module = Helpers.DynamicModule();
44 ConstructorInfo attributeConstructor = typeof(IntAllAttribute).GetConstructor(new Type[] { typeof(int) });
45 CustomAttributeBuilder attributeBuilder = new CustomAttributeBuilder(attributeConstructor, new object[] { 5 });
46 module.SetCustomAttribute(attributeBuilder);
47
48 object[] attributes = module.GetCustomAttributes().ToArray();
49 Assert.Equal(1, attributes.Length);
50 Assert.True(attributes[0] is IntAllAttribute);
51 Assert.Equal(5, ((IntAllAttribute)attributes[0])._i);
52 }
53
54 [Fact]
55 public void SetCustomAttribute_CustomAttributeBuilder_NullBuilder_ThrowsArgumentNullException()
56 {
57 ModuleBuilder module = Helpers.DynamicModule();
58 AssertExtensions.Throws<ArgumentNullException>("customBuilder", () => module.SetCustomAttribute(null));
59 }
60 }
61 }