-
Notifications
You must be signed in to change notification settings - Fork 4.6k
/
ModuleBuilderSetCustomAttribute.cs
60 lines (52 loc) · 2.64 KB
/
ModuleBuilderSetCustomAttribute.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.Linq;
using Xunit;
namespace System.Reflection.Emit.Tests
{
public class ModuleBuilderSetCustomAttribute
{
[Fact]
public void SetCustomAttribute_ConstructorInfo_ByteArray()
{
ModuleBuilder module = Helpers.DynamicModule();
ConstructorInfo attributeConstructor = typeof(IntAllAttribute).GetConstructor(new Type[] { typeof(int) });
module.SetCustomAttribute(attributeConstructor, new byte[] { 01, 00, 05, 00, 00, 00 });
object[] attributes = module.GetCustomAttributes().ToArray();
Assert.Equal(1, attributes.Length);
Assert.True(attributes[0] is IntAllAttribute);
Assert.Equal(5, ((IntAllAttribute)attributes[0])._i);
}
[Fact]
public void SetCustomAttribute_ConstructorInfo_ByteArray_NullConstructor_ThrowsArgumentNullException()
{
ModuleBuilder module = Helpers.DynamicModule();
AssertExtensions.Throws<ArgumentNullException>("con", () => module.SetCustomAttribute(null, new byte[0]));
}
[Fact]
public void SetCustomAttribute_ConstructorInfo_ByteArray_NullBinaryAttribute_ThrowsArgumentNullException()
{
ModuleBuilder module = Helpers.DynamicModule();
ConstructorInfo constructor = typeof(IntAllAttribute).GetConstructor(new Type[] { typeof(int) });
AssertExtensions.Throws<ArgumentNullException>("binaryAttribute", () => module.SetCustomAttribute(constructor, null));
}
[Fact]
public void SetCustomAttribute_CustomAttributeBuilder()
{
ModuleBuilder module = Helpers.DynamicModule();
ConstructorInfo attributeConstructor = typeof(IntAllAttribute).GetConstructor(new Type[] { typeof(int) });
CustomAttributeBuilder attributeBuilder = new CustomAttributeBuilder(attributeConstructor, new object[] { 5 });
module.SetCustomAttribute(attributeBuilder);
object[] attributes = module.GetCustomAttributes().ToArray();
Assert.Equal(1, attributes.Length);
Assert.True(attributes[0] is IntAllAttribute);
Assert.Equal(5, ((IntAllAttribute)attributes[0])._i);
}
[Fact]
public void SetCustomAttribute_CustomAttributeBuilder_NullBuilder_ThrowsArgumentNullException()
{
ModuleBuilder module = Helpers.DynamicModule();
AssertExtensions.Throws<ArgumentNullException>("customBuilder", () => module.SetCustomAttribute(null));
}
}
}