Imported Upstream version 5.0.0.42

Former-commit-id: fd56571888259555122d8a0f58c68838229cea2b
This commit is contained in:
Xamarin Public Jenkins (auto-signing)
2017-04-10 11:41:01 +00:00
parent 1190d13a04
commit 6bdd276d05
19939 changed files with 3099680 additions and 93811 deletions

View File

@@ -0,0 +1,25 @@
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}"
ProjectSection(ProjectDependencies) = postProject
{} = {}
EndProjectSection
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
DebugNETCoreAppnetstandard1.3netstandard1.3|AnyCPU = DebugNETCoreAppnetstandard1.3netstandard1.3|AnyCPU
ReleaseNETCoreAppnetstandard1.3netstandard1.3|AnyCPU = ReleaseNETCoreAppnetstandard1.3netstandard1.3|AnyCPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{C338DCF7-FB75-407B-A2ED-117FBF3AAA18}.DebugNETCoreAppnetstandard1.3netstandard1.3|AnyCPU.ActiveCfg = netstandard1.3-Debug|Any CPU
{C338DCF7-FB75-407B-A2ED-117FBF3AAA18}.DebugNETCoreAppnetstandard1.3netstandard1.3|AnyCPU.Build.0 = netstandard1.3-Debug|Any CPU
{C338DCF7-FB75-407B-A2ED-117FBF3AAA18}.ReleaseNETCoreAppnetstandard1.3netstandard1.3|AnyCPU.ActiveCfg = netstandard1.3-Release|Any CPU
{C338DCF7-FB75-407B-A2ED-117FBF3AAA18}.ReleaseNETCoreAppnetstandard1.3netstandard1.3|AnyCPU.Build.0 = netstandard1.3-Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
EndGlobal

View File

@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="..\dir.props" />
<PropertyGroup>
<AssemblyVersion>4.0.3.0</AssemblyVersion>
<IsNETCoreApp>true</IsNETCoreApp>
</PropertyGroup>
</Project>

View File

@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<BuildConfigurations>
netcoreapp;
uap;
</BuildConfigurations>
</PropertyGroup>
</Project>

View File

@@ -0,0 +1,55 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
// ------------------------------------------------------------------------------
// Changes to this file must follow the http://aka.ms/api-review process.
// ------------------------------------------------------------------------------
namespace System.Reflection.Emit
{
public sealed partial class DynamicMethod : System.Reflection.MethodInfo
{
[System.Security.SecuritySafeCriticalAttribute]
public DynamicMethod(string name, System.Reflection.MethodAttributes attributes, System.Reflection.CallingConventions callingConvention, System.Type returnType, System.Type[] parameterTypes, System.Reflection.Module m, bool skipVisibility) { }
[System.Security.SecuritySafeCriticalAttribute]
public DynamicMethod(string name, System.Reflection.MethodAttributes attributes, System.Reflection.CallingConventions callingConvention, System.Type returnType, System.Type[] parameterTypes, System.Type owner, bool skipVisibility) { }
[System.Security.SecuritySafeCriticalAttribute]
public DynamicMethod(string name, System.Type returnType, System.Type[] parameterTypes) { }
[System.Security.SecuritySafeCriticalAttribute]
public DynamicMethod(string name, System.Type returnType, System.Type[] parameterTypes, bool restrictedSkipVisibility) { }
[System.Security.SecuritySafeCriticalAttribute]
public DynamicMethod(string name, System.Type returnType, System.Type[] parameterTypes, System.Reflection.Module m) { }
[System.Security.SecuritySafeCriticalAttribute]
public DynamicMethod(string name, System.Type returnType, System.Type[] parameterTypes, System.Reflection.Module m, bool skipVisibility) { }
[System.Security.SecuritySafeCriticalAttribute]
public DynamicMethod(string name, System.Type returnType, System.Type[] parameterTypes, System.Type owner) { }
[System.Security.SecuritySafeCriticalAttribute]
public DynamicMethod(string name, System.Type returnType, System.Type[] parameterTypes, System.Type owner, bool skipVisibility) { }
public override System.Reflection.MethodAttributes Attributes { get { throw null; } }
public override System.Reflection.CallingConventions CallingConvention { get { throw null; } }
public override System.Type DeclaringType { get { throw null; } }
public bool InitLocals { get { throw null; } set { } }
public override System.RuntimeMethodHandle MethodHandle { get { throw null; } }
public override string Name { get { throw null; } }
public override System.Type ReflectedType { get { throw null; } }
public override System.Reflection.ParameterInfo ReturnParameter { get { throw null; } }
public override System.Type ReturnType { get { throw null; } }
public override System.Reflection.ICustomAttributeProvider ReturnTypeCustomAttributes { get { throw null; } }
[System.Security.SecuritySafeCriticalAttribute]
public sealed override System.Delegate CreateDelegate(System.Type delegateType) { throw null; }
[System.Security.SecuritySafeCriticalAttribute]
public sealed override System.Delegate CreateDelegate(System.Type delegateType, object target) { throw null; }
public override System.Reflection.MethodInfo GetBaseDefinition() { throw null; }
public override object[] GetCustomAttributes(bool inherit) { throw null; }
public override object[] GetCustomAttributes(System.Type attributeType, bool inherit) { throw null; }
public System.Reflection.Emit.ILGenerator GetILGenerator() { throw null; }
[System.Security.SecuritySafeCriticalAttribute]
public System.Reflection.Emit.ILGenerator GetILGenerator(int streamSize) { throw null; }
public override System.Reflection.MethodImplAttributes GetMethodImplementationFlags() { throw null; }
public override System.Reflection.ParameterInfo[] GetParameters() { throw null; }
public override object Invoke(object obj, System.Reflection.BindingFlags invokeAttr, System.Reflection.Binder binder, object[] parameters, System.Globalization.CultureInfo culture) { throw null; }
public override bool IsDefined(System.Type attributeType, bool inherit) { throw null; }
public override string ToString() { throw null; }
}
}

View File

@@ -0,0 +1,16 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="14.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" DefaultTargets="Build">
<Import Project="$([MSBuild]::GetDirectoryNameOfFileAbove($(MSBuildThisFileDirectory), dir.props))\dir.props" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'netcoreapp-Debug|AnyCPU'" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'netcoreapp-Release|AnyCPU'" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'uap-Debug|AnyCPU'" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'uap-Release|AnyCPU'" />
<ItemGroup>
<Compile Include="System.Reflection.Emit.Lightweight.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\System.Runtime\ref\System.Runtime.csproj" />
<ProjectReference Include="..\..\System.Reflection.Emit.ILGeneration\ref\System.Reflection.Emit.ILGeneration.csproj" />
</ItemGroup>
<Import Project="$([MSBuild]::GetDirectoryNameOfFileAbove($(MSBuildThisFileDirectory), dir.targets))\dir.targets" />
</Project>

View File

@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<BuildConfigurations>
netcoreapp;
uap;
</BuildConfigurations>
</PropertyGroup>
</Project>

View File

@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$([MSBuild]::GetDirectoryNameOfFileAbove($(MSBuildThisFileDirectory), dir.props))\dir.props" />
<PropertyGroup>
<AssemblyName>System.Reflection.Emit.Lightweight</AssemblyName>
<IsPartialFacadeAssembly>true</IsPartialFacadeAssembly>
</PropertyGroup>
<!-- Help VS understand available configurations -->
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'netcoreapp-Debug|AnyCPU'" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'netcoreapp-Release|AnyCPU'" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'uap-Debug|AnyCPU'" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'uap-Release|AnyCPU'" />
<ItemGroup>
<ReferenceFromRuntime Include="System.Private.CoreLib" />
</ItemGroup>
<Import Project="$([MSBuild]::GetDirectoryNameOfFileAbove($(MSBuildThisFileDirectory), dir.targets))\dir.targets" />
</Project>

View File

@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<BuildConfigurations>
netstandard1.3;
</BuildConfigurations>
</PropertyGroup>
</Project>

View File

@@ -0,0 +1,138 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System.Collections.Generic;
using Xunit;
namespace System.Reflection.Emit.Tests
{
public class DynamicMethodCreateDelegateTests
{
private const string FieldName = "_id";
public static IEnumerable<object[]> Targets_TestData()
{
yield return new object[] { new IDClass() };
yield return new object[] { new IDSubClass() };
}
[Theory]
[MemberData(nameof(Targets_TestData))]
public void CreateDelegate_Target_Type(IDClass target)
{
int newId = 0;
FieldInfo field = typeof(IDClass).GetField(FieldName, BindingFlags.NonPublic | BindingFlags.Instance);
DynamicMethod method = new DynamicMethod("Method", typeof(int), new Type[] { typeof(IDClass), typeof(int) }, typeof(IDClass));
ILGenerator ilGenerator = method.GetILGenerator();
Helpers.EmitMethodBody(ilGenerator, field);
IntDelegate instanceCallBack = (IntDelegate)method.CreateDelegate(typeof(IntDelegate), target);
Assert.Equal(instanceCallBack(newId), target.ID);
Assert.Equal(newId, target.ID);
}
[Theory]
[MemberData(nameof(Targets_TestData))]
public void CreateDelegate_Target_Module(IDClass target)
{
Module module = typeof(TestClass).GetTypeInfo().Module;
int newId = 0;
FieldInfo field = typeof(IDClass).GetField(FieldName, BindingFlags.NonPublic | BindingFlags.Instance);
DynamicMethod method = new DynamicMethod("Method", typeof(int), new Type[] { typeof(IDClass), typeof(int) }, module, true);
ILGenerator ilGenerator = method.GetILGenerator();
Helpers.EmitMethodBody(ilGenerator, field);
IntDelegate instanceCallBack = (IntDelegate)method.CreateDelegate(typeof(IntDelegate), target);
Assert.Equal(instanceCallBack(newId), target.ID);
Assert.Equal(newId, target.ID);
}
[Theory]
[MemberData(nameof(Targets_TestData))]
public void CreateDelegate_Type(IDClass target)
{
int newId = 0;
FieldInfo field = typeof(IDClass).GetField(FieldName, BindingFlags.NonPublic | BindingFlags.Instance);
DynamicMethod method = new DynamicMethod("Method", typeof(int), new Type[] { typeof(IDClass), typeof(int) }, typeof(IDClass));
ILGenerator ilGenerator = method.GetILGenerator();
Helpers.EmitMethodBody(ilGenerator, field);
IDClassDelegate staticCallBack = (IDClassDelegate)method.CreateDelegate(typeof(IDClassDelegate));
Assert.Equal(staticCallBack(target, newId), target.ID);
Assert.Equal(newId, target.ID);
}
[Theory]
[MemberData(nameof(Targets_TestData))]
public void CreateDelegate_Module(IDClass target)
{
Module module = typeof(TestClass).GetTypeInfo().Module;
int newId = 0;
FieldInfo field = typeof(IDClass).GetField(FieldName, BindingFlags.NonPublic | BindingFlags.Instance);
DynamicMethod method = new DynamicMethod("Method", typeof(int), new Type[] { typeof(IDClass), typeof(int) }, module, true);
ILGenerator ilGenerator = method.GetILGenerator();
Helpers.EmitMethodBody(ilGenerator, field);
IDClassDelegate staticCallBack = (IDClassDelegate)method.CreateDelegate(typeof(IDClassDelegate));
Assert.Equal(staticCallBack(target, newId), target.ID);
Assert.Equal(newId, target.ID);
}
[Fact]
public void CreateDelegate_NoMethodBody_ThrowsInvalidOperationException()
{
IDClass target = new IDClass();
DynamicMethod method = new DynamicMethod("Method", typeof(int), new Type[] { typeof(IDClass), typeof(int) }, typeof(IDClass));
Assert.Throws<InvalidOperationException>(() => method.CreateDelegate(typeof(IntDelegate)));
Assert.Throws<InvalidOperationException>(() => method.CreateDelegate(typeof(IntDelegate), target));
}
[Fact]
public void CreateDelegate_InvalidTarget_ThrowsArgumentException()
{
FieldInfo field = typeof(IDClass).GetField(FieldName, BindingFlags.NonPublic | BindingFlags.Instance);
DynamicMethod method = new DynamicMethod("Method", typeof(int), new Type[] { typeof(IDClass), typeof(int) }, typeof(IDClass));
ILGenerator ilGenerator = method.GetILGenerator();
Helpers.EmitMethodBody(ilGenerator, field);
Assert.Throws<ArgumentException>(null, () => method.CreateDelegate(typeof(IntDelegate), "foo"));
}
[Theory]
[InlineData(typeof(InvalidRetType))]
[InlineData(typeof(WrongParamNumber))]
[InlineData(typeof(InvalidParamType))]
public void CreateDelegate_DelegateTypeInvalid_ThrowsArgumentException(Type delegateType)
{
FieldInfo field = typeof(IDClass).GetField(FieldName, BindingFlags.NonPublic | BindingFlags.Instance);
DynamicMethod method = new DynamicMethod("Method", typeof(int), new Type[] { typeof(IDClass), typeof(int) }, typeof(IDClass));
ILGenerator ilGenerator = method.GetILGenerator();
Helpers.EmitMethodBody(ilGenerator, field);
Assert.Throws<ArgumentException>(null, () => method.CreateDelegate(delegateType));
Assert.Throws<ArgumentException>(null, () => method.CreateDelegate(delegateType, new IDClass()));
}
}
public class IDSubClass : IDClass
{
public IDSubClass(int id) : base(id) { }
public IDSubClass() : base() { }
}
public delegate int IDClassDelegate(IDClass owner, int id);
public delegate IDClass InvalidRetType(int id);
public delegate int WrongParamNumber(int id, int m);
public delegate int InvalidParamType(IDClass owner);
}

View File

@@ -0,0 +1,175 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using Xunit;
namespace System.Reflection.Emit.Tests
{
public class DynamicMethodctor1
{
[Theory]
[InlineData("Method", typeof(void), null)]
[InlineData("Method", typeof(void), new Type[] { typeof(int), typeof(string) })]
[InlineData("Method", typeof(string), null)]
[InlineData("Method", typeof(string), new Type[] { typeof(int), typeof(string) })]
[InlineData("", typeof(string), new Type[] { typeof(int), typeof(string) })]
[InlineData("Method", typeof(char?), null)]
[InlineData("Method", typeof(TestClass), null)]
[InlineData("Method", typeof(GenericClass<>), null)]
[InlineData("Method", typeof(TestInterface), null)]
[InlineData("Method", null, null)]
[InlineData("Method", typeof(int), new Type[] { typeof(int) })]
[InlineData("method", typeof(string), new Type[] { typeof(char?) })]
[InlineData("Method", typeof(string), new Type[] { typeof(GenericClass2<,>), typeof(GenericClass2<,>) })]
[InlineData("Method", typeof(string), new Type[] { typeof(TestInterface) })]
public void String_Type_TypeArray_Module(string name, Type returnType, Type[] parameterTypes)
{
Module module = typeof(TestClass).GetTypeInfo().Module;
DynamicMethod method1 = new DynamicMethod(name, returnType, parameterTypes, module);
Helpers.VerifyMethod(method1, name, MethodAttributes.Public | MethodAttributes.Static, CallingConventions.Standard, returnType, parameterTypes, module);
DynamicMethod method2 = new DynamicMethod(name, returnType, parameterTypes, module, true);
Helpers.VerifyMethod(method2, name, MethodAttributes.Public | MethodAttributes.Static, CallingConventions.Standard, returnType, parameterTypes, module);
DynamicMethod method3 = new DynamicMethod(name, returnType, parameterTypes, module, false);
Helpers.VerifyMethod(method3, name, MethodAttributes.Public | MethodAttributes.Static, CallingConventions.Standard, returnType, parameterTypes, module);
DynamicMethod method4 = new DynamicMethod(name, MethodAttributes.Public | MethodAttributes.Static, CallingConventions.Standard, returnType, parameterTypes, module, true);
Helpers.VerifyMethod(method4, name, MethodAttributes.Public | MethodAttributes.Static, CallingConventions.Standard, returnType, parameterTypes, module);
DynamicMethod method5 = new DynamicMethod(name, MethodAttributes.Public | MethodAttributes.Static, CallingConventions.Standard, returnType, parameterTypes, module, false);
Helpers.VerifyMethod(method5, name, MethodAttributes.Public | MethodAttributes.Static, CallingConventions.Standard, returnType, parameterTypes, module);
}
[Theory]
[InlineData("Method", typeof(void), null, typeof(TestClass))]
[InlineData("Method", typeof(void), new Type[] { typeof(int), typeof(string) }, typeof(TestClass))]
[InlineData("Method", typeof(string), null, typeof(TestClass))]
[InlineData("Method", typeof(string), new Type[] { typeof(int), typeof(string) }, typeof(TestClass))]
[InlineData("", typeof(string), new Type[] { typeof(int), typeof(string) }, typeof(TestClass))]
public void String_Type_TypeArray_Type(string name, Type returnType, Type[] parameterTypes, Type owner)
{
DynamicMethod method1 = new DynamicMethod(name, returnType, parameterTypes, owner);
Helpers.VerifyMethod(method1, name, MethodAttributes.Public | MethodAttributes.Static, CallingConventions.Standard, returnType, parameterTypes, owner.GetTypeInfo().Module);
DynamicMethod method2 = new DynamicMethod(name, returnType, parameterTypes, owner, true);
Helpers.VerifyMethod(method2, name, MethodAttributes.Public | MethodAttributes.Static, CallingConventions.Standard, returnType, parameterTypes, owner.GetTypeInfo().Module);
DynamicMethod method3 = new DynamicMethod(name, returnType, parameterTypes, owner, false);
Helpers.VerifyMethod(method3, name, MethodAttributes.Public | MethodAttributes.Static, CallingConventions.Standard, returnType, parameterTypes, owner.GetTypeInfo().Module);
DynamicMethod method4 = new DynamicMethod(name, MethodAttributes.Public | MethodAttributes.Static, CallingConventions.Standard, returnType, parameterTypes, owner, true);
Helpers.VerifyMethod(method4, name, MethodAttributes.Public | MethodAttributes.Static, CallingConventions.Standard, returnType, parameterTypes, owner.GetTypeInfo().Module);
DynamicMethod method5 = new DynamicMethod(name, MethodAttributes.Public | MethodAttributes.Static, CallingConventions.Standard, returnType, parameterTypes, owner, false);
Helpers.VerifyMethod(method5, name, MethodAttributes.Public | MethodAttributes.Static, CallingConventions.Standard, returnType, parameterTypes, owner.GetTypeInfo().Module);
}
[Fact]
public void NullObjectInParameterTypes_ThrowsArgumentException()
{
Module module = typeof(TestClass).GetTypeInfo().Module;
Assert.Throws<ArgumentException>(null, () => new DynamicMethod("Method", typeof(void), new Type[] { null, typeof(string) }, module));
Assert.Throws<ArgumentException>(null, () => new DynamicMethod("Method", typeof(void), new Type[] { null, typeof(string) }, module, true));
Assert.Throws<ArgumentException>(null, () => new DynamicMethod("Method", typeof(void), new Type[] { null, typeof(string) }, module, false));
Assert.Throws<ArgumentException>(null, () => new DynamicMethod("Method", MethodAttributes.Public | MethodAttributes.Static, CallingConventions.Standard, typeof(void), new Type[] { null, typeof(string) }, module, true));
Assert.Throws<ArgumentException>(null, () => new DynamicMethod("Method", MethodAttributes.Public | MethodAttributes.Static, CallingConventions.Standard, typeof(void), new Type[] { null, typeof(string) }, module, false));
Assert.Throws<ArgumentException>(null, () => new DynamicMethod("Method", typeof(void), new Type[] { null, typeof(string) }, typeof(TestClass)));
Assert.Throws<ArgumentException>(null, () => new DynamicMethod("Method", typeof(void), new Type[] { null, typeof(string) }, typeof(TestClass), true));
Assert.Throws<ArgumentException>(null, () => new DynamicMethod("Method", typeof(void), new Type[] { null, typeof(string) }, typeof(TestClass), false));
Assert.Throws<ArgumentException>(null, () => new DynamicMethod("Method", MethodAttributes.Public | MethodAttributes.Static, CallingConventions.Standard, typeof(void), new Type[] { null, typeof(string) }, typeof(TestClass), true));
Assert.Throws<ArgumentException>(null, () => new DynamicMethod("Method", MethodAttributes.Public | MethodAttributes.Static, CallingConventions.Standard, typeof(void), new Type[] { null, typeof(string) }, typeof(TestClass), false));
}
[Fact]
public void NullName_ThrowsArgumentNullException()
{
Module module = typeof(TestClass).GetTypeInfo().Module;
Assert.Throws<ArgumentNullException>("name", () => new DynamicMethod(null, typeof(void), new Type[0], module));
Assert.Throws<ArgumentNullException>("name", () => new DynamicMethod(null, typeof(void), new Type[0], module, true));
Assert.Throws<ArgumentNullException>("name", () => new DynamicMethod(null, typeof(void), new Type[0], module, false));
Assert.Throws<ArgumentNullException>("name", () => new DynamicMethod(null, MethodAttributes.Public | MethodAttributes.Static, CallingConventions.Standard, typeof(void), new Type[0], module, true));
Assert.Throws<ArgumentNullException>("name", () => new DynamicMethod(null, MethodAttributes.Public | MethodAttributes.Static, CallingConventions.Standard, typeof(void), new Type[0], module, false));
Assert.Throws<ArgumentNullException>("name", () => new DynamicMethod(null, typeof(void), new Type[0], typeof(TestClass)));
Assert.Throws<ArgumentNullException>("name", () => new DynamicMethod(null, typeof(void), new Type[0], typeof(TestClass), true));
Assert.Throws<ArgumentNullException>("name", () => new DynamicMethod(null, typeof(void), new Type[0], typeof(TestClass), false));
Assert.Throws<ArgumentNullException>("name", () => new DynamicMethod(null, MethodAttributes.Public | MethodAttributes.Static, CallingConventions.Standard, typeof(void), new Type[0], typeof(TestClass), true));
Assert.Throws<ArgumentNullException>("name", () => new DynamicMethod(null, MethodAttributes.Public | MethodAttributes.Static, CallingConventions.Standard, typeof(void), new Type[0], typeof(TestClass), false));
}
[Fact]
public void ByRefReturnType_ThrowsNotSupportedException()
{
Module module = typeof(TestClass).GetTypeInfo().Module;
Assert.Throws<NotSupportedException>(() => new DynamicMethod("Method", typeof(int).MakeByRefType(), new Type[0], module));
Assert.Throws<NotSupportedException>(() => new DynamicMethod("Method", typeof(int).MakeByRefType(), new Type[0], module, true));
Assert.Throws<NotSupportedException>(() => new DynamicMethod("Method", typeof(int).MakeByRefType(), new Type[0], module, false));
Assert.Throws<NotSupportedException>(() => new DynamicMethod("Method", MethodAttributes.Public | MethodAttributes.Static, CallingConventions.Standard, typeof(int).MakeByRefType(), new Type[0], module, true));
Assert.Throws<NotSupportedException>(() => new DynamicMethod("Method", MethodAttributes.Public | MethodAttributes.Static, CallingConventions.Standard, typeof(int).MakeByRefType(), new Type[0], module, false));
Assert.Throws<NotSupportedException>(() => new DynamicMethod("Method", typeof(int).MakeByRefType(), new Type[0], typeof(TestClass)));
Assert.Throws<NotSupportedException>(() => new DynamicMethod("Method", typeof(int).MakeByRefType(), new Type[0], typeof(TestClass), true));
Assert.Throws<NotSupportedException>(() => new DynamicMethod("Method", typeof(int).MakeByRefType(), new Type[0], typeof(TestClass), false));
Assert.Throws<NotSupportedException>(() => new DynamicMethod("Method", MethodAttributes.Public | MethodAttributes.Static, CallingConventions.Standard, typeof(int).MakeByRefType(), new Type[0], typeof(TestClass), true));
Assert.Throws<NotSupportedException>(() => new DynamicMethod("Method", MethodAttributes.Public | MethodAttributes.Static, CallingConventions.Standard, typeof(int).MakeByRefType(), new Type[0], typeof(TestClass), false));
}
[Fact]
public void NullModule_ThrowsArgumentNullException()
{
Assert.Throws<ArgumentNullException>("m", () => new DynamicMethod("Method", typeof(void), new Type[0], (Module)null));
Assert.Throws<ArgumentNullException>("m", () => new DynamicMethod("Method", typeof(void), new Type[0], (Module)null, true));
Assert.Throws<ArgumentNullException>("m", () => new DynamicMethod("Method", typeof(void), new Type[0], (Module)null, false));
Assert.Throws<ArgumentNullException>("m", () => new DynamicMethod("Method", MethodAttributes.Public | MethodAttributes.Static, CallingConventions.Standard, typeof(void), new Type[0], (Module)null, true));
Assert.Throws<ArgumentNullException>("m", () => new DynamicMethod("Method", MethodAttributes.Public | MethodAttributes.Static, CallingConventions.Standard, typeof(void), new Type[0], (Module)null, false));
}
[Fact]
public void NullOwner_ThrowsArgumentNullException()
{
Assert.Throws<ArgumentNullException>("owner", () => new DynamicMethod("Method", typeof(void), new Type[0], (Type)null));
Assert.Throws<ArgumentNullException>("owner", () => new DynamicMethod("Method", typeof(void), new Type[0], (Type)null, true));
Assert.Throws<ArgumentNullException>("owner", () => new DynamicMethod("Method", typeof(void), new Type[0], (Type)null, false));
Assert.Throws<ArgumentNullException>("owner", () => new DynamicMethod("Method", MethodAttributes.Public | MethodAttributes.Static, CallingConventions.Standard, typeof(void), new Type[0], (Type)null, true));
Assert.Throws<ArgumentNullException>("owner", () => new DynamicMethod("Method", MethodAttributes.Public | MethodAttributes.Static, CallingConventions.Standard, typeof(void), new Type[0], (Type)null, false));
}
[Theory]
[InlineData(typeof(int[]))]
[InlineData(typeof(TestInterface))]
[InlineData(typeof(GenericClass<>))]
[InlineData(typeof(int*))]
public void InvalidOwner_ThrowsArgumentException(Type owner)
{
Assert.Throws<ArgumentException>(null, () => new DynamicMethod("Method", typeof(void), new Type[0], owner));
Assert.Throws<ArgumentException>(null, () => new DynamicMethod("Method", typeof(void), new Type[0], owner, true));
Assert.Throws<ArgumentException>(null, () => new DynamicMethod("Method", typeof(void), new Type[0], owner, false));
Assert.Throws<ArgumentException>(null, () => new DynamicMethod("Method", MethodAttributes.Public | MethodAttributes.Static, CallingConventions.Standard, typeof(void), new Type[0], owner, true));
Assert.Throws<ArgumentException>(null, () => new DynamicMethod("Method", MethodAttributes.Public | MethodAttributes.Static, CallingConventions.Standard, typeof(void), new Type[0], owner, false));
}
}
}

View File

@@ -0,0 +1,36 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using Xunit;
namespace System.Reflection.Emit.Tests
{
public class DynamicMethodGetBaseDefinition
{
[Theory]
[InlineData(true)]
[InlineData(false)]
public void CreateDynMethod_TypeOwner(bool skipVisibility)
{
Type[] parameterTypes = new Type[] { typeof(TestClass), typeof(int) };
DynamicMethod method = new DynamicMethod("Method", typeof(int), parameterTypes, typeof(TestClass), skipVisibility);
MethodInfo baseDefinition = method.GetBaseDefinition();
Assert.Equal(method, baseDefinition);
}
[Theory]
[InlineData(true)]
[InlineData(false)]
public void CreateDynMethod_Module(bool skipVisibility)
{
Module module = typeof(TestClass).GetTypeInfo().Module;
Type[] parameterTypes = new Type[] { typeof(TestClass), typeof(int) };
DynamicMethod method = new DynamicMethod("Method", typeof(int), parameterTypes, module, skipVisibility);
MethodInfo baseDefinition = method.GetBaseDefinition();
Assert.Equal(method, baseDefinition);
}
}
}

View File

@@ -0,0 +1,106 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using Xunit;
namespace System.Reflection.Emit.Tests
{
public class DynamicMethodGetILGenerator1
{
private const string FieldName = "_id";
[Theory]
[InlineData(true)]
[InlineData(false)]
public void GetILGenerator_Int_Owner(bool skipVisibility)
{
IDClass target = new IDClass();
FieldInfo field = typeof(IDClass).GetField(FieldName, BindingFlags.Instance | BindingFlags.NonPublic);
Type[] paramTypes = new Type[] { typeof(IDClass), typeof(int) };
DynamicMethod method = new DynamicMethod("Method", typeof(int), paramTypes, typeof(IDClass), skipVisibility);
ILGenerator ilGenerator = method.GetILGenerator(8);
Helpers.EmitMethodBody(ilGenerator, field);
IntDelegate instanceCallBack = (IntDelegate)method.CreateDelegate(typeof(IntDelegate), target);
VerifyILGenerator(instanceCallBack, target, 0);
}
[Fact]
[SkipOnTargetFramework(TargetFrameworkMonikers.NetFramework, "The coreclr ignores the skipVisibility value of DynamicMethod.")]
public void GetILGenerator_Int_Module_CoreclrIgnoresSkipVisibility()
{
GetILGenerator_Int_Module(skipVisibility: false);
}
[Theory]
[InlineData(true)]
public void GetILGenerator_Int_Module(bool skipVisibility)
{
Module module = typeof(IDClass).GetTypeInfo().Module;
IDClass target = new IDClass();
FieldInfo field = typeof(IDClass).GetField(FieldName, BindingFlags.Instance | BindingFlags.NonPublic);
Type[] paramTypes = new Type[] { typeof(IDClass), typeof(int) };
DynamicMethod method = new DynamicMethod("Method", typeof(int), paramTypes, module, skipVisibility);
ILGenerator ilGenerator = method.GetILGenerator(8);
Helpers.EmitMethodBody(ilGenerator, field);
IntDelegate instanceCallBack = (IntDelegate)method.CreateDelegate(typeof(IntDelegate), target);
VerifyILGenerator(instanceCallBack, target, 0);
}
[Theory]
[InlineData(true)]
[InlineData(false)]
public void GetILGenerator_Owner(bool skipVisibility)
{
IDClass target = new IDClass();
FieldInfo field = typeof(IDClass).GetField(FieldName, BindingFlags.Instance | BindingFlags.NonPublic);
Type[] paramTypes = new Type[] { typeof(IDClass), typeof(int) };
DynamicMethod method = new DynamicMethod("MethodName", typeof(int), paramTypes, typeof(IDClass), skipVisibility);
ILGenerator ilGenerator = method.GetILGenerator();
Helpers.EmitMethodBody(ilGenerator, field);
IntDelegate instanceCallBack = (IntDelegate)method.CreateDelegate(typeof(IntDelegate), target);
VerifyILGenerator(instanceCallBack, target, 0);
}
[Fact]
[SkipOnTargetFramework(TargetFrameworkMonikers.NetFramework, "The coreclr ignores the skipVisibility value of DynamicMethod.")]
public void GetILGenerator_Module_CoreclrIgnoresSkipVisibility()
{
GetILGenerator_Module(skipVisibility: false);
}
[Theory]
[InlineData(true)]
public void GetILGenerator_Module(bool skipVisibility)
{
Module module = typeof(TestClass).GetTypeInfo().Module;
IDClass target = new IDClass();
FieldInfo field = typeof(IDClass).GetField(FieldName, BindingFlags.Instance | BindingFlags.NonPublic);
Type[] paramTypes = new Type[] { typeof(IDClass), typeof(int) };
DynamicMethod method = new DynamicMethod("Method", typeof(int), paramTypes, module, skipVisibility);
ILGenerator ilGenerator = method.GetILGenerator();
Helpers.EmitMethodBody(ilGenerator, field);
IntDelegate instanceCallBack = (IntDelegate)method.CreateDelegate(typeof(IntDelegate), target);
VerifyILGenerator(instanceCallBack, target, 0);
}
private void VerifyILGenerator(IntDelegate instanceCallBack, IDClass target, int newId)
{
Assert.Equal(instanceCallBack(newId), target.ID);
Assert.Equal(newId, target.ID);
}
}
}

View File

@@ -0,0 +1,21 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using Xunit;
namespace System.Reflection.Emit.Tests
{
public class DynamicMethodInitLocals
{
[Theory]
[InlineData(true)]
[InlineData(false)]
public void InitLocals_Set_ReturnsNewValue(bool newInitLocals)
{
DynamicMethod method = new DynamicMethod("Method", MethodAttributes.Static | MethodAttributes.Public, CallingConventions.Standard, null, null, typeof(TestClass).GetTypeInfo().Module, true);
method.InitLocals = newInitLocals;
Assert.Equal(newInitLocals, method.InitLocals);
}
}
}

View File

@@ -0,0 +1,21 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using Xunit;
namespace System.Reflection.Emit.Tests
{
public class DynamicMethodToString
{
[Theory]
[InlineData(typeof(string), new Type[] { typeof(string), typeof(int), typeof(TestClass) }, "System.String MethodName(System.String, Int32, System.Reflection.Emit.Tests.TestClass)")]
[InlineData(typeof(string), new Type[] { typeof(GenericClass<>) }, "System.String MethodName(System.Reflection.Emit.Tests.GenericClass`1[T])")]
[InlineData(null, null, "Void MethodName()")]
public void ToStringTest(Type returnType, Type[] parameterTypes, string expected)
{
DynamicMethod method = new DynamicMethod("MethodName", returnType, parameterTypes, typeof(TestClass).GetTypeInfo().Module);
Assert.Equal(expected, method.ToString());
}
}
}

View File

@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$([MSBuild]::GetDirectoryNameOfFileAbove($(MSBuildThisFileDirectory), dir.props))\dir.props" />
<ItemGroup>
<Project Include="System.Reflection.Emit.Lightweight.Tests.csproj" />
<Project Include="System.Reflection.Emit.Lightweight.Tests.csproj">
<OSGroup>Windows_NT</OSGroup>
<TestTFMs>net46</TestTFMs>
</Project>
</ItemGroup>
<Import Project="$([MSBuild]::GetDirectoryNameOfFileAbove($(MSBuildThisFileDirectory), dir.traversal.targets))\dir.traversal.targets" />
</Project>

View File

@@ -0,0 +1,20 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$([MSBuild]::GetDirectoryNameOfFileAbove($(MSBuildThisFileDirectory), dir.props))\dir.props" />
<PropertyGroup>
<ProjectGuid>{C338DCF7-FB75-407B-A2ED-117FBF3AAA18}</ProjectGuid>
</PropertyGroup>
<!-- Default configurations to help VS understand the configurations -->
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='netstandard1.3-Debug|AnyCPU'" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='netstandard1.3-Release|AnyCPU'" />
<ItemGroup>
<Compile Include="DynamicMethodCreateDelegate.cs" />
<Compile Include="DynamicMethodCtor.cs" />
<Compile Include="DynamicMethodGetBaseDefinition.cs" />
<Compile Include="DynamicMethodGetILGenerator.cs" />
<Compile Include="DynamicMethodInitLocals.cs" />
<Compile Include="DynamicMethodToString.cs" />
<Compile Include="Utilities.cs" />
</ItemGroup>
<Import Project="$([MSBuild]::GetDirectoryNameOfFileAbove($(MSBuildThisFileDirectory), dir.targets))\dir.targets" />
</Project>

View File

@@ -0,0 +1,76 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using Xunit;
namespace System.Reflection.Emit.Tests
{
public class Helpers
{
public static ModuleBuilder GetModuleBuilder(AssemblyBuilder asmBuild, string moduleName)
{
return asmBuild.DefineDynamicModule(moduleName);
}
public static void VerifyMethod(DynamicMethod method, string name, MethodAttributes attributes, CallingConventions callingConvention, Type returnType, Type[] parameterTypes, Module module)
{
Assert.Equal(name, method.Name);
Assert.Equal(attributes, method.Attributes);
Assert.Equal(callingConvention, method.CallingConvention);
Assert.Equal(returnType ?? typeof(void), method.ReturnType);
Assert.Null(method.ReturnParameter);
ParameterInfo[] parameters = method.GetParameters();
if (parameterTypes == null)
{
Assert.Empty(parameters);
}
else
{
Assert.Equal(parameterTypes.Length, parameters.Length);
for (int i = 0; i < parameterTypes.Length; i++)
{
ParameterInfo parameter = parameters[i];
Assert.Equal(parameterTypes[i], parameter.ParameterType);
Assert.Equal(i, parameter.Position);
}
}
Assert.Same(module, method.Module);
Assert.Null(method.DeclaringType);
Assert.True(method.InitLocals);
}
public static void EmitMethodBody(ILGenerator ilGenerator, FieldInfo field)
{
ilGenerator.Emit(OpCodes.Ldarg_0);
ilGenerator.Emit(OpCodes.Ldfld, field);
ilGenerator.Emit(OpCodes.Ldarg_0);
ilGenerator.Emit(OpCodes.Ldarg_1);
ilGenerator.Emit(OpCodes.Stfld, field);
ilGenerator.Emit(OpCodes.Ret);
}
}
public class TestClass { }
public class GenericClass<T> { }
public class GenericClass2<T, U> { }
public interface TestInterface { }
public class IDClass
{
private int _id;
public IDClass(int id) { _id = id; }
public IDClass() : this(0) { }
public int ID => _id;
}
public delegate int IntDelegate(int id);
}