Debian Codegraph @

external/corefx/src/System.Reflection.Emit/tests/MethodBuilder/MethodBuilderSetCustomAttribute.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 MethodBuilderCustomIntAttribute : Attribute
11 {
12 public MethodBuilderCustomIntAttribute(int intField)
13 {
14 _intField = intField;
15 }
16
17 public string Field12345;
18 public int _intField;
19 }
20
21 public class MethodBuilderCustomAttribute : Attribute
22 {
23 public string TestString
24 {
25 get { return TestStringField; }
26 set { TestStringField = value; }
27 }
28
29 public int TestInt32
30 {
31 get { return TestInt; }
32 set { TestInt = value; }
33 }
34
35 public string GetOnlyString
36 {
37 get { return GetString; }
38 }
39
40 public int GetOnlyInt32
41 {
42 get { return GetInt; }
43 }
44
45 public string TestStringField;
46 public int TestInt;
47 public string GetString;
48 public int GetInt;
49
50 public MethodBuilderCustomAttribute() { }
51
52 public MethodBuilderCustomAttribute(string getOnlyString, int getOnlyInt32)
53 {
54 GetString = getOnlyString;
55 GetInt = getOnlyInt32;
56 }
57
58 public MethodBuilderCustomAttribute(string testString, int testInt32, string getOnlyString, int getOnlyInt32)
59 {
60 TestStringField = testString;
61 TestInt = testInt32;
62 GetString = getOnlyString;
63 GetInt = getOnlyInt32;
64 }
65 }
66
67 public class MethodBuilderSetCustomAttribute
68 {
69 [Fact]
70 public void SetCustomAttribute_CustomAttributeBuilder()
71 {
72 TypeBuilder type = Helpers.DynamicType(TypeAttributes.NotPublic);
73 MethodBuilder method = type.DefineMethod("TestMethod", MethodAttributes.Public | MethodAttributes.Static);
74
75 ILGenerator ilgen = method.GetILGenerator();
76 ilgen.Emit(OpCodes.Ldc_I4, 100);
77 ilgen.Emit(OpCodes.Ret);
78
79 Type attributeType = typeof(MethodBuilderCustomIntAttribute);
80 ConstructorInfo constructor = attributeType.GetConstructors()[0];
81 FieldInfo field = attributeType.GetField("Field12345");
82
83 CustomAttributeBuilder attribute = new CustomAttributeBuilder(constructor, new object[] { 4 }, new FieldInfo[] { field }, new object[] { "hello" });
84 method.SetCustomAttribute(attribute);
85 Type createdType = type.CreateTypeInfo().AsType();
86
87 object[] attributes = createdType.GetMethod("TestMethod").GetCustomAttributes(false).ToArray();
88 Assert.Equal(1, attributes.Length);
89
90 MethodBuilderCustomIntAttribute obj = (MethodBuilderCustomIntAttribute)attributes[0];
91 Assert.Equal("hello", obj.Field12345);
92 Assert.Equal(4, obj._intField);
93 }
94
95 [Fact]
96 public void SetCustomAttribute_CustomAttributeBuilder_NullBuilder_ThrowsArgumentNullException()
97 {
98 TypeBuilder type = Helpers.DynamicType(TypeAttributes.Public);
99 MethodBuilder method = type.DefineMethod("method1", MethodAttributes.Public | MethodAttributes.Static);
100
101 AssertExtensions.Throws<ArgumentNullException>("customBuilder", () => method.SetCustomAttribute(null));
102 }
103
104 [Fact]
105 public void SetCustomAttribute_ConstructorInfo_ByteArray()
106 {
107 byte[] binaryAttribute = Enumerable.Range(0, 128).Select(i => (byte)i).ToArray();
108
109 TypeBuilder type = Helpers.DynamicType(TypeAttributes.Abstract);
110 MethodBuilder method = type.DefineMethod("TestMethod", MethodAttributes.Public);
111
112 method.SetCustomAttribute(typeof(MethodBuilderCustomAttribute).GetConstructor(new Type[0]), binaryAttribute);
113 }
114
115 [Fact]
116 public void SetCustomAttribute_ConstructorInfo_ByteArray_NullConstructor_ThrowsArgumentNullException()
117 {
118 TypeBuilder type = Helpers.DynamicType(TypeAttributes.Abstract);
119 MethodBuilder method = type.DefineMethod("TestMethod", MethodAttributes.Public);
120
121 AssertExtensions.Throws<ArgumentNullException>("con", () => method.SetCustomAttribute(null, new byte[0]));
122 }
123
124 [Fact]
125 public void SetCustomAttribute_ConstructorInfo_ByteArray_NullBinaryAttribute_ThrowsArgumentNullException()
126 {
127 TypeBuilder type = Helpers.DynamicType(TypeAttributes.Abstract);
128 MethodBuilder builder = type.DefineMethod("TestMethod", MethodAttributes.Public);
129
130 AssertExtensions.Throws<ArgumentNullException>("binaryAttribute", () => builder.SetCustomAttribute(typeof(MethodBuilderCustomAttribute).GetConstructor(new Type[0]), null));
131 }
132 }
133 }