| 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 Xunit; |
| 6 |
|
| 7 |
namespace System.Reflection.Emit.Tests |
| 8 |
{ |
| 9 |
public class ModuleBuilderCreateGlobalFunctions |
| 10 |
{ |
| 11 |
[Fact] |
| 12 |
public void CreateGlobalFunctions_SingleGlobalMethod() |
| 13 |
{ |
| 14 |
ModuleBuilder module = Helpers.DynamicModule(); |
| 15 |
MethodBuilder method = module.DefineGlobalMethod("TestMethod", MethodAttributes.Static | MethodAttributes.Public, null, null); |
| 16 |
ILGenerator ilGenerator = method.GetILGenerator(); |
| 17 |
ilGenerator.EmitWriteLine("Hello World from global method."); |
| 18 |
ilGenerator.Emit(OpCodes.Ret); |
| 19 |
|
| 20 |
module.CreateGlobalFunctions(); |
| 21 |
} |
| 22 |
|
| 23 |
[Fact] |
| 24 |
public void CreateGlobalFunctions_MultipleGlobalMethods() |
| 25 |
{ |
| 26 |
ModuleBuilder module = Helpers.DynamicModule(); |
| 27 |
MethodBuilder method = module.DefineGlobalMethod("TestMethod", MethodAttributes.Static | MethodAttributes.Public, null, null); |
| 28 |
ILGenerator ilGenerator = method.GetILGenerator(); |
| 29 |
ilGenerator.EmitWriteLine("Hello World from global method."); |
| 30 |
ilGenerator.Emit(OpCodes.Ret); |
| 31 |
|
| 32 |
method = module.DefineGlobalMethod("MyMethod2", MethodAttributes.Static | MethodAttributes.Public, |
| 33 |
null, null); |
| 34 |
ilGenerator = method.GetILGenerator(); |
| 35 |
ilGenerator.EmitWriteLine("Hello World from global method again!"); |
| 36 |
|
| 37 |
module.CreateGlobalFunctions(); |
| 38 |
} |
| 39 |
|
| 40 |
[Fact] |
| 41 |
public void CreateGlobalFunctions_CalledMultipleTimes_ThrowsInvalidOperationException() |
| 42 |
{ |
| 43 |
ModuleBuilder module = Helpers.DynamicModule(); |
| 44 |
MethodBuilder method = module.DefineGlobalMethod("TestMethod", MethodAttributes.Static | MethodAttributes.Public, null, null); |
| 45 |
ILGenerator ilGenerator = method.GetILGenerator(); |
| 46 |
ilGenerator.EmitWriteLine("Hello World from global method."); |
| 47 |
ilGenerator.Emit(OpCodes.Ret); |
| 48 |
|
| 49 |
module.CreateGlobalFunctions(); |
| 50 |
Assert.Throws<InvalidOperationException>(() => module.CreateGlobalFunctions()); |
| 51 |
} |
| 52 |
} |
| 53 |
} |