From ce7a16e6ec8d0691ca113612371bfa6927eb4289 Mon Sep 17 00:00:00 2001 From: dotnet-bot Date: Thu, 21 May 2015 17:30:29 -0700 Subject: [PATCH] Initial commit of System.Reflection.Emit.Lightweight --- .../System.Reflection.Emit.Lightweight.sln | 22 + .../tests/DynamicMethodAttributes.cs | 44 ++ .../tests/DynamicMethodCallingConvention.cs | 23 + .../tests/DynamicMethodCreateDelegate1.cs | 443 ++++++++++++++++++ .../tests/DynamicMethodCreateDelegate2.cs | 280 +++++++++++ .../tests/DynamicMethodDeclaringType.cs | 27 ++ .../tests/DynamicMethodGetBaseDefinition.cs | 133 ++++++ .../tests/DynamicMethodGetILGenerator1.cs | 184 ++++++++ .../tests/DynamicMethodGetILGenerator2.cs | 178 +++++++ .../tests/DynamicMethodGetParameters.cs | 78 +++ .../tests/DynamicMethodInitLocals.cs | 42 ++ .../tests/DynamicMethodModule.cs | 43 ++ .../tests/DynamicMethodName.cs | 21 + .../tests/DynamicMethodReturnParameter.cs | 27 ++ .../tests/DynamicMethodReturnType.cs | 75 +++ .../tests/DynamicMethodToString.cs | 59 +++ .../tests/DynamicMethodctor1.cs | 97 ++++ .../tests/DynamicMethodctor2.cs | 142 ++++++ .../tests/DynamicMethodctor3.cs | 114 +++++ .../tests/DynamicMethodctor4.cs | 167 +++++++ .../tests/DynamicMethodctor5.cs | 149 ++++++ .../tests/DynamicMethodctor6.cs | 201 ++++++++ ...m.Reflection.Emit.Lightweight.Tests.csproj | 45 ++ .../tests/Utilities.cs | 13 + .../tests/project.json | 18 + 25 files changed, 2625 insertions(+) create mode 100644 src/System.Reflection.Emit.Lightweight/System.Reflection.Emit.Lightweight.sln create mode 100644 src/System.Reflection.Emit.Lightweight/tests/DynamicMethodAttributes.cs create mode 100644 src/System.Reflection.Emit.Lightweight/tests/DynamicMethodCallingConvention.cs create mode 100644 src/System.Reflection.Emit.Lightweight/tests/DynamicMethodCreateDelegate1.cs create mode 100644 src/System.Reflection.Emit.Lightweight/tests/DynamicMethodCreateDelegate2.cs create mode 100644 src/System.Reflection.Emit.Lightweight/tests/DynamicMethodDeclaringType.cs create mode 100644 src/System.Reflection.Emit.Lightweight/tests/DynamicMethodGetBaseDefinition.cs create mode 100644 src/System.Reflection.Emit.Lightweight/tests/DynamicMethodGetILGenerator1.cs create mode 100644 src/System.Reflection.Emit.Lightweight/tests/DynamicMethodGetILGenerator2.cs create mode 100644 src/System.Reflection.Emit.Lightweight/tests/DynamicMethodGetParameters.cs create mode 100644 src/System.Reflection.Emit.Lightweight/tests/DynamicMethodInitLocals.cs create mode 100644 src/System.Reflection.Emit.Lightweight/tests/DynamicMethodModule.cs create mode 100644 src/System.Reflection.Emit.Lightweight/tests/DynamicMethodName.cs create mode 100644 src/System.Reflection.Emit.Lightweight/tests/DynamicMethodReturnParameter.cs create mode 100644 src/System.Reflection.Emit.Lightweight/tests/DynamicMethodReturnType.cs create mode 100644 src/System.Reflection.Emit.Lightweight/tests/DynamicMethodToString.cs create mode 100644 src/System.Reflection.Emit.Lightweight/tests/DynamicMethodctor1.cs create mode 100644 src/System.Reflection.Emit.Lightweight/tests/DynamicMethodctor2.cs create mode 100644 src/System.Reflection.Emit.Lightweight/tests/DynamicMethodctor3.cs create mode 100644 src/System.Reflection.Emit.Lightweight/tests/DynamicMethodctor4.cs create mode 100644 src/System.Reflection.Emit.Lightweight/tests/DynamicMethodctor5.cs create mode 100644 src/System.Reflection.Emit.Lightweight/tests/DynamicMethodctor6.cs create mode 100644 src/System.Reflection.Emit.Lightweight/tests/System.Reflection.Emit.Lightweight.Tests.csproj create mode 100644 src/System.Reflection.Emit.Lightweight/tests/Utilities.cs create mode 100644 src/System.Reflection.Emit.Lightweight/tests/project.json diff --git a/src/System.Reflection.Emit.Lightweight/System.Reflection.Emit.Lightweight.sln b/src/System.Reflection.Emit.Lightweight/System.Reflection.Emit.Lightweight.sln new file mode 100644 index 000000000000..e2dfc3242872 --- /dev/null +++ b/src/System.Reflection.Emit.Lightweight/System.Reflection.Emit.Lightweight.sln @@ -0,0 +1,22 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio 14 +VisualStudioVersion = 14.0.22609.0 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "System.Reflection.Emit.Lightweight.Tests", "tests\System.Reflection.Emit.Lightweight.Tests.csproj", "{C338DCF7-FB75-407B-A2ED-117FBF3AAA18}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {C338DCF7-FB75-407B-A2ED-117FBF3AAA18}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {C338DCF7-FB75-407B-A2ED-117FBF3AAA18}.Debug|Any CPU.Build.0 = Debug|Any CPU + {C338DCF7-FB75-407B-A2ED-117FBF3AAA18}.Release|Any CPU.ActiveCfg = Release|Any CPU + {C338DCF7-FB75-407B-A2ED-117FBF3AAA18}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection +EndGlobal diff --git a/src/System.Reflection.Emit.Lightweight/tests/DynamicMethodAttributes.cs b/src/System.Reflection.Emit.Lightweight/tests/DynamicMethodAttributes.cs new file mode 100644 index 000000000000..963d4059b2d0 --- /dev/null +++ b/src/System.Reflection.Emit.Lightweight/tests/DynamicMethodAttributes.cs @@ -0,0 +1,44 @@ +// Copyright (c) Microsoft. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. + +using Xunit; + +namespace System.Reflection.Emit.Lightweight.Tests +{ + public class DynamicMethodAttributesTests + { + public const string c_DYNAMICMETHODNAME = "MethodName"; + + [Fact] + public void PosTest1() + { + DynamicMethod dynamicmethod = new DynamicMethod(c_DYNAMICMETHODNAME, MethodAttributes.Static | MethodAttributes.Public, CallingConventions.Standard, null, null, typeof(TestClass).GetTypeInfo().Module, true); + MethodAttributes methodattribute = dynamicmethod.Attributes; + Assert.False(methodattribute != (MethodAttributes.Public | MethodAttributes.Static)); + } + + [Fact] + public void PosTest2() + { + DynamicMethod dynamicmethod = new DynamicMethod(c_DYNAMICMETHODNAME, MethodAttributes.Static | MethodAttributes.Family | MethodAttributes.FamANDAssem, CallingConventions.Standard, null, null, typeof(TestClass).GetTypeInfo().Module, true); + MethodAttributes methodattribute = dynamicmethod.Attributes; + Assert.False(methodattribute != (MethodAttributes.Public | MethodAttributes.Static)); + } + } + + public class TestClass + { + } + + public class GenClass1 + { + } + + public class GenClass2 + { + } + + public interface TestInter + { + } +} \ No newline at end of file diff --git a/src/System.Reflection.Emit.Lightweight/tests/DynamicMethodCallingConvention.cs b/src/System.Reflection.Emit.Lightweight/tests/DynamicMethodCallingConvention.cs new file mode 100644 index 000000000000..9be8771d18d4 --- /dev/null +++ b/src/System.Reflection.Emit.Lightweight/tests/DynamicMethodCallingConvention.cs @@ -0,0 +1,23 @@ +// Copyright (c) Microsoft. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. + +using System; +using System.Reflection; +using System.Reflection.Emit; +using Xunit; + +namespace System.Reflection.Emit.Lightweight.Tests +{ + public class DynamicMethodCallingConventionTests + { + public const string c_DYNAMICMETHODNAME = "MethodName"; + + [Fact] + public void PosTest1() + { + DynamicMethod dynamicmethod = new DynamicMethod(c_DYNAMICMETHODNAME, MethodAttributes.Static | MethodAttributes.Public, CallingConventions.Standard, null, null, typeof(TestClass).GetTypeInfo().Module, true); + CallingConventions callingconvertion = dynamicmethod.CallingConvention; + Assert.False(callingconvertion != CallingConventions.Standard); + } + } +} \ No newline at end of file diff --git a/src/System.Reflection.Emit.Lightweight/tests/DynamicMethodCreateDelegate1.cs b/src/System.Reflection.Emit.Lightweight/tests/DynamicMethodCreateDelegate1.cs new file mode 100644 index 000000000000..03781a9e9520 --- /dev/null +++ b/src/System.Reflection.Emit.Lightweight/tests/DynamicMethodCreateDelegate1.cs @@ -0,0 +1,443 @@ +// Copyright (c) Microsoft. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. + +using System; +using System.Reflection; +using System.Reflection.Emit; +using Xunit; + +namespace System.Reflection.Emit.Lightweight.Tests +{ + public class DynamicMethodCreateDelegateTests + { + private const string c_DYNAMIC_METHOD_NAME = "TestDynamicMethodA"; + private readonly Type _DYNAMIC_METHOD_OWNER_TYPE = typeof(TestCreateDelegateOwner1); + private readonly Type _DYNAMIC_METHOD_OWNER_DERIVED_TYPE = typeof(TestCreateDelegateOwner1Derived); + private const string c_FIELD_NAME = "_id"; + + public Module CurrentModule + { + get + { + return typeof(DynamicMethodCreateDelegateTests).GetTypeInfo().Assembly.ManifestModule; + } + } + + [Fact] + public void PosTest1() + { + DynamicMethod testDynMethod; + Type retType; + Type[] paramTypes; + FieldInfo fieldInfo; + TestCreateDelegateOwner1 target = new TestCreateDelegateOwner1(); + int newId = 100; + + bool actualResult; + retType = typeof(int); + paramTypes = new Type[] + { + _DYNAMIC_METHOD_OWNER_TYPE, + typeof(int) + }; + + fieldInfo = _DYNAMIC_METHOD_OWNER_TYPE.GetField( + c_FIELD_NAME, + BindingFlags.NonPublic | + BindingFlags.Instance); + + testDynMethod = new DynamicMethod(c_DYNAMIC_METHOD_NAME, + retType, + paramTypes, + _DYNAMIC_METHOD_OWNER_TYPE); + + ILGenerator testDynMethodIL = testDynMethod.GetILGenerator(); + + testDynMethodIL.Emit(OpCodes.Ldarg_0); + testDynMethodIL.Emit(OpCodes.Ldfld, fieldInfo); + + testDynMethodIL.Emit(OpCodes.Ldarg_0); + testDynMethodIL.Emit(OpCodes.Ldarg_1); + testDynMethodIL.Emit(OpCodes.Stfld, fieldInfo); + + testDynMethodIL.Emit(OpCodes.Ret); + + UseLikeInstance instanceCallBack = (UseLikeInstance)testDynMethod.CreateDelegate( + typeof(UseLikeInstance), + target); + actualResult = target.ID == instanceCallBack(newId); + actualResult = (target.ID == newId) && actualResult; + Assert.True(actualResult, "Failed to create delegate for dynamic method."); + } + + [Fact] + public void PosTest2() + { + DynamicMethod testDynMethod; + Type retType; + Type[] paramTypes; + FieldInfo fieldInfo; + TestCreateDelegateOwner1Derived target = new TestCreateDelegateOwner1Derived(); + int newId = 100; + + bool actualResult; + + retType = typeof(int); + paramTypes = new Type[] + { + _DYNAMIC_METHOD_OWNER_TYPE, + typeof(int) + }; + + fieldInfo = _DYNAMIC_METHOD_OWNER_TYPE.GetField( + c_FIELD_NAME, + BindingFlags.NonPublic | + BindingFlags.Instance); + + testDynMethod = new DynamicMethod(c_DYNAMIC_METHOD_NAME, + retType, + paramTypes, + _DYNAMIC_METHOD_OWNER_TYPE); + + ILGenerator testDynMethodIL = testDynMethod.GetILGenerator(); + + testDynMethodIL.Emit(OpCodes.Ldarg_0); + testDynMethodIL.Emit(OpCodes.Ldfld, fieldInfo); + + testDynMethodIL.Emit(OpCodes.Ldarg_0); + testDynMethodIL.Emit(OpCodes.Ldarg_1); + testDynMethodIL.Emit(OpCodes.Stfld, fieldInfo); + + testDynMethodIL.Emit(OpCodes.Ret); + + UseLikeInstance instanceCallBack = (UseLikeInstance)testDynMethod.CreateDelegate( + typeof(UseLikeInstance), + target); + actualResult = target.ID == instanceCallBack(newId); + actualResult = (target.ID == newId) && actualResult; + Assert.True(actualResult, "Failed to create delegate for dynamic method"); + } + + [Fact] + public void PosTest3() + { + DynamicMethod testDynMethod; + Type retType; + Type[] paramTypes; + FieldInfo fieldInfo; + TestCreateDelegateOwner1 target = new TestCreateDelegateOwner1(); + int newId = 100; + + bool actualResult; + + retType = typeof(int); + paramTypes = new Type[] + { + _DYNAMIC_METHOD_OWNER_TYPE, + typeof(int) + }; + + fieldInfo = _DYNAMIC_METHOD_OWNER_TYPE.GetField( + c_FIELD_NAME, + BindingFlags.NonPublic | + BindingFlags.Instance); + + testDynMethod = new DynamicMethod(c_DYNAMIC_METHOD_NAME, + retType, + paramTypes, + this.CurrentModule, + true); + + ILGenerator testDynMethodIL = testDynMethod.GetILGenerator(); + + testDynMethodIL.Emit(OpCodes.Ldarg_0); + testDynMethodIL.Emit(OpCodes.Ldfld, fieldInfo); + + testDynMethodIL.Emit(OpCodes.Ldarg_0); + testDynMethodIL.Emit(OpCodes.Ldarg_1); + testDynMethodIL.Emit(OpCodes.Stfld, fieldInfo); + + testDynMethodIL.Emit(OpCodes.Ret); + + UseLikeInstance instanceCallBack = (UseLikeInstance)testDynMethod.CreateDelegate( + typeof(UseLikeInstance), + target); + actualResult = target.ID == instanceCallBack(newId); + actualResult = (target.ID == newId) && actualResult; + Assert.True(actualResult, "Failed to create delegate for dynamic method"); + } + + [Fact] + public void PosTest4() + { + DynamicMethod testDynMethod; + Type retType; + Type[] paramTypes; + FieldInfo fieldInfo; + TestCreateDelegateOwner1Derived target = new TestCreateDelegateOwner1Derived(); + int newId = 100; + + bool actualResult; + retType = typeof(int); + paramTypes = new Type[] + { + _DYNAMIC_METHOD_OWNER_TYPE, + typeof(int) + }; + + fieldInfo = _DYNAMIC_METHOD_OWNER_TYPE.GetField( + c_FIELD_NAME, + BindingFlags.NonPublic | + BindingFlags.Instance); + + testDynMethod = new DynamicMethod(c_DYNAMIC_METHOD_NAME, + retType, + paramTypes, + _DYNAMIC_METHOD_OWNER_DERIVED_TYPE, + true); + + ILGenerator testDynMethodIL = testDynMethod.GetILGenerator(); + + testDynMethodIL.Emit(OpCodes.Ldarg_0); + testDynMethodIL.Emit(OpCodes.Ldfld, fieldInfo); + + testDynMethodIL.Emit(OpCodes.Ldarg_0); + testDynMethodIL.Emit(OpCodes.Ldarg_1); + testDynMethodIL.Emit(OpCodes.Stfld, fieldInfo); + + testDynMethodIL.Emit(OpCodes.Ret); + + UseLikeInstance instanceCallBack = (UseLikeInstance)testDynMethod.CreateDelegate( + typeof(UseLikeInstance), + target); + actualResult = target.ID == instanceCallBack(newId); + actualResult = (target.ID == newId) && actualResult; + Assert.True(actualResult, "Failed to create delegate for dynamic method."); + } + + [Fact] + public void NegTest1() + { + DynamicMethod testDynMethod; + Type retType; + Type[] paramTypes; + TestCreateDelegateOwner1 target = new TestCreateDelegateOwner1(); + retType = typeof(int); + paramTypes = new Type[] + { + _DYNAMIC_METHOD_OWNER_TYPE, + typeof(int) + }; + + testDynMethod = new DynamicMethod(c_DYNAMIC_METHOD_NAME, + retType, + paramTypes, + _DYNAMIC_METHOD_OWNER_TYPE); + + Assert.Throws(() => + { + UseLikeInstance instanceCallBack = (UseLikeInstance)testDynMethod.CreateDelegate(typeof(UseLikeInstance), target); + }); + } + + [Fact] + public void NegTest2() + { + DynamicMethod testDynMethod; + Type retType; + Type[] paramTypes; + FieldInfo fieldInfo; + string target = "foo"; + + retType = typeof(int); + paramTypes = new Type[] + { + _DYNAMIC_METHOD_OWNER_TYPE, + typeof(int) + }; + + fieldInfo = _DYNAMIC_METHOD_OWNER_TYPE.GetField( + c_FIELD_NAME, + BindingFlags.NonPublic | + BindingFlags.Instance); + + testDynMethod = new DynamicMethod(c_DYNAMIC_METHOD_NAME, + retType, + paramTypes, + _DYNAMIC_METHOD_OWNER_TYPE); + + ILGenerator testDynMethodIL = testDynMethod.GetILGenerator(); + + testDynMethodIL.Emit(OpCodes.Ldarg_0); + testDynMethodIL.Emit(OpCodes.Ldfld, fieldInfo); + + testDynMethodIL.Emit(OpCodes.Ldarg_0); + testDynMethodIL.Emit(OpCodes.Ldarg_1); + testDynMethodIL.Emit(OpCodes.Stfld, fieldInfo); + + testDynMethodIL.Emit(OpCodes.Ret); + + Assert.Throws(() => + { + UseLikeInstance instanceCallBack = (UseLikeInstance)testDynMethod.CreateDelegate(typeof(UseLikeInstance), target); + }); + } + + [Fact] + public void NegTest3() + { + DynamicMethod testDynMethod; + Type retType; + Type[] paramTypes; + FieldInfo fieldInfo; + TestCreateDelegateOwner1 target = new TestCreateDelegateOwner1(); + + retType = typeof(int); + paramTypes = new Type[] + { + _DYNAMIC_METHOD_OWNER_TYPE, + typeof(int) + }; + + fieldInfo = _DYNAMIC_METHOD_OWNER_TYPE.GetField( + c_FIELD_NAME, + BindingFlags.NonPublic | + BindingFlags.Instance); + + testDynMethod = new DynamicMethod(c_DYNAMIC_METHOD_NAME, + retType, + paramTypes, + _DYNAMIC_METHOD_OWNER_TYPE); + + ILGenerator testDynMethodIL = testDynMethod.GetILGenerator(); + + testDynMethodIL.Emit(OpCodes.Ldarg_0); + testDynMethodIL.Emit(OpCodes.Ldfld, fieldInfo); + + testDynMethodIL.Emit(OpCodes.Ldarg_0); + testDynMethodIL.Emit(OpCodes.Ldarg_1); + testDynMethodIL.Emit(OpCodes.Stfld, fieldInfo); + + testDynMethodIL.Emit(OpCodes.Ret); + + + Assert.Throws(() => { InvalidRetType invalidCallBack = (InvalidRetType)testDynMethod.CreateDelegate(typeof(InvalidRetType), target); }); + } + + [Fact] + public void NegTest4() + { + DynamicMethod testDynMethod; + Type retType; + Type[] paramTypes; + FieldInfo fieldInfo; + TestCreateDelegateOwner1 target = new TestCreateDelegateOwner1(); + + retType = typeof(int); + paramTypes = new Type[] + { + _DYNAMIC_METHOD_OWNER_TYPE, + typeof(int) + }; + + fieldInfo = _DYNAMIC_METHOD_OWNER_TYPE.GetField( + c_FIELD_NAME, + BindingFlags.NonPublic | + BindingFlags.Instance); + + testDynMethod = new DynamicMethod(c_DYNAMIC_METHOD_NAME, + retType, + paramTypes, + _DYNAMIC_METHOD_OWNER_TYPE); + + ILGenerator testDynMethodIL = testDynMethod.GetILGenerator(); + + testDynMethodIL.Emit(OpCodes.Ldarg_0); + testDynMethodIL.Emit(OpCodes.Ldfld, fieldInfo); + + testDynMethodIL.Emit(OpCodes.Ldarg_0); + testDynMethodIL.Emit(OpCodes.Ldarg_1); + testDynMethodIL.Emit(OpCodes.Stfld, fieldInfo); + + testDynMethodIL.Emit(OpCodes.Ret); + + Assert.Throws(() => { WrongParamNumber invalidCallBack = (WrongParamNumber)testDynMethod.CreateDelegate(typeof(WrongParamNumber), target); }); + } + + [Fact] + public void NegTest5() + { + DynamicMethod testDynMethod; + Type retType; + Type[] paramTypes; + FieldInfo fieldInfo; + TestCreateDelegateOwner1 target = new TestCreateDelegateOwner1(); + + retType = typeof(int); + paramTypes = new Type[] + { + _DYNAMIC_METHOD_OWNER_TYPE, + typeof(int) + }; + + fieldInfo = _DYNAMIC_METHOD_OWNER_TYPE.GetField( + c_FIELD_NAME, + BindingFlags.NonPublic | + BindingFlags.Instance); + + testDynMethod = new DynamicMethod(c_DYNAMIC_METHOD_NAME, + retType, + paramTypes, + _DYNAMIC_METHOD_OWNER_TYPE); + + ILGenerator testDynMethodIL = testDynMethod.GetILGenerator(); + + testDynMethodIL.Emit(OpCodes.Ldarg_0); + testDynMethodIL.Emit(OpCodes.Ldfld, fieldInfo); + + testDynMethodIL.Emit(OpCodes.Ldarg_0); + testDynMethodIL.Emit(OpCodes.Ldarg_1); + testDynMethodIL.Emit(OpCodes.Stfld, fieldInfo); + + testDynMethodIL.Emit(OpCodes.Ret); + + Assert.Throws(() => { InvalidParamType invalidCallBack = (InvalidParamType)testDynMethod.CreateDelegate(typeof(InvalidParamType), target); }); + } + } + + internal class TestCreateDelegateOwner1 + { + private int _id; //c_FIELD_NAME + + public TestCreateDelegateOwner1(int id) + { + _id = id; + } + public TestCreateDelegateOwner1() : this(0) + { + } + + public int ID + { + get { return _id; } + } + } + + internal class TestCreateDelegateOwner1Derived : TestCreateDelegateOwner1 + { + public TestCreateDelegateOwner1Derived(int id) + : base(id) + { + } + + public TestCreateDelegateOwner1Derived() + : base() + { + } + } + + internal delegate int UseLikeInstance(int id); + internal delegate TestCreateDelegateOwner1 InvalidRetType(int id); + internal delegate int WrongParamNumber(int id, int m); + internal delegate int InvalidParamType(TestCreateDelegateOwner1 owner); +} \ No newline at end of file diff --git a/src/System.Reflection.Emit.Lightweight/tests/DynamicMethodCreateDelegate2.cs b/src/System.Reflection.Emit.Lightweight/tests/DynamicMethodCreateDelegate2.cs new file mode 100644 index 000000000000..91f83b8e4281 --- /dev/null +++ b/src/System.Reflection.Emit.Lightweight/tests/DynamicMethodCreateDelegate2.cs @@ -0,0 +1,280 @@ +// Copyright (c) Microsoft. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. + +using System; +using System.Reflection; +using System.Reflection.Emit; +using Xunit; + +namespace System.Reflection.Emit.Lightweight.Tests +{ + public class DynamicMethodCreateDelegateStaticTests + { + private const string c_DYNAMIC_METHOD_NAME = "TestDynamicMethodA"; + private readonly Type _DYNAMIC_METHOD_OWNER_TYPE = typeof(TestCreateDelegateOwner2); + private readonly Type _DYNAMIC_METHOD_OWNER_DERIVED_TYPE = typeof(TestCreateDelegateOwner2Derived); + private const string c_FIELD_NAME = "_id"; + + public Module CurrentModule + { + get + { + return typeof(DynamicMethodCreateDelegateStaticTests).GetTypeInfo().Assembly.ManifestModule; + } + } + + [Fact] + public void PosTest1() + { + DynamicMethod testDynMethod; + FieldInfo fieldInfo; + TestCreateDelegateOwner2 target = new TestCreateDelegateOwner2(); + int newId = 100; + + bool actualResult; + + fieldInfo = _DYNAMIC_METHOD_OWNER_TYPE.GetField( + c_FIELD_NAME, + BindingFlags.NonPublic | + BindingFlags.Instance); + + testDynMethod = this.CreateDynMethod(_DYNAMIC_METHOD_OWNER_TYPE); + this.EmitDynMethodBody(testDynMethod, fieldInfo); + + UseLikeStatic2 staticCallBack = (UseLikeStatic2)testDynMethod.CreateDelegate(typeof(UseLikeStatic2)); + actualResult = target.ID == staticCallBack(target, newId); + actualResult = (target.ID == newId) && actualResult; + Assert.True(actualResult, "Failed to create delegate for dynamic method."); + } + + [Fact] + public void PosTest2() + { + DynamicMethod testDynMethod; + FieldInfo fieldInfo; + TestCreateDelegateOwner2Derived target = new TestCreateDelegateOwner2Derived(); + int newId = 100; + + bool actualResult; + + fieldInfo = _DYNAMIC_METHOD_OWNER_TYPE.GetField( + c_FIELD_NAME, + BindingFlags.NonPublic | + BindingFlags.Instance); + + testDynMethod = this.CreateDynMethod(_DYNAMIC_METHOD_OWNER_TYPE); + this.EmitDynMethodBody(testDynMethod, fieldInfo); + + UseLikeStatic2 staticCallBack = (UseLikeStatic2)testDynMethod.CreateDelegate(typeof(UseLikeStatic2)); + actualResult = target.ID == staticCallBack(target, newId); + actualResult = (target.ID == newId) && actualResult; + Assert.True(actualResult, "Failed to create delegate for dynamic method."); + } + + [Fact] + public void PosTest3() + { + DynamicMethod testDynMethod; + FieldInfo fieldInfo; + TestCreateDelegateOwner2 target = new TestCreateDelegateOwner2(); + int newId = 100; + + bool actualResult; + fieldInfo = _DYNAMIC_METHOD_OWNER_TYPE.GetField( + c_FIELD_NAME, + BindingFlags.NonPublic | + BindingFlags.Instance); + + testDynMethod = this.CreateDynMethod(this.CurrentModule); + this.EmitDynMethodBody(testDynMethod, fieldInfo); + + UseLikeStatic2 staticCallBack = (UseLikeStatic2)testDynMethod.CreateDelegate(typeof(UseLikeStatic2)); + actualResult = target.ID == staticCallBack(target, newId); + actualResult = (target.ID == newId) && actualResult; + + Assert.True(actualResult, "Failed to create delegate for dynamic method."); + } + + [Fact] + public void PosTest4() + { + DynamicMethod testDynMethod; + FieldInfo fieldInfo; + TestCreateDelegateOwner2Derived target = new TestCreateDelegateOwner2Derived(); + int newId = 100; + + bool actualResult; + + fieldInfo = _DYNAMIC_METHOD_OWNER_TYPE.GetField( + c_FIELD_NAME, + BindingFlags.NonPublic | + BindingFlags.Instance); + + testDynMethod = this.CreateDynMethod(this.CurrentModule); + this.EmitDynMethodBody(testDynMethod, fieldInfo); + + UseLikeStatic2 staticCallBack = (UseLikeStatic2)testDynMethod.CreateDelegate(typeof(UseLikeStatic2)); + actualResult = target.ID == staticCallBack(target, newId); + actualResult = (target.ID == newId) && actualResult; + Assert.True(actualResult, "Failed to create delegate for dynamic method."); + } + + [Fact] + public void NegTest1() + { + DynamicMethod testDynMethod; + TestCreateDelegateOwner2 target = new TestCreateDelegateOwner2(); + + testDynMethod = this.CreateDynMethod(_DYNAMIC_METHOD_OWNER_TYPE); + Assert.Throws(() => + { + UseLikeStatic2 staticCallBack = (UseLikeStatic2)testDynMethod.CreateDelegate(typeof(UseLikeStatic2)); + }); + } + + [Fact] + public void NegTest2() + { + DynamicMethod testDynMethod; + FieldInfo fieldInfo; + TestCreateDelegateOwner2 target = new TestCreateDelegateOwner2(); + + + fieldInfo = _DYNAMIC_METHOD_OWNER_TYPE.GetField( + c_FIELD_NAME, + BindingFlags.NonPublic | + BindingFlags.Instance); + + testDynMethod = this.CreateDynMethod(_DYNAMIC_METHOD_OWNER_TYPE); + this.EmitDynMethodBody(testDynMethod, fieldInfo); + + Assert.Throws(() => + { + InvalidRetType2 invalidCallBack = (InvalidRetType2)testDynMethod.CreateDelegate(typeof(InvalidRetType2)); + }); + } + + [Fact] + public void NegTest3() + { + DynamicMethod testDynMethod; + FieldInfo fieldInfo; + TestCreateDelegateOwner2 target = new TestCreateDelegateOwner2(); + + fieldInfo = _DYNAMIC_METHOD_OWNER_TYPE.GetField( + c_FIELD_NAME, + BindingFlags.NonPublic | + BindingFlags.Instance); + + testDynMethod = this.CreateDynMethod(_DYNAMIC_METHOD_OWNER_TYPE); + this.EmitDynMethodBody(testDynMethod, fieldInfo); + + Assert.Throws(() => + { + WrongParamNumber2 invalidCallBack = (WrongParamNumber2)testDynMethod.CreateDelegate(typeof(WrongParamNumber2)); + }); + } + + [Fact] + public void NegTest4() + { + DynamicMethod testDynMethod; + FieldInfo fieldInfo; + TestCreateDelegateOwner2 target = new TestCreateDelegateOwner2(); + + fieldInfo = _DYNAMIC_METHOD_OWNER_TYPE.GetField( + c_FIELD_NAME, + BindingFlags.NonPublic | + BindingFlags.Instance); + + testDynMethod = this.CreateDynMethod(_DYNAMIC_METHOD_OWNER_TYPE); + this.EmitDynMethodBody(testDynMethod, fieldInfo); + Assert.Throws(() => + { + InvalidParamType2 invalidCallBack = (InvalidParamType2)testDynMethod.CreateDelegate(typeof(InvalidParamType2)); + }); + } + + private DynamicMethod CreateDynMethod(Type dynMethodOwnerType) + { + Type retType = typeof(int); + Type[] paramTypes = new Type[] + { + _DYNAMIC_METHOD_OWNER_TYPE, + typeof(int) + }; + + return new DynamicMethod(c_DYNAMIC_METHOD_NAME, + retType, + paramTypes, + dynMethodOwnerType); + } + + private DynamicMethod CreateDynMethod(Module mod) + { + Type retType = typeof(int); + Type[] paramTypes = new Type[] + { + _DYNAMIC_METHOD_OWNER_TYPE, + typeof(int) + }; + + return new DynamicMethod(c_DYNAMIC_METHOD_NAME, + retType, + paramTypes, + mod, + true); + } + + private void EmitDynMethodBody(DynamicMethod dynMethod, FieldInfo fld) + { + ILGenerator methodIL = dynMethod.GetILGenerator(); + + methodIL.Emit(OpCodes.Ldarg_0); + methodIL.Emit(OpCodes.Ldfld, fld); + + methodIL.Emit(OpCodes.Ldarg_0); + methodIL.Emit(OpCodes.Ldarg_1); + methodIL.Emit(OpCodes.Stfld, fld); + + methodIL.Emit(OpCodes.Ret); + } + } + + internal class TestCreateDelegateOwner2 + { + private int _id; //c_FIELD_NAME + + public TestCreateDelegateOwner2(int id) + { + _id = id; + } + public TestCreateDelegateOwner2() : this(0) + { + } + + public int ID + { + get { return _id; } + } + } + + + internal class TestCreateDelegateOwner2Derived : TestCreateDelegateOwner2 + { + public TestCreateDelegateOwner2Derived(int id) + : base(id) + { + } + + public TestCreateDelegateOwner2Derived() + : base() + { + } + } + + internal delegate int UseLikeStatic2(TestCreateDelegateOwner2 owner, int id); + internal delegate TestCreateDelegateOwner2 InvalidRetType2(TestCreateDelegateOwner2 owner, int id); + internal delegate int WrongParamNumber2(TestCreateDelegateOwner2 owner, int id, int m); + internal delegate int InvalidParamType2(int id, TestCreateDelegateOwner2 owner); +} \ No newline at end of file diff --git a/src/System.Reflection.Emit.Lightweight/tests/DynamicMethodDeclaringType.cs b/src/System.Reflection.Emit.Lightweight/tests/DynamicMethodDeclaringType.cs new file mode 100644 index 000000000000..b0ad05888ebf --- /dev/null +++ b/src/System.Reflection.Emit.Lightweight/tests/DynamicMethodDeclaringType.cs @@ -0,0 +1,27 @@ +// Copyright (c) Microsoft. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. + +using System; +using System.Reflection; +using System.Reflection.Emit; +using Xunit; + +namespace System.Reflection.Emit.Lightweight.Tests +{ + public class DynamicMethodDeclaringType + { + public const string c_DYNAMICMETHODNAME = "MethodName"; + + [Fact] + public void PosTest1() + { + DynamicMethod dynamicmethod = new DynamicMethod(c_DYNAMICMETHODNAME, MethodAttributes.Static | MethodAttributes.Public, CallingConventions.Standard, null, null, typeof(DeclaringTypeTestClass).GetTypeInfo().Module, true); + Type type = dynamicmethod.DeclaringType; + Assert.Null(type); + } + } + + public class DeclaringTypeTestClass + { + } +} \ No newline at end of file diff --git a/src/System.Reflection.Emit.Lightweight/tests/DynamicMethodGetBaseDefinition.cs b/src/System.Reflection.Emit.Lightweight/tests/DynamicMethodGetBaseDefinition.cs new file mode 100644 index 000000000000..11f719921011 --- /dev/null +++ b/src/System.Reflection.Emit.Lightweight/tests/DynamicMethodGetBaseDefinition.cs @@ -0,0 +1,133 @@ +// Copyright (c) Microsoft. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. + +using System; +using System.Reflection; +using System.Reflection.Emit; +using Xunit; + +namespace System.Reflection.Emit.Lightweight.Tests +{ + public class DynamicMethodGetBaseDefinition + { + private const string c_DYNAMIC_METHOD_NAME = "TestDynamicMethodA"; + private readonly Type _DYNAMIC_METHOD_OWNER_TYPE = typeof(TestBaseDefinitionOwner); + private const string c_FIELD_NAME = "_id"; + + public Module CurrentModule + { + get + { + return typeof(DynamicMethodGetBaseDefinition).GetTypeInfo().Assembly.ManifestModule; + } + } + + [Fact] + public void PosTest1() + { + DynamicMethod testDynMethod; + + bool actualResult; + + testDynMethod = this.CreateDynMethod(_DYNAMIC_METHOD_OWNER_TYPE, true); + + MethodInfo testDynMethodInfo = testDynMethod.GetBaseDefinition(); + + actualResult = testDynMethodInfo == testDynMethod; + Assert.True(actualResult, "Failed to get base definition of dynamic method."); + } + + [Fact] + public void PosTest2() + { + DynamicMethod testDynMethod; + + bool actualResult; + + testDynMethod = this.CreateDynMethod(_DYNAMIC_METHOD_OWNER_TYPE, false); + + MethodInfo testDynMethodInfo = testDynMethod.GetBaseDefinition(); + + actualResult = testDynMethodInfo == testDynMethod; + Assert.True(actualResult, "Failed to get base definition of dynamic method."); + } + + [Fact] + public void PosTest3() + { + DynamicMethod testDynMethod; + + bool actualResult; + testDynMethod = this.CreateDynMethod(this.CurrentModule, true); + + MethodInfo testDynMethodInfo = testDynMethod.GetBaseDefinition(); + + actualResult = testDynMethodInfo == testDynMethod; + Assert.True(actualResult, "Failed to get base definition of dynamic method."); + } + + [Fact] + public void PosTest4() + { + DynamicMethod testDynMethod; + + bool actualResult; + testDynMethod = this.CreateDynMethod(this.CurrentModule, false); + + MethodInfo testDynMethodInfo = testDynMethod.GetBaseDefinition(); + + actualResult = testDynMethodInfo == testDynMethod; + Assert.True(actualResult, "Failed to get base definition of dynamic method."); + } + + private DynamicMethod CreateDynMethod(Type dynMethodOwnerType, bool skipVisibility) + { + Type retType = typeof(int); + Type[] paramTypes = new Type[] + { + _DYNAMIC_METHOD_OWNER_TYPE, + typeof(int) + }; + + return new DynamicMethod(c_DYNAMIC_METHOD_NAME, + retType, + paramTypes, + dynMethodOwnerType, + skipVisibility); + } + + private DynamicMethod CreateDynMethod(Module mod, bool skipVisibility) + { + Type retType = typeof(int); + Type[] paramTypes = new Type[] + { + _DYNAMIC_METHOD_OWNER_TYPE, + typeof(int) + }; + + return new DynamicMethod(c_DYNAMIC_METHOD_NAME, + retType, + paramTypes, + mod, + skipVisibility); + } + } + + internal class TestBaseDefinitionOwner + { + private int _id; //c_FIELD_NAME + + public TestBaseDefinitionOwner(int id) + { + _id = id; + } + public TestBaseDefinitionOwner() : this(0) + { + } + + public int ID + { + get { return _id; } + } + } +} \ No newline at end of file diff --git a/src/System.Reflection.Emit.Lightweight/tests/DynamicMethodGetILGenerator1.cs b/src/System.Reflection.Emit.Lightweight/tests/DynamicMethodGetILGenerator1.cs new file mode 100644 index 000000000000..f7e71c7aeb94 --- /dev/null +++ b/src/System.Reflection.Emit.Lightweight/tests/DynamicMethodGetILGenerator1.cs @@ -0,0 +1,184 @@ +// Copyright (c) Microsoft. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. + +using System; +using System.Reflection; +using System.Reflection.Emit; +using Xunit; + +namespace System.Reflection.Emit.Lightweight.Tests +{ + public class DynamicMethodGetILGenerator1 + { + private const string c_DYNAMIC_METHOD_NAME = "TestDynamicMethodA"; + private readonly Type _DYNAMIC_METHOD_OWNER_TYPE = typeof(TestILGeneratorOwner1); + private const string c_FIELD_NAME = "_id"; + + public Module CurrentModule + { + get + { + return typeof(DynamicMethodGetILGenerator1).GetTypeInfo().Assembly.ManifestModule; + } + } + + [Fact] + public void PosTest1() + { + DynamicMethod testDynMethod; + int streamSize; + TestILGeneratorOwner1 target = new TestILGeneratorOwner1(); + int newId = 100; + + bool actualResult = false; + + FieldInfo fieldInfo = _DYNAMIC_METHOD_OWNER_TYPE.GetField( + c_FIELD_NAME, + BindingFlags.Instance | + BindingFlags.NonPublic); + + testDynMethod = this.CreateDynMethod(_DYNAMIC_METHOD_OWNER_TYPE, true); + + streamSize = 8; + ILGenerator dynMethodIL = testDynMethod.GetILGenerator(streamSize); + this.EmitDynMethodBody(dynMethodIL, fieldInfo); + + ILUseLikeInstance1 instanceCallBack = (ILUseLikeInstance1)testDynMethod.CreateDelegate( + typeof(ILUseLikeInstance1), + target); + + actualResult = this.VerifyILGenerator(instanceCallBack, target, newId); + Assert.True(actualResult, "Failed to get IL generator for dynamic method."); + } + + [Fact] + public void PosTest2() + { + DynamicMethod testDynMethod; + int streamSize; + TestILGeneratorOwner1 target = new TestILGeneratorOwner1(); + int newId = 100; + + bool actualResult; + FieldInfo fieldInfo = _DYNAMIC_METHOD_OWNER_TYPE.GetField( + c_FIELD_NAME, + BindingFlags.Instance | + BindingFlags.NonPublic); + + testDynMethod = this.CreateDynMethod(_DYNAMIC_METHOD_OWNER_TYPE, false); + + streamSize = 8; + ILGenerator dynMethodIL = testDynMethod.GetILGenerator(streamSize); + this.EmitDynMethodBody(dynMethodIL, fieldInfo); + + ILUseLikeInstance1 instanceCallBack = (ILUseLikeInstance1)testDynMethod.CreateDelegate( + typeof(ILUseLikeInstance1), + target); + + actualResult = this.VerifyILGenerator(instanceCallBack, target, newId); + Assert.True(actualResult, "Failed to get IL generator for dynamic method."); + } + + [Fact] + public void PosTest3() + { + DynamicMethod testDynMethod; + int streamSize; + TestILGeneratorOwner1 target = new TestILGeneratorOwner1(); + int newId = 100; + + bool actualResult; + FieldInfo fieldInfo = _DYNAMIC_METHOD_OWNER_TYPE.GetField( + c_FIELD_NAME, + BindingFlags.Instance | + BindingFlags.NonPublic); + + testDynMethod = this.CreateDynMethod(this.CurrentModule, true); + + streamSize = 8; + ILGenerator dynMethodIL = testDynMethod.GetILGenerator(streamSize); + this.EmitDynMethodBody(dynMethodIL, fieldInfo); + + ILUseLikeInstance1 instanceCallBack = (ILUseLikeInstance1)testDynMethod.CreateDelegate( + typeof(ILUseLikeInstance1), + target); + + actualResult = this.VerifyILGenerator(instanceCallBack, target, newId); + Assert.True(actualResult, "Failed to get IL generator for dynamic method."); + } + + private DynamicMethod CreateDynMethod(Type dynMethodOwnerType, bool skipVisibility) + { + Type retType = typeof(int); + Type[] paramTypes = new Type[] + { + _DYNAMIC_METHOD_OWNER_TYPE, + typeof(int) + }; + + return new DynamicMethod(c_DYNAMIC_METHOD_NAME, + retType, + paramTypes, + dynMethodOwnerType, + skipVisibility); + } + + private DynamicMethod CreateDynMethod(Module mod, bool skipVisibility) + { + Type retType = typeof(int); + Type[] paramTypes = new Type[] + { + _DYNAMIC_METHOD_OWNER_TYPE, + typeof(int) + }; + + return new DynamicMethod(c_DYNAMIC_METHOD_NAME, + retType, + paramTypes, + mod, + skipVisibility); + } + + private void EmitDynMethodBody(ILGenerator methodIL, FieldInfo fld) + { + methodIL.Emit(OpCodes.Ldarg_0); + methodIL.Emit(OpCodes.Ldfld, fld); + + methodIL.Emit(OpCodes.Ldarg_0); + methodIL.Emit(OpCodes.Ldarg_1); + methodIL.Emit(OpCodes.Stfld, fld); + + methodIL.Emit(OpCodes.Ret); + } + + private bool VerifyILGenerator(ILUseLikeInstance1 instanceCallBack, + TestILGeneratorOwner1 target, + int newId) + { + bool retVal = false; + retVal = target.ID == instanceCallBack(newId); + retVal = (target.ID == newId) && retVal; + return retVal; + } + } + + internal class TestILGeneratorOwner1 + { + private int _id; //c_FIELD_NAME + + public TestILGeneratorOwner1(int id) + { + _id = id; + } + public TestILGeneratorOwner1() : this(0) + { + } + + public int ID + { + get { return _id; } + } + } + + internal delegate int ILUseLikeInstance1(int id); +} \ No newline at end of file diff --git a/src/System.Reflection.Emit.Lightweight/tests/DynamicMethodGetILGenerator2.cs b/src/System.Reflection.Emit.Lightweight/tests/DynamicMethodGetILGenerator2.cs new file mode 100644 index 000000000000..2681752c1792 --- /dev/null +++ b/src/System.Reflection.Emit.Lightweight/tests/DynamicMethodGetILGenerator2.cs @@ -0,0 +1,178 @@ +// Copyright (c) Microsoft. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. + +using System; +using System.Reflection; +using System.Reflection.Emit; +using Xunit; + +namespace System.Reflection.Emit.Lightweight.Tests +{ + public class DynamicMethodGetILGenerator2 + { + private const string c_DYNAMIC_METHOD_NAME = "TestDynamicMethodA"; + private readonly Type _DYNAMIC_METHOD_OWNER_TYPE = typeof(TestILGeneratorOwner2); + private const string c_FIELD_NAME = "_id"; + + public Module CurrentModule + { + get + { + return typeof(DynamicMethodGetILGenerator2).GetTypeInfo().Assembly.ManifestModule; + } + } + + public void PosTest1() + { + DynamicMethod testDynMethod; + TestILGeneratorOwner2 target = new TestILGeneratorOwner2(); + int newId = 100; + + bool actualResult = false; + + FieldInfo fieldInfo = _DYNAMIC_METHOD_OWNER_TYPE.GetField( + c_FIELD_NAME, + BindingFlags.Instance | + BindingFlags.NonPublic); + + testDynMethod = this.CreateDynMethod(_DYNAMIC_METHOD_OWNER_TYPE, true); + + ILGenerator dynMethodIL = testDynMethod.GetILGenerator(); + this.EmitDynMethodBody(dynMethodIL, fieldInfo); + + ILUseLikeInstance2 instanceCallBack = (ILUseLikeInstance2)testDynMethod.CreateDelegate( + typeof(ILUseLikeInstance2), + target); + + actualResult = this.VerifyILGenerator(instanceCallBack, target, newId); + Assert.True(actualResult, "Failed to get IL Generator for dynamic method."); + } + + public void PosTest2() + { + DynamicMethod testDynMethod; + TestILGeneratorOwner2 target = new TestILGeneratorOwner2(); + int newId = 100; + + bool actualResult; + + FieldInfo fieldInfo = _DYNAMIC_METHOD_OWNER_TYPE.GetField( + c_FIELD_NAME, + BindingFlags.Instance | + BindingFlags.NonPublic); + + testDynMethod = this.CreateDynMethod(_DYNAMIC_METHOD_OWNER_TYPE, false); + + ILGenerator dynMethodIL = testDynMethod.GetILGenerator(); + this.EmitDynMethodBody(dynMethodIL, fieldInfo); + + ILUseLikeInstance2 instanceCallBack = (ILUseLikeInstance2)testDynMethod.CreateDelegate( + typeof(ILUseLikeInstance2), + target); + + actualResult = this.VerifyILGenerator(instanceCallBack, target, newId); + Assert.True(actualResult, "Failed to get IL Generator for dynamic method."); + } + + public void PosTest3() + { + DynamicMethod testDynMethod; + TestILGeneratorOwner2 target = new TestILGeneratorOwner2(); + int newId = 100; + + bool actualResult; + + + FieldInfo fieldInfo = _DYNAMIC_METHOD_OWNER_TYPE.GetField( + c_FIELD_NAME, + BindingFlags.Instance | + BindingFlags.NonPublic); + + testDynMethod = this.CreateDynMethod(this.CurrentModule, true); + + ILGenerator dynMethodIL = testDynMethod.GetILGenerator(); + this.EmitDynMethodBody(dynMethodIL, fieldInfo); + + ILUseLikeInstance2 instanceCallBack = (ILUseLikeInstance2)testDynMethod.CreateDelegate( + typeof(ILUseLikeInstance2), + target); + + actualResult = this.VerifyILGenerator(instanceCallBack, target, newId); + Assert.True(actualResult, "Failed to get IL Generator for dynamic method."); + } + + private DynamicMethod CreateDynMethod(Type dynMethodOwnerType, bool skipVisibility) + { + Type retType = typeof(int); + Type[] paramTypes = new Type[] + { + _DYNAMIC_METHOD_OWNER_TYPE, + typeof(int) + }; + + return new DynamicMethod(c_DYNAMIC_METHOD_NAME, + retType, + paramTypes, + dynMethodOwnerType, + skipVisibility); + } + + private DynamicMethod CreateDynMethod(Module mod, bool skipVisibility) + { + Type retType = typeof(int); + Type[] paramTypes = new Type[] + { + _DYNAMIC_METHOD_OWNER_TYPE, + typeof(int) + }; + + return new DynamicMethod(c_DYNAMIC_METHOD_NAME, + retType, + paramTypes, + mod, + skipVisibility); + } + + private void EmitDynMethodBody(ILGenerator methodIL, FieldInfo fld) + { + methodIL.Emit(OpCodes.Ldarg_0); + methodIL.Emit(OpCodes.Ldfld, fld); + + methodIL.Emit(OpCodes.Ldarg_0); + methodIL.Emit(OpCodes.Ldarg_1); + methodIL.Emit(OpCodes.Stfld, fld); + + methodIL.Emit(OpCodes.Ret); + } + + private bool VerifyILGenerator(ILUseLikeInstance2 instanceCallBack, + TestILGeneratorOwner2 target, + int newId) + { + bool retVal = false; + retVal = target.ID == instanceCallBack(newId); + retVal = (target.ID == newId) && retVal; + return retVal; + } + } + + internal class TestILGeneratorOwner2 + { + private int _id; //c_FIELD_NAME + + public TestILGeneratorOwner2(int id) + { + _id = id; + } + public TestILGeneratorOwner2() : this(0) + { + } + + public int ID + { + get { return _id; } + } + } + + internal delegate int ILUseLikeInstance2(int id); +} \ No newline at end of file diff --git a/src/System.Reflection.Emit.Lightweight/tests/DynamicMethodGetParameters.cs b/src/System.Reflection.Emit.Lightweight/tests/DynamicMethodGetParameters.cs new file mode 100644 index 000000000000..cc5221f0ef3e --- /dev/null +++ b/src/System.Reflection.Emit.Lightweight/tests/DynamicMethodGetParameters.cs @@ -0,0 +1,78 @@ +// Copyright (c) Microsoft. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. + +using System; +using System.Reflection; +using System.Reflection.Emit; +using System.Globalization; +using Xunit; + +namespace System.Reflection.Emit.Lightweight.Tests +{ + public class DynamicMethodGetParameters + { + public const string c_DYNAMICMETHODNAME = "MethodName"; + + [Fact] + public void PosTest1() + { + Type[] typeparameters = { typeof(string), typeof(int), typeof(TestGetParametersClass) }; + DynamicMethod dynamicmethod = new DynamicMethod(c_DYNAMICMETHODNAME, typeof(string), typeparameters, typeof(TestGetParametersClass).GetTypeInfo().Module); + ParameterInfo[] parameterinfo = dynamicmethod.GetParameters(); + Assert.False(parameterinfo.Length != 3); + Assert.False((parameterinfo[0].ParameterType != typeof(String)) || (parameterinfo[1].ParameterType != typeof(int)) || (parameterinfo[2].ParameterType != typeof(TestGetParametersClass))); + } + + [Fact] + public void PosTest2() + { + Type[] typeparameters = { typeof(char?) }; + DynamicMethod dynamicmethod = new DynamicMethod(c_DYNAMICMETHODNAME, typeof(string), typeparameters, typeof(TestGetParametersClass).GetTypeInfo().Module); + ParameterInfo[] parameterinfo = dynamicmethod.GetParameters(); + Assert.False((parameterinfo.Length != 1) || (parameterinfo[0].ParameterType != typeof(char?))); + } + + [Fact] + public void PosTest3() + { + Type[] typeparameters = { typeof(GetParametersGenClass1<>), typeof(GetParametersGenClass2<,>) }; + DynamicMethod dynamicmethod = new DynamicMethod(c_DYNAMICMETHODNAME, typeof(string), typeparameters, typeof(TestGetParametersClass).GetTypeInfo().Module); + ParameterInfo[] parameterinfo = dynamicmethod.GetParameters(); + Assert.False((parameterinfo.Length != 2) || (parameterinfo[0].ParameterType != typeof(GetParametersGenClass1<>)) || (parameterinfo[1].ParameterType != typeof(GetParametersGenClass2<,>))); + } + + [Fact] + public void PosTest4() + { + Type[] typeparameters = { typeof(GetParametersTestInter) }; + DynamicMethod dynamicmethod = new DynamicMethod(c_DYNAMICMETHODNAME, typeof(string), typeparameters, typeof(TestGetParametersClass).GetTypeInfo().Module); + ParameterInfo[] parameterinfo = dynamicmethod.GetParameters(); + Assert.False((parameterinfo.Length != 1) || (parameterinfo[0].ParameterType != typeof(GetParametersTestInter))); + } + + [Fact] + public void PosTest5() + { + DynamicMethod dynamicmethod = new DynamicMethod(c_DYNAMICMETHODNAME, typeof(string), null, typeof(TestGetParametersClass).GetTypeInfo().Module); + ParameterInfo[] parameterinfo = dynamicmethod.GetParameters(); + Assert.False(parameterinfo.Length != 0); + } + } + + + public class TestGetParametersClass + { + } + + public class GetParametersGenClass1 + { + } + + public class GetParametersGenClass2 + { + } + + public interface GetParametersTestInter + { + } +} \ No newline at end of file diff --git a/src/System.Reflection.Emit.Lightweight/tests/DynamicMethodInitLocals.cs b/src/System.Reflection.Emit.Lightweight/tests/DynamicMethodInitLocals.cs new file mode 100644 index 000000000000..e08d3637f661 --- /dev/null +++ b/src/System.Reflection.Emit.Lightweight/tests/DynamicMethodInitLocals.cs @@ -0,0 +1,42 @@ +// Copyright (c) Microsoft. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. + +using System; +using System.Reflection; +using System.Reflection.Emit; +using Xunit; + +namespace System.Reflection.Emit.Lightweight.Tests +{ + public class DynamicMethodInitLocals + { + public const string c_DYNAMICMETHODNAME = "MethodName"; + + [Fact] + public void PosTest1() + { + DynamicMethod dynamicmethod = new DynamicMethod(c_DYNAMICMETHODNAME, MethodAttributes.Static | MethodAttributes.Public, CallingConventions.Standard, null, null, typeof(TestMethodInitLocalsClass).GetTypeInfo().Module, true); + dynamicmethod.InitLocals = true; + Assert.False(!dynamicmethod.InitLocals); + } + + [Fact] + public void PosTest2() + { + DynamicMethod dynamicmethod = new DynamicMethod(c_DYNAMICMETHODNAME, MethodAttributes.Static | MethodAttributes.Public, CallingConventions.Standard, null, null, typeof(TestMethodInitLocalsClass).GetTypeInfo().Module, true); + dynamicmethod.InitLocals = false; + Assert.False(dynamicmethod.InitLocals); + } + + [Fact] + public void PosTest3() + { + DynamicMethod dynamicmethod = new DynamicMethod(c_DYNAMICMETHODNAME, MethodAttributes.Static | MethodAttributes.Public, CallingConventions.Standard, null, null, typeof(TestMethodInitLocalsClass).GetTypeInfo().Module, true); + Assert.False(!dynamicmethod.InitLocals); + } + } + + public class TestMethodInitLocalsClass + { + } +} \ No newline at end of file diff --git a/src/System.Reflection.Emit.Lightweight/tests/DynamicMethodModule.cs b/src/System.Reflection.Emit.Lightweight/tests/DynamicMethodModule.cs new file mode 100644 index 000000000000..e005e07b5040 --- /dev/null +++ b/src/System.Reflection.Emit.Lightweight/tests/DynamicMethodModule.cs @@ -0,0 +1,43 @@ +// Copyright (c) Microsoft. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. + +using System; +using System.Reflection; +using System.Reflection.Emit; +using Xunit; + +namespace System.Reflection.Emit.Lightweight.Tests +{ + public class DynamicMethodModule + { + public const string c_DYNAMICMETHODNAME = "MethodName"; + + [Fact] + public void PosTest1() + { + DynamicMethod dynamicmethod = new DynamicMethod(c_DYNAMICMETHODNAME, null, null, typeof(String).GetTypeInfo().Module); + Module module = dynamicmethod.Module; + Assert.NotNull(module); + } + + [Fact] + public void PosTest2() + { + DynamicMethod dynamicmethod = new DynamicMethod(c_DYNAMICMETHODNAME, null, null, typeof(DynamicMethodModule).GetTypeInfo().Module); + Module module = dynamicmethod.Module; + Assert.NotNull(module); + } + + [Fact] + public void PosTest3() + { + DynamicMethod dynamicmethod = new DynamicMethod(c_DYNAMICMETHODNAME, null, null, typeof(MethodModuleTestClass).GetTypeInfo().Module); + Module module = dynamicmethod.Module; + Assert.NotNull(module); + } + } + + public class MethodModuleTestClass + { + } +} diff --git a/src/System.Reflection.Emit.Lightweight/tests/DynamicMethodName.cs b/src/System.Reflection.Emit.Lightweight/tests/DynamicMethodName.cs new file mode 100644 index 000000000000..ccd9fab3482a --- /dev/null +++ b/src/System.Reflection.Emit.Lightweight/tests/DynamicMethodName.cs @@ -0,0 +1,21 @@ +// Copyright (c) Microsoft. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. + +using System; +using System.Reflection; +using System.Reflection.Emit; +using Xunit; + +namespace System.Reflection.Emit.Lightweight.Tests +{ + public class DynamicMethodName + { + [Fact] + public void PosTest1() + { + string dynamicname = "TestDynamicStringName"; + DynamicMethod dynamicmethod = new DynamicMethod(dynamicname, null, null, typeof(DynamicMethodName).GetTypeInfo().Module); + Assert.Equal(dynamicmethod.Name, dynamicname); + } + } +} diff --git a/src/System.Reflection.Emit.Lightweight/tests/DynamicMethodReturnParameter.cs b/src/System.Reflection.Emit.Lightweight/tests/DynamicMethodReturnParameter.cs new file mode 100644 index 000000000000..17d2d26aca36 --- /dev/null +++ b/src/System.Reflection.Emit.Lightweight/tests/DynamicMethodReturnParameter.cs @@ -0,0 +1,27 @@ +// Copyright (c) Microsoft. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. + +using System; +using System.Reflection; +using System.Reflection.Emit; +using Xunit; + +namespace System.Reflection.Emit.Lightweight.Tests +{ + public class DynamicMethodReturnParameter + { + public const string c_DYNAMICMETHODNAME = "MethodName"; + + [Fact] + public void PosTest1() + { + DynamicMethod dynamicmethod = new DynamicMethod(c_DYNAMICMETHODNAME, null, null, typeof(ReturnParameterTestClass).GetTypeInfo().Module); + ParameterInfo parameterinfo = dynamicmethod.ReturnParameter; + Assert.Null(parameterinfo); + } + } + + public class ReturnParameterTestClass + { + } +} diff --git a/src/System.Reflection.Emit.Lightweight/tests/DynamicMethodReturnType.cs b/src/System.Reflection.Emit.Lightweight/tests/DynamicMethodReturnType.cs new file mode 100644 index 000000000000..c0a0237966ff --- /dev/null +++ b/src/System.Reflection.Emit.Lightweight/tests/DynamicMethodReturnType.cs @@ -0,0 +1,75 @@ +// Copyright (c) Microsoft. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. + +using System; +using System.Reflection; +using System.Reflection.Emit; +using Xunit; + +namespace System.Reflection.Emit.Lightweight.Tests +{ + public class DynamicMethodReturnType + { + public const string c_DYNAMICMETHODNAME = "MethodName"; + + [Fact] + public void PosTest1() + { + DynamicMethod dynamicmethod = new DynamicMethod(c_DYNAMICMETHODNAME, typeof(String), null, typeof(ReturnTypeTestClass).GetTypeInfo().Module); + Type type = dynamicmethod.ReturnType; + Assert.Equal(type, typeof(String)); + } + + [Fact] + public void PosTest2() + { + DynamicMethod dynamicmethod = new DynamicMethod(c_DYNAMICMETHODNAME, typeof(char?), null, typeof(ReturnTypeTestClass).GetTypeInfo().Module); + Type type = dynamicmethod.ReturnType; + Assert.Equal(type, typeof(char?)); + } + + [Fact] + public void PosTest3() + { + DynamicMethod dynamicmethod = new DynamicMethod(c_DYNAMICMETHODNAME, typeof(ReturnTypeGenClass<>), null, typeof(ReturnTypeTestClass).GetTypeInfo().Module); + Type type = dynamicmethod.ReturnType; + Assert.Equal(type, typeof(ReturnTypeGenClass<>)); + } + + [Fact] + public void PosTest4() + { + DynamicMethod dynamicmethod = new DynamicMethod(c_DYNAMICMETHODNAME, typeof(ReturnTypeTestClass), null, typeof(ReturnTypeTestClass).GetTypeInfo().Module); + Type type = dynamicmethod.ReturnType; + Assert.Equal(type, typeof(ReturnTypeTestClass)); + } + + [Fact] + public void PosTest5() + { + DynamicMethod dynamicmethod = new DynamicMethod(c_DYNAMICMETHODNAME, typeof(ReturnTypeTestInterface), null, typeof(ReturnTypeTestClass).GetTypeInfo().Module); + Type type = dynamicmethod.ReturnType; + Assert.Equal(type, typeof(ReturnTypeTestInterface)); + } + + [Fact] + public void PosTest6() + { + DynamicMethod dynamicmethod = new DynamicMethod(c_DYNAMICMETHODNAME, null, null, typeof(ReturnTypeTestClass).GetTypeInfo().Module); + Type type = dynamicmethod.ReturnType; + Assert.Equal(type, typeof(void)); + } + } + + public class ReturnTypeTestClass + { + } + + public class ReturnTypeGenClass + { + } + + public interface ReturnTypeTestInterface + { + } +} \ No newline at end of file diff --git a/src/System.Reflection.Emit.Lightweight/tests/DynamicMethodToString.cs b/src/System.Reflection.Emit.Lightweight/tests/DynamicMethodToString.cs new file mode 100644 index 000000000000..6c02fa4f3a2f --- /dev/null +++ b/src/System.Reflection.Emit.Lightweight/tests/DynamicMethodToString.cs @@ -0,0 +1,59 @@ +// Copyright (c) Microsoft. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. + +using System; +using System.Reflection; +using System.Reflection.Emit; +using System.Globalization; +using Xunit; + +namespace System.Reflection.Emit.Lightweight.Tests +{ + public class DynamicMethodToString + { + public const string c_DYNAMICMETHODNAME = "MethodName"; + + [Fact] + public void PosTest1() + { + Type[] typeparameters = { typeof(string), typeof(int), typeof(ToStringTestClass) }; + DynamicMethod dynamicmethod = new DynamicMethod(c_DYNAMICMETHODNAME, typeof(string), typeparameters, typeof(ToStringTestClass).GetTypeInfo().Module); + string strvalue = dynamicmethod.ToString(); + Assert.Equal(strvalue, "System.String MethodName(System.String, Int32, System.Reflection.Emit.Lightweight.Tests.ToStringTestClass)"); + } + + [Fact] + public void PosTest2() + { + string method_name = "TestDynamicMethodName"; + Type[] typeparameters = { typeof(ToStringGenClass1<>) }; + DynamicMethod dynamicmethod = new DynamicMethod(method_name, typeof(string), typeparameters, typeof(ToStringTestClass).GetTypeInfo().Module); + string strvalue = dynamicmethod.ToString(); + Assert.NotNull(strvalue); + } + + [Fact] + public void PosTest3() + { + DynamicMethod dynamicmethod = new DynamicMethod(c_DYNAMICMETHODNAME, null, null, typeof(ToStringTestClass).GetTypeInfo().Module); + string strvalue = dynamicmethod.ToString(); + Assert.Equal(strvalue, "Void MethodName()"); + } + } + + public class ToStringTestClass + { + } + + public class ToStringGenClass1 + { + } + + public class ToStringGenClass2 + { + } + + public interface ToStringTestInter + { + } +} \ No newline at end of file diff --git a/src/System.Reflection.Emit.Lightweight/tests/DynamicMethodctor1.cs b/src/System.Reflection.Emit.Lightweight/tests/DynamicMethodctor1.cs new file mode 100644 index 000000000000..4f18623a7867 --- /dev/null +++ b/src/System.Reflection.Emit.Lightweight/tests/DynamicMethodctor1.cs @@ -0,0 +1,97 @@ +// Copyright (c) Microsoft. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. + +using System; +using System.Reflection; +using System.Reflection.Emit; +using System.Threading; +using Xunit; + +namespace System.Reflection.Emit.Lightweight.Tests +{ + public class DynamicMethodctor1 + { + [Fact] + public void PosTest1() + { + Module mod = typeof(MethodTestClass).GetTypeInfo().Module; + DynamicMethod dynamicMeth = new DynamicMethod("Pos1method", typeof(void), null, mod); + Assert.False(dynamicMeth == null || dynamicMeth.Name != "Pos1method"); + } + + [Fact] + public void PosTest2() + { + Module mod = typeof(MethodTestClass).GetTypeInfo().Module; + DynamicMethod dynamicMeth = new DynamicMethod("Pos2method", typeof(void), new Type[] { typeof(int), typeof(string) }, mod); + Assert.False(dynamicMeth == null || dynamicMeth.Name != "Pos2method"); + } + + [Fact] + public void PosTest3() + { + Module mod = typeof(MethodTestClass).GetTypeInfo().Module; + DynamicMethod dynamicMeth = new DynamicMethod("Pos3method", typeof(string), null, mod); + Assert.False(dynamicMeth == null || dynamicMeth.Name != "Pos3method"); + } + + [Fact] + public void PosTest4() + { + Module mod = typeof(MethodTestClass).GetTypeInfo().Module; + DynamicMethod dynamicMeth = new DynamicMethod("Pos4method", typeof(string), new Type[] { typeof(int), typeof(string) }, mod); + Assert.False(dynamicMeth == null || dynamicMeth.Name != "Pos4method"); + } + + [Fact] + public void PosTest5() + { + Module mod = typeof(MethodTestClass).GetTypeInfo().Module; + DynamicMethod dynamicMeth = new DynamicMethod(string.Empty, typeof(string), new Type[] { typeof(int), typeof(string) }, mod); + Assert.False(dynamicMeth == null || dynamicMeth.Name != string.Empty); + } + + [Fact] + public void NegTest1() + { + Module mod = typeof(MethodTestClass).GetTypeInfo().Module; + Assert.Throws(() => { DynamicMethod dynamicMeth = new DynamicMethod("Neg1method", typeof(void), new Type[] { null, typeof(string) }, mod); }); + } + + [Fact] + public void NegTest2() + { + Module mod = typeof(MethodTestClass).GetTypeInfo().Module; + Assert.Throws(() => { DynamicMethod dynamicMeth = new DynamicMethod(null, typeof(void), new Type[] { typeof(string) }, mod); }); + } + + [Fact] + public void NegTest3() + { + Module mod = null; + Assert.Throws(() => { DynamicMethod dynamicMeth = new DynamicMethod("Neg3method", typeof(void), new Type[] { typeof(string) }, mod); }); + } + + [Fact] + public void NegTest4() + { + Module mod = typeof(MethodTestClass).GetTypeInfo().Module; + Type tpA = CreateType(); + Assert.Throws(() => { DynamicMethod dynamicMeth = new DynamicMethod("Neg4method", tpA, new Type[] { typeof(string) }, mod); }); + } + + private static Type CreateType() + { + AssemblyName assemName = new AssemblyName("assemName"); + AssemblyBuilder myAssemBuilder = AssemblyBuilder.DefineDynamicAssembly(assemName, AssemblyBuilderAccess.Run); + ModuleBuilder myModB = Utilities.GetModuleBuilder(myAssemBuilder, "Module1"); + TypeBuilder myTypeB = myModB.DefineType("testType"); + return myTypeB.MakeByRefType(); + } + } + + public class MethodTestClass + { + public MethodTestClass() { } + } +} \ No newline at end of file diff --git a/src/System.Reflection.Emit.Lightweight/tests/DynamicMethodctor2.cs b/src/System.Reflection.Emit.Lightweight/tests/DynamicMethodctor2.cs new file mode 100644 index 000000000000..868132ba53e7 --- /dev/null +++ b/src/System.Reflection.Emit.Lightweight/tests/DynamicMethodctor2.cs @@ -0,0 +1,142 @@ +// Copyright (c) Microsoft. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. + +using System; +using System.Reflection; +using System.Reflection.Emit; +using System.Threading; +using Xunit; + +namespace System.Reflection.Emit.Lightweight.Tests +{ + public class DynamicMethodctor2 + { + [Fact] + public void PosTest1() + { + Type owner = typeof(MethodTestClass2); + DynamicMethod dynamicMeth = new DynamicMethod("Pos1method", typeof(void), null, owner); + Assert.False(dynamicMeth == null || dynamicMeth.Name != "Pos1method"); + } + + [Fact] + public void PosTest2() + { + Type owner = typeof(MethodTestClass2); + DynamicMethod dynamicMeth = new DynamicMethod("Pos2method", typeof(void), new Type[] { typeof(int), typeof(string) }, owner); + Assert.False(dynamicMeth == null || dynamicMeth.Name != "Pos2method"); + } + + [Fact] + public void PosTest3() + { + Type owner = typeof(MethodTestClass2); + DynamicMethod dynamicMeth = new DynamicMethod("Pos3method", typeof(string), null, owner); + Assert.False(dynamicMeth == null || dynamicMeth.Name != "Pos3method"); + } + + [Fact] + public void PosTest4() + { + Type owner = typeof(MethodTestClass2); + DynamicMethod dynamicMeth = new DynamicMethod("Pos4method", typeof(string), new Type[] { typeof(int), typeof(string) }, owner); + Assert.False(dynamicMeth == null || dynamicMeth.Name != "Pos4method"); + } + + [Fact] + public void PosTest5() + { + Type owner = typeof(MethodTestClass2); + DynamicMethod dynamicMeth = new DynamicMethod(string.Empty, typeof(string), new Type[] { typeof(int), typeof(string) }, owner); + Assert.False(dynamicMeth == null || dynamicMeth.Name != string.Empty); + } + + [Fact] + public void NegTest1() + { + Type owner = typeof(MethodTestClass2); + Assert.Throws(() => { DynamicMethod dynamicMeth = new DynamicMethod("Neg1method", typeof(void), new Type[] { null, typeof(string) }, owner); }); + } + + [Fact] + public void NegTest2() + { + Type owner = typeof(Array); + Assert.Throws(() => { DynamicMethod dynamicMeth = new DynamicMethod("Neg2method", typeof(void), new Type[] { null, typeof(string) }, owner); }); + } + + [Fact] + public void NegTest3() + { + Type owner = typeof(MethodTestInterface); + Assert.Throws(() => { DynamicMethod dynamicMeth = new DynamicMethod("Neg3method", typeof(void), new Type[] { null, typeof(string) }, owner); }); + } + + [Fact] + public void NegTest4() + { + Type[] genericTypes = typeof(MethodMyClass<>).GetGenericArguments(); + Type owner = genericTypes[0]; + Assert.Throws(() => { DynamicMethod dynamicMeth = new DynamicMethod("Neg4method", typeof(void), new Type[] { null, typeof(string) }, owner); }); + } + + [Fact] + public void NegTest5() + { + Type owner = typeof(MethodTestClass2); + Assert.Throws(() => { DynamicMethod dynamicMeth = new DynamicMethod(null, typeof(void), new Type[] { typeof(string) }, owner); }); + } + + [Fact] + public void NegTest6() + { + Type owner = null; + Assert.Throws(() => + { + DynamicMethod dynamicMeth = new DynamicMethod("Neg6method", typeof(void), new Type[] { typeof(string) }, owner); + }); + } + + [Fact] + public void NegTest7() + { + Type returnType = CreateType(); + Type owner = typeof(MethodTestClass2); + Assert.Throws(() => + { + DynamicMethod dynamicMeth = new DynamicMethod("Neg7method", returnType, new Type[] { typeof(string) }, owner); + }); + } + + private static Type CreateType() + { + AssemblyName assemName = new AssemblyName("assemName"); + AssemblyBuilder myAssemBuilder = AssemblyBuilder.DefineDynamicAssembly(assemName, AssemblyBuilderAccess.Run); + ModuleBuilder myModB = Utilities.GetModuleBuilder(myAssemBuilder, "Module1"); + TypeBuilder myTypeB = myModB.DefineType("testType"); + return myTypeB.MakeByRefType(); + } + } + + public class MethodTestClass2 + { + private int _id = 0; + public MethodTestClass2(int id) + { + _id = id; + } + public int ID + { + get + { + return _id; + } + } + } + + + public interface MethodTestInterface { } + + + public class MethodMyClass { } +} \ No newline at end of file diff --git a/src/System.Reflection.Emit.Lightweight/tests/DynamicMethodctor3.cs b/src/System.Reflection.Emit.Lightweight/tests/DynamicMethodctor3.cs new file mode 100644 index 000000000000..e14a30f97831 --- /dev/null +++ b/src/System.Reflection.Emit.Lightweight/tests/DynamicMethodctor3.cs @@ -0,0 +1,114 @@ +// Copyright (c) Microsoft. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. + +using System; +using System.Reflection; +using System.Reflection.Emit; +using System.Threading; +using Xunit; + +namespace System.Reflection.Emit.Lightweight.Tests +{ + public class DynamicMethodctor3 + { + [Fact] + public void PosTest1() + { + Module mod = typeof(MethodTestClass3).GetTypeInfo().Module; + bool skipVisb = true; + DynamicMethod dynamicMeth = new DynamicMethod("Pos1method", typeof(int), new Type[] { typeof(int) }, mod, skipVisb); + MethodAttributes attributes = dynamicMeth.Attributes; + Assert.False(dynamicMeth == null || dynamicMeth.Name != "Pos1method" || attributes != (MethodAttributes.Public | MethodAttributes.Static)); + } + + [Fact] + public void PosTest2() + { + Module mod = typeof(MethodTestClass3).GetTypeInfo().Module; + bool skipVisb = false; + DynamicMethod dynamicMeth = new DynamicMethod("Pos2method", typeof(void), new Type[] { typeof(int), typeof(string) }, mod, skipVisb); + MethodAttributes attributes = dynamicMeth.Attributes; + Assert.False(dynamicMeth == null || dynamicMeth.Name != "Pos2method" || attributes != (MethodAttributes.Public | MethodAttributes.Static)); + } + + [Fact] + public void PosTest3() + { + Module mod = typeof(MethodTestClass3).GetTypeInfo().Module; + bool skipVisib = true; + DynamicMethod dynamicMeth = new DynamicMethod("Pos3method", typeof(string), new Type[] { typeof(int), typeof(string) }, mod, skipVisib); + MethodAttributes attributes = dynamicMeth.Attributes; + Assert.False(dynamicMeth == null || dynamicMeth.Name != "Pos3method" || attributes != (MethodAttributes.Static | MethodAttributes.Public)); + } + + [Fact] + public void NegTest1() + { + Module mod = typeof(MethodTestClass3).GetTypeInfo().Module; + bool[] skipVisibs = new bool[] { false, true }; + for (int i = 0; i < skipVisibs.Length; i++) + { + Assert.Throws(() => { DynamicMethod dynamicMeth = new DynamicMethod("Neg1method", typeof(void), new Type[] { null, typeof(string) }, mod, skipVisibs[i]); }); + } + } + + [Fact] + public void NegTest2() + { + Module mod = typeof(MethodTestClass3).GetTypeInfo().Module; + bool[] skipVisibs = new bool[] { false, true }; + for (int i = 0; i < skipVisibs.Length; i++) + { + Assert.Throws(() => { DynamicMethod dynamicMeth = new DynamicMethod(null, typeof(void), new Type[] { typeof(string) }, mod, skipVisibs[i]); }); + } + } + + [Fact] + public void NegTest3() + { + Module mod = null; + bool[] skipVisibs = new bool[] { false, true }; + for (int i = 0; i < skipVisibs.Length; i++) + { + Assert.Throws(() => { DynamicMethod dynamicMeth = new DynamicMethod("Neg3method", typeof(void), new Type[] { typeof(string) }, mod, skipVisibs[i]); }); + } + } + + [Fact] + public void NegTest4() + { + Module mod = typeof(MethodTestClass3).GetTypeInfo().Module; + bool[] skipVisibs = new bool[] { false, true }; + Type tpA = CreateType(); + for (int i = 0; i < skipVisibs.Length; i++) + { + Assert.Throws(() => { DynamicMethod dynamicMeth = new DynamicMethod("Neg4method", tpA, new Type[] { typeof(string) }, mod, skipVisibs[i]); }); + } + } + + private static Type CreateType() + { + AssemblyName assemName = new AssemblyName("assemName"); + AssemblyBuilder myAssemBuilder = AssemblyBuilder.DefineDynamicAssembly(assemName, AssemblyBuilderAccess.Run); + ModuleBuilder myModB = Utilities.GetModuleBuilder(myAssemBuilder, "Module1"); + TypeBuilder myTypeB = myModB.DefineType("testType"); + return myTypeB.MakeByRefType(); + } + } + + public class MethodTestClass3 + { + private int _id = 0; + public MethodTestClass3(int id) + { + _id = id; + } + public int ID + { + get + { + return _id; + } + } + } +} \ No newline at end of file diff --git a/src/System.Reflection.Emit.Lightweight/tests/DynamicMethodctor4.cs b/src/System.Reflection.Emit.Lightweight/tests/DynamicMethodctor4.cs new file mode 100644 index 000000000000..be1bcb27e547 --- /dev/null +++ b/src/System.Reflection.Emit.Lightweight/tests/DynamicMethodctor4.cs @@ -0,0 +1,167 @@ +// Copyright (c) Microsoft. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. + +using System; +using System.Reflection; +using System.Reflection.Emit; +using System.Threading; +using Xunit; + +namespace System.Reflection.Emit.Lightweight.Tests +{ + public class DynamicMethodctor4 + { + [Fact] + public void PosTest1() + { + Type owner = typeof(MethodTestClass4); + bool skipVisb = true; + DynamicMethod dynamicMeth = new DynamicMethod("Pos1method", typeof(int), new Type[] { typeof(int) }, owner, skipVisb); + MethodAttributes attributes = dynamicMeth.Attributes; + Assert.False(dynamicMeth == null || dynamicMeth.Name != "Pos1method" || attributes != (MethodAttributes.Public | MethodAttributes.Static)); + } + + [Fact] + public void PosTest2() + { + Type owner = typeof(MethodTestClass4); + bool skipVisb = false; + DynamicMethod dynamicMeth = new DynamicMethod("Pos2method", typeof(void), new Type[] { typeof(int), typeof(string) }, owner, skipVisb); + MethodAttributes attributes = dynamicMeth.Attributes; + Assert.False(dynamicMeth == null || dynamicMeth.Name != "Pos2method" || attributes != (MethodAttributes.Public | MethodAttributes.Static)); + } + + [Fact] + public void PosTest3() + { + Type owner = typeof(MethodTestClass4); + bool skipVisib = true; + DynamicMethod dynamicMeth = new DynamicMethod("Pos3method", typeof(string), new Type[] { typeof(int), typeof(string) }, owner, skipVisib); + MethodAttributes attributes = dynamicMeth.Attributes; + Assert.False(dynamicMeth == null || dynamicMeth.Name != "Pos3method" || attributes != (MethodAttributes.Static | MethodAttributes.Public)); + } + + [Fact] + public void NegTest1() + { + Type owner = typeof(MethodTestClass4); + bool[] skipVisbs = new bool[] { false, true }; + for (int i = 0; i < skipVisbs.Length; i++) + { + Assert.Throws(() => { DynamicMethod dynamicMeth = new DynamicMethod("Neg1method", typeof(void), new Type[] { null, typeof(string) }, owner, skipVisbs[i]); }); + } + } + + [Fact] + public void NegTest2() + { + Type owner = typeof(Array); + bool[] skipVisbs = new bool[] { false, true }; + for (int i = 0; i < skipVisbs.Length; i++) + { + Assert.Throws(() => { DynamicMethod dynamicMeth = new DynamicMethod("Neg2method", typeof(void), new Type[] { null, typeof(string) }, owner, skipVisbs[i]); }); + } + } + + [Fact] + public void NegTest3() + { + Type owner = typeof(MethodTestInterface2); + bool[] skipVisbs = new bool[] { false, true }; + for (int i = 0; i < skipVisbs.Length; i++) + { + Assert.Throws(() => + { + DynamicMethod dynamicMeth = new DynamicMethod("Neg3method", typeof(void), new Type[] { null, typeof(string) }, owner, skipVisbs[i]); + }); + } + } + + [Fact] + public void NegTest4() + { + Type[] genericTypes = typeof(MethodMyClass2<>).GetGenericArguments(); + Type owner = genericTypes[0]; + bool[] skipVisbs = new bool[] { false, true }; + for (int i = 0; i < skipVisbs.Length; i++) + { + Assert.Throws(() => + { + DynamicMethod dynamicMeth = new DynamicMethod("Neg4method", typeof(void), new Type[] { null, typeof(string) }, owner, skipVisbs[i]); + }); + } + } + + [Fact] + public void NegTest5() + { + Type owner = typeof(MethodTestClass4); + bool[] skipVisbs = new bool[] { false, true }; + for (int i = 0; i < skipVisbs.Length; i++) + { + Assert.Throws(() => + { + DynamicMethod dynamicMeth = new DynamicMethod(null, typeof(void), new Type[] { typeof(string) }, owner, skipVisbs[i]); + }); + } + } + + [Fact] + public void NegTest6() + { + Type owner = null; + bool[] skipVisbs = new bool[] { false, true }; + for (int i = 0; i < skipVisbs.Length; i++) + { + Assert.Throws(() => + { + DynamicMethod dynamicMeth = new DynamicMethod("Neg6method", typeof(void), new Type[] { typeof(string) }, owner, skipVisbs[i]); + }); + } + } + + [Fact] + public void NegTest7() + { + Type returnType = CreateType(); + Type owner = typeof(MethodTestClass4); + bool[] skipVisbs = new bool[] { false, true }; + for (int i = 0; i < skipVisbs.Length; i++) + { + Assert.Throws(() => + { + DynamicMethod dynamicMeth = new DynamicMethod("Neg7method", returnType, new Type[] { typeof(string) }, owner, skipVisbs[i]); + }); + } + } + + private static Type CreateType() + { + AssemblyName assemName = new AssemblyName("assemName"); + AssemblyBuilder myAssemBuilder = AssemblyBuilder.DefineDynamicAssembly(assemName, AssemblyBuilderAccess.Run); + ModuleBuilder myModB = Utilities.GetModuleBuilder(myAssemBuilder, "Module1"); + TypeBuilder myTypeB = myModB.DefineType("testType"); + return myTypeB.MakeByRefType(); + } + } + + public class MethodTestClass4 + { + private int _id = 0; + public MethodTestClass4(int id) + { + _id = id; + } + public int ID + { + get + { + return _id; + } + } + } + + public interface MethodTestInterface2 { } + + public class MethodMyClass2 { } +} \ No newline at end of file diff --git a/src/System.Reflection.Emit.Lightweight/tests/DynamicMethodctor5.cs b/src/System.Reflection.Emit.Lightweight/tests/DynamicMethodctor5.cs new file mode 100644 index 000000000000..78afb7340be1 --- /dev/null +++ b/src/System.Reflection.Emit.Lightweight/tests/DynamicMethodctor5.cs @@ -0,0 +1,149 @@ +// Copyright (c) Microsoft. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. + +using System; +using System.Reflection; +using System.Reflection.Emit; +using System.Threading; +using Xunit; + +namespace System.Reflection.Emit.Lightweight.Tests +{ + public class DynamicMethodctor5 + { + [Fact] + public void PosTest1() + { + Module mod = typeof(MethodTestClass5).GetTypeInfo().Module; + bool skipVisb = true; + DynamicMethod dynamicMeth = new DynamicMethod("Pos1method", MethodAttributes.Public | MethodAttributes.Static, CallingConventions.Standard, typeof(void), null, mod, skipVisb); + MethodAttributes attributes = dynamicMeth.Attributes; + CallingConventions callCon = dynamicMeth.CallingConvention; + Assert.False(dynamicMeth == null || dynamicMeth.Name != "Pos1method" || attributes != (MethodAttributes.Public | MethodAttributes.Static) || callCon != CallingConventions.Standard); + } + + [Fact] + public void PosTest2() + { + Module mod = typeof(MethodTestClass5).GetTypeInfo().Module; + bool skipVisb = false; + DynamicMethod dynamicMeth = new DynamicMethod("Pos2method", MethodAttributes.Public | MethodAttributes.Static, CallingConventions.Standard, typeof(void), new Type[] { typeof(int), typeof(string) }, mod, skipVisb); + MethodAttributes attributes = dynamicMeth.Attributes; + CallingConventions callCon = dynamicMeth.CallingConvention; + Assert.False(dynamicMeth == null || dynamicMeth.Name != "Pos2method" || attributes != (MethodAttributes.Public | MethodAttributes.Static) || callCon != CallingConventions.Standard); + } + + [Fact] + public void PosTest3() + { + Module mod = typeof(MethodTestClass5).GetTypeInfo().Module; + bool skipVisib = true; + DynamicMethod dynamicMeth = new DynamicMethod("Pos3method", MethodAttributes.Public | MethodAttributes.Static, CallingConventions.Standard, typeof(string), new Type[] { typeof(int), typeof(string) }, mod, skipVisib); + MethodAttributes attributes = dynamicMeth.Attributes; + Assert.False(dynamicMeth == null || dynamicMeth.Name != "Pos3method" || attributes != (MethodAttributes.Static | MethodAttributes.Public) || dynamicMeth.CallingConvention != CallingConventions.Standard); + } + + [Fact] + public void NegTest1() + { + Module mod = typeof(MethodTestClass5).GetTypeInfo().Module; + bool[] skipVisibs = new bool[] { false, true }; + for (int i = 0; i < skipVisibs.Length; i++) + { + Assert.Throws(() => { DynamicMethod dynamicMeth = new DynamicMethod("Neg1method", MethodAttributes.Public | MethodAttributes.Static, CallingConventions.Standard, typeof(void), new Type[] { null, typeof(string) }, mod, skipVisibs[i]); }); + } + } + + [Fact] + public void NegTest2() + { + Module mod = typeof(MethodTestClass5).GetTypeInfo().Module; + bool[] skipVisibs = new bool[] { false, true }; + for (int i = 0; i < skipVisibs.Length; i++) + { + Assert.Throws(() => { DynamicMethod dynamicMeth = new DynamicMethod("Neg2method", MethodAttributes.Public | MethodAttributes.Static, CallingConventions.Standard, typeof(void), new Type[] { typeof(void), typeof(string) }, mod, skipVisibs[i]); }); + } + } + + [Fact] + public void NegTest3() + { + Module mod = typeof(MethodTestClass5).GetTypeInfo().Module; + bool[] skipVisibs = new bool[] { false, true }; + for (int i = 0; i < skipVisibs.Length; i++) + { + Assert.Throws(() => { DynamicMethod dynamicMeth = new DynamicMethod(null, MethodAttributes.Public | MethodAttributes.Static, CallingConventions.Standard, typeof(void), new Type[] { typeof(string) }, mod, skipVisibs[i]); }); + } + } + + [Fact] + public void NegTest4() + { + Module mod = null; + bool[] skipVisibs = new bool[] { false, true }; + for (int i = 0; i < skipVisibs.Length; i++) + { + Assert.Throws(() => { DynamicMethod dynamicMeth = new DynamicMethod("Neg4method", MethodAttributes.Public | MethodAttributes.Static, CallingConventions.Standard, typeof(void), new Type[] { typeof(string) }, mod, skipVisibs[i]); }); + } + } + + [Fact] + public void NegTest5() + { + Module mod = typeof(MethodTestClass5).GetTypeInfo().Module; + bool[] skipVisibs = new bool[] { false, true }; + Type tpA = CreateType(); + for (int i = 0; i < skipVisibs.Length; i++) + { + Assert.Throws(() => { DynamicMethod dynamicMeth = new DynamicMethod("Neg4method", MethodAttributes.Public | MethodAttributes.Static, CallingConventions.Standard, tpA, new Type[] { typeof(string) }, mod, skipVisibs[i]); }); + } + } + + [Fact] + public void NegTest6() + { + Module mod = typeof(MethodTestClass5).GetTypeInfo().Module; + bool[] skipVisibs = new bool[] { false, true }; + for (int i = 0; i < skipVisibs.Length; i++) + { + Assert.Throws(() => { DynamicMethod dynamicMeth = new DynamicMethod("Neg6method", MethodAttributes.Private | MethodAttributes.Virtual, CallingConventions.Standard, typeof(void), new Type[] { typeof(string) }, mod, skipVisibs[i]); }); + } + } + + [Fact] + public void NegTest7() + { + Module mod = typeof(MethodTestClass5).GetTypeInfo().Module; + bool[] skipVisibs = new bool[] { false, true }; + for (int i = 0; i < skipVisibs.Length; i++) + { + Assert.Throws(() => { DynamicMethod dynamicMeth = new DynamicMethod("Neg7method", MethodAttributes.Public | MethodAttributes.Static, CallingConventions.VarArgs, typeof(void), new Type[] { typeof(string) }, mod, skipVisibs[i]); }); + } + } + + private static Type CreateType() + { + AssemblyName assemName = new AssemblyName("assemName"); + AssemblyBuilder myAssemBuilder = AssemblyBuilder.DefineDynamicAssembly(assemName, AssemblyBuilderAccess.Run); + ModuleBuilder myModB = Utilities.GetModuleBuilder(myAssemBuilder, "Module1"); + TypeBuilder myTypeB = myModB.DefineType("testType"); + return myTypeB.MakeByRefType(); + } + } + + public class MethodTestClass5 + { + private int _id = 0; + public MethodTestClass5(int id) + { + _id = id; + } + public int ID + { + get + { + return _id; + } + } + } +} \ No newline at end of file diff --git a/src/System.Reflection.Emit.Lightweight/tests/DynamicMethodctor6.cs b/src/System.Reflection.Emit.Lightweight/tests/DynamicMethodctor6.cs new file mode 100644 index 000000000000..6431aba4b190 --- /dev/null +++ b/src/System.Reflection.Emit.Lightweight/tests/DynamicMethodctor6.cs @@ -0,0 +1,201 @@ +// Copyright (c) Microsoft. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. + +using System; +using System.Reflection; +using System.Reflection.Emit; +using System.Threading; +using Xunit; + +namespace System.Reflection.Emit.Lightweight.Tests +{ + public class DynamicMethodctor6 + { + [Fact] + public void PosTest1() + { + Type owner = typeof(MethodTestClass6); + bool skipVisb = true; + DynamicMethod dynamicMeth = new DynamicMethod("Pos1method", MethodAttributes.Public | MethodAttributes.Static, CallingConventions.Standard, typeof(void), null, owner, skipVisb); + MethodAttributes attributes = dynamicMeth.Attributes; + Assert.False(dynamicMeth == null || dynamicMeth.Name != "Pos1method" || attributes != (MethodAttributes.Public | MethodAttributes.Static) || dynamicMeth.CallingConvention != CallingConventions.Standard); + } + + [Fact] + public void PosTest2() + { + Type owner = typeof(MethodTestClass6); + bool skipVisb = false; + DynamicMethod dynamicMeth = new DynamicMethod("Pos2method", MethodAttributes.Public | MethodAttributes.Static, CallingConventions.Standard, typeof(void), new Type[] { typeof(int), typeof(string) }, owner, skipVisb); + MethodAttributes attributes = dynamicMeth.Attributes; + Assert.False(dynamicMeth == null || dynamicMeth.Name != "Pos2method" || attributes != (MethodAttributes.Public | MethodAttributes.Static) || dynamicMeth.CallingConvention != CallingConventions.Standard); + } + + [Fact] + public void PosTest3() + { + Type owner = typeof(MethodTestClass6); + bool skipVisib = true; + DynamicMethod dynamicMeth = new DynamicMethod("Pos3method", MethodAttributes.Public | MethodAttributes.Static, CallingConventions.Standard, typeof(string), new Type[] { typeof(int), typeof(string) }, owner, skipVisib); + MethodAttributes attributes = dynamicMeth.Attributes; + Assert.False(dynamicMeth == null || dynamicMeth.Name != "Pos3method" || attributes != (MethodAttributes.Static | MethodAttributes.Public) || dynamicMeth.CallingConvention != CallingConventions.Standard); + } + + [Fact] + public void NegTest1() + { + Type owner = typeof(MethodTestClass6); + bool[] skipVisbs = new bool[] { false, true }; + for (int i = 0; i < skipVisbs.Length; i++) + { + Assert.Throws(() => + { + DynamicMethod dynamicMeth = new DynamicMethod("Neg1method", MethodAttributes.Public | MethodAttributes.Static, CallingConventions.Standard, typeof(void), new Type[] { null, typeof(string) }, owner, skipVisbs[i]); + }); + } + } + + [Fact] + public void NegTest2() + { + Type owner = typeof(Array); + bool[] skipVisbs = new bool[] { false, true }; + for (int i = 0; i < skipVisbs.Length; i++) + { + Assert.Throws(() => + { + DynamicMethod dynamicMeth = new DynamicMethod("Neg2method", MethodAttributes.Public | MethodAttributes.Static, CallingConventions.Standard, typeof(void), new Type[] { null, typeof(string) }, owner, skipVisbs[i]); + }); + } + } + + [Fact] + public void NegTest3() + { + Type owner = typeof(MethodTestInterface6); + bool[] skipVisbs = new bool[] { false, true }; + for (int i = 0; i < skipVisbs.Length; i++) + { + Assert.Throws(() => + { + DynamicMethod dynamicMeth = new DynamicMethod("Neg3method", MethodAttributes.Public | MethodAttributes.Static, CallingConventions.Standard, typeof(void), new Type[] { null, typeof(string) }, owner, skipVisbs[i]); + }); + } + } + + [Fact] + public void NegTest4() + { + Type[] genericTypes = typeof(MethodMyClass6<>).GetGenericArguments(); + Type owner = genericTypes[0]; + bool[] skipVisbs = new bool[] { false, true }; + for (int i = 0; i < skipVisbs.Length; i++) + { + Assert.Throws(() => + { + DynamicMethod dynamicMeth = new DynamicMethod("Neg4method", MethodAttributes.Public | MethodAttributes.Static, CallingConventions.Standard, typeof(void), new Type[] { null, typeof(string) }, owner, skipVisbs[i]); + }); + } + } + + [Fact] + public void NegTest5() + { + Type owner = typeof(MethodTestClass6); + bool[] skipVisbs = new bool[] { false, true }; + for (int i = 0; i < skipVisbs.Length; i++) + { + Assert.Throws(() => + { + DynamicMethod dynamicMeth = new DynamicMethod(null, MethodAttributes.Public | MethodAttributes.Static, CallingConventions.Standard, typeof(void), new Type[] { typeof(string) }, owner, skipVisbs[i]); + }); + } + } + + [Fact] + public void NegTest6() + { + Type owner = null; + bool[] skipVisbs = new bool[] { false, true }; + for (int i = 0; i < skipVisbs.Length; i++) + { + Assert.Throws(() => { DynamicMethod dynamicMeth = new DynamicMethod("Neg6method", MethodAttributes.Public | MethodAttributes.Static, CallingConventions.Standard, typeof(void), new Type[] { typeof(string) }, owner, skipVisbs[i]); }); + } + } + + [Fact] + public void NegTest7() + { + Type returnType = CreateType(); + Type owner = typeof(MethodTestClass6); + bool[] skipVisbs = new bool[] { false, true }; + for (int i = 0; i < skipVisbs.Length; i++) + { + Assert.Throws(() => + { + DynamicMethod dynamicMeth = new DynamicMethod("Neg7method", MethodAttributes.Public | MethodAttributes.Static, CallingConventions.Standard, returnType, new Type[] { typeof(string) }, owner, skipVisbs[i]); + }); + } + } + + [Fact] + public void NegTest8() + { + Type owner = typeof(MethodTestClass6); + bool[] skipVisibs = new bool[] { false, true }; + for (int i = 0; i < skipVisibs.Length; i++) + { + Assert.Throws(() => + { + DynamicMethod dynamicMeth = new DynamicMethod("Neg6method", MethodAttributes.Private | MethodAttributes.Virtual, CallingConventions.Standard, typeof(void), new Type[] { typeof(string) }, owner, skipVisibs[i]); + }); + } + } + + [Fact] + public void NegTest9() + { + Type owner = typeof(MethodTestClass6); + bool[] skipVisibs = new bool[] { false, true }; + for (int i = 0; i < skipVisibs.Length; i++) + { + Assert.Throws(() => + { + DynamicMethod dynamicMeth = new DynamicMethod("Neg7method", MethodAttributes.Public | MethodAttributes.Static, CallingConventions.VarArgs, typeof(void), new Type[] { typeof(string) }, owner, skipVisibs[i]); + }); + } + } + + private static Type CreateType() + { + AssemblyName assemName = new AssemblyName("assemName"); + AssemblyBuilder myAssemBuilder = AssemblyBuilder.DefineDynamicAssembly(assemName, AssemblyBuilderAccess.Run); + ModuleBuilder myModB = Utilities.GetModuleBuilder(myAssemBuilder, "Module1"); + TypeBuilder myTypeB = myModB.DefineType("testType"); + return myTypeB.MakeByRefType(); + } + } + + public class MethodTestClass6 + { + private int _id = 0; + public MethodTestClass6(int id) + { + _id = id; + } + + public int ID + { + get + { + return _id; + } + } + } + + + public interface MethodTestInterface6 { } + + + public class MethodMyClass6 { } +} \ No newline at end of file diff --git a/src/System.Reflection.Emit.Lightweight/tests/System.Reflection.Emit.Lightweight.Tests.csproj b/src/System.Reflection.Emit.Lightweight/tests/System.Reflection.Emit.Lightweight.Tests.csproj new file mode 100644 index 000000000000..5d2156e2378f --- /dev/null +++ b/src/System.Reflection.Emit.Lightweight/tests/System.Reflection.Emit.Lightweight.Tests.csproj @@ -0,0 +1,45 @@ + + + + + Debug + AnyCPU + Library + System.Reflection.Emit.Lightweight.Tests + System.Reflection.Emit.Lightweight.Tests + {C338DCF7-FB75-407B-A2ED-117FBF3AAA18} + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/src/System.Reflection.Emit.Lightweight/tests/Utilities.cs b/src/System.Reflection.Emit.Lightweight/tests/Utilities.cs new file mode 100644 index 000000000000..f402705ce636 --- /dev/null +++ b/src/System.Reflection.Emit.Lightweight/tests/Utilities.cs @@ -0,0 +1,13 @@ +// Copyright (c) Microsoft. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. + +namespace System.Reflection.Emit.Lightweight.Tests +{ + internal class Utilities + { + public static ModuleBuilder GetModuleBuilder(AssemblyBuilder asmBuild, string moduleName) + { + return asmBuild.DefineDynamicModule(moduleName); + } + } +} diff --git a/src/System.Reflection.Emit.Lightweight/tests/project.json b/src/System.Reflection.Emit.Lightweight/tests/project.json new file mode 100644 index 000000000000..2add36cba7e7 --- /dev/null +++ b/src/System.Reflection.Emit.Lightweight/tests/project.json @@ -0,0 +1,18 @@ +{ + "dependencies": { + "System.Diagnostics.Debug": "4.0.10-beta-*", + "System.Reflection.Emit": "4.0.0-beta-*", + "System.Runtime": "4.0.20-beta-*", + "System.Runtime.Extensions": "4.0.10-beta-*", + "System.Reflection.Emit.Lightweight": "4.0.0-beta-*", + "System.Reflection.TypeExtensions": "4.0.0-beta-*", + "xunit": "2.0.0-beta5-build2785", + "xunit.abstractions.netcore": "1.0.0-prerelease", + "xunit.netcore.extensions": "1.0.0-prerelease-*", + "xunit.assert": "2.0.0-beta5-build2785", + "xunit.core.netcore": "1.0.1-prerelease" + }, + "frameworks": { + "dnxcore50": { } + } +}