-
Notifications
You must be signed in to change notification settings - Fork 4.6k
/
EventBuilderAddOtherMethod.cs
97 lines (82 loc) · 4.4 KB
/
EventBuilderAddOtherMethod.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using Xunit;
namespace System.Reflection.Emit.Tests
{
public class EventBuilderAddOtherMethod
{
public delegate void TestEventHandler(object sender, object arg);
[Fact]
public void AddOtherMethod_AbstractVirtualMethod_Twice()
{
TypeBuilder type = Helpers.DynamicType(TypeAttributes.Abstract);
EventBuilder eventBuilder = type.DefineEvent("TestEvent", EventAttributes.None, typeof(TestEventHandler));
MethodBuilder method = type.DefineMethod("TestMethod", MethodAttributes.Abstract | MethodAttributes.Virtual);
eventBuilder.AddOtherMethod(method);
eventBuilder.AddOtherMethod(method);
}
[Fact]
public void AddOtherMethod_InstanceMethod_Twice()
{
TypeBuilder type = Helpers.DynamicType(TypeAttributes.Abstract);
EventBuilder eventBuilder = type.DefineEvent("TestEvent", EventAttributes.None, typeof(TestEventHandler));
MethodBuilder method = type.DefineMethod("TestMethod", MethodAttributes.Public);
ILGenerator ilGenerator = method.GetILGenerator();
ilGenerator.Emit(OpCodes.Ret);
eventBuilder.AddOtherMethod(method);
eventBuilder.AddOtherMethod(method);
}
[Fact]
public void AddOtherMethod_StaticMethod_Twice()
{
TypeBuilder type = Helpers.DynamicType(TypeAttributes.Abstract);
EventBuilder eventBuilder = type.DefineEvent("TestEvent", EventAttributes.None, typeof(TestEventHandler));
MethodBuilder method = type.DefineMethod("TestMethod", MethodAttributes.Static);
ILGenerator ilGenerator = method.GetILGenerator();
ilGenerator.Emit(OpCodes.Ret);
eventBuilder.AddOtherMethod(method);
eventBuilder.AddOtherMethod(method);
}
[Fact]
public void AddOtherMethod_PInvokeImpl_Twice()
{
TypeBuilder type = Helpers.DynamicType(TypeAttributes.Abstract);
EventBuilder eventBuilder = type.DefineEvent("TestEvent", EventAttributes.None, typeof(TestEventHandler));
MethodBuilder method = type.DefineMethod("TestMethod", MethodAttributes.PinvokeImpl);
eventBuilder.AddOtherMethod(method);
eventBuilder.AddOtherMethod(method);
}
[Fact]
public void AddOtherMethod_MultipleDifferentMethods()
{
TypeBuilder type = Helpers.DynamicType(TypeAttributes.Abstract);
EventBuilder eventBuilder = type.DefineEvent("TestEvent", EventAttributes.None, typeof(TestEventHandler));
MethodBuilder method1 = type.DefineMethod("PInvokeMethod", MethodAttributes.PinvokeImpl);
MethodBuilder method2 = type.DefineMethod("PublicMethod", MethodAttributes.Public);
ILGenerator ilGenerator = method2.GetILGenerator();
ilGenerator.Emit(OpCodes.Ret);
MethodBuilder method3 = type.DefineMethod("StaticMethod", MethodAttributes.Static);
MethodBuilder method4 = type.DefineMethod("AbstractMethod", MethodAttributes.Abstract | MethodAttributes.Virtual);
eventBuilder.AddOtherMethod(method1);
eventBuilder.AddOtherMethod(method2);
eventBuilder.AddOtherMethod(method3);
eventBuilder.AddOtherMethod(method4);
}
[Fact]
public void AddOtherMethod_NullMethod_ThrowsArgumentNullException()
{
TypeBuilder type = Helpers.DynamicType(TypeAttributes.Abstract);
EventBuilder eventBuilder = type.DefineEvent("TestEvent", EventAttributes.None, typeof(TestEventHandler));
AssertExtensions.Throws<ArgumentNullException>("mdBuilder", () => eventBuilder.AddOtherMethod(null));
}
[Fact]
public void AddOtherMethod_TypeCreated_ThrowsInvalidOperationException()
{
TypeBuilder type = Helpers.DynamicType(TypeAttributes.Abstract);
EventBuilder eventBuilder = type.DefineEvent("TestEvent", EventAttributes.None, typeof(TestEventHandler));
MethodBuilder method = type.DefineMethod("TestMethod", MethodAttributes.Abstract | MethodAttributes.Virtual);
type.CreateType();
Assert.Throws<InvalidOperationException>(() => eventBuilder.AddOtherMethod(method));
}
}
}