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