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.ILGeneration.Tests", "tests\System.Reflection.Emit.ILGeneration.Tests.csproj", "{FB037640-0591-4DF4-A331-0BEFE50A200B}"
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
{FB037640-0591-4DF4-A331-0BEFE50A200B}.DebugNETCoreAppnetstandard1.3netstandard1.3|AnyCPU.ActiveCfg = netstandard1.3-Debug|Any CPU
{FB037640-0591-4DF4-A331-0BEFE50A200B}.DebugNETCoreAppnetstandard1.3netstandard1.3|AnyCPU.Build.0 = netstandard1.3-Debug|Any CPU
{FB037640-0591-4DF4-A331-0BEFE50A200B}.ReleaseNETCoreAppnetstandard1.3netstandard1.3|AnyCPU.ActiveCfg = netstandard1.3-Release|Any CPU
{FB037640-0591-4DF4-A331-0BEFE50A200B}.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,126 @@
// 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 partial class CustomAttributeBuilder
{
public CustomAttributeBuilder(System.Reflection.ConstructorInfo con, object[] constructorArgs) { }
public CustomAttributeBuilder(System.Reflection.ConstructorInfo con, object[] constructorArgs, System.Reflection.FieldInfo[] namedFields, object[] fieldValues) { }
public CustomAttributeBuilder(System.Reflection.ConstructorInfo con, object[] constructorArgs, System.Reflection.PropertyInfo[] namedProperties, object[] propertyValues) { }
public CustomAttributeBuilder(System.Reflection.ConstructorInfo con, object[] constructorArgs, System.Reflection.PropertyInfo[] namedProperties, object[] propertyValues, System.Reflection.FieldInfo[] namedFields, object[] fieldValues) { }
}
public partial class ILGenerator
{
internal ILGenerator() { }
public virtual int ILOffset { get { throw null; } }
public virtual void BeginCatchBlock(System.Type exceptionType) { }
public virtual void BeginExceptFilterBlock() { }
public virtual System.Reflection.Emit.Label BeginExceptionBlock() { throw null; }
public virtual void BeginFaultBlock() { }
public virtual void BeginFinallyBlock() { }
public virtual void BeginScope() { }
public virtual System.Reflection.Emit.LocalBuilder DeclareLocal(System.Type localType) { throw null; }
public virtual System.Reflection.Emit.LocalBuilder DeclareLocal(System.Type localType, bool pinned) { throw null; }
public virtual System.Reflection.Emit.Label DefineLabel() { throw null; }
public virtual void Emit(System.Reflection.Emit.OpCode opcode) { }
public virtual void Emit(System.Reflection.Emit.OpCode opcode, byte arg) { }
[System.Security.SecuritySafeCriticalAttribute]
public virtual void Emit(System.Reflection.Emit.OpCode opcode, double arg) { }
public virtual void Emit(System.Reflection.Emit.OpCode opcode, short arg) { }
public virtual void Emit(System.Reflection.Emit.OpCode opcode, int arg) { }
public virtual void Emit(System.Reflection.Emit.OpCode opcode, long arg) { }
[System.Security.SecuritySafeCriticalAttribute]
public virtual void Emit(System.Reflection.Emit.OpCode opcode, System.Reflection.ConstructorInfo con) { }
public virtual void Emit(System.Reflection.Emit.OpCode opcode, System.Reflection.Emit.Label label) { }
public virtual void Emit(System.Reflection.Emit.OpCode opcode, System.Reflection.Emit.Label[] labels) { }
public virtual void Emit(System.Reflection.Emit.OpCode opcode, System.Reflection.Emit.LocalBuilder local) { }
public virtual void Emit(System.Reflection.Emit.OpCode opcode, System.Reflection.Emit.SignatureHelper signature) { }
public virtual void Emit(System.Reflection.Emit.OpCode opcode, System.Reflection.FieldInfo field) { }
[System.Security.SecuritySafeCriticalAttribute]
public virtual void Emit(System.Reflection.Emit.OpCode opcode, System.Reflection.MethodInfo meth) { }
[System.CLSCompliantAttribute(false)]
public void Emit(System.Reflection.Emit.OpCode opcode, sbyte arg) { }
[System.Security.SecuritySafeCriticalAttribute]
public virtual void Emit(System.Reflection.Emit.OpCode opcode, float arg) { }
public virtual void Emit(System.Reflection.Emit.OpCode opcode, string str) { }
[System.Security.SecuritySafeCriticalAttribute]
public virtual void Emit(System.Reflection.Emit.OpCode opcode, System.Type cls) { }
[System.Security.SecuritySafeCriticalAttribute]
public virtual void EmitCall(System.Reflection.Emit.OpCode opcode, System.Reflection.MethodInfo methodInfo, System.Type[] optionalParameterTypes) { }
[System.Security.SecuritySafeCriticalAttribute]
public virtual void EmitCalli(System.Reflection.Emit.OpCode opcode, System.Reflection.CallingConventions callingConvention, System.Type returnType, System.Type[] parameterTypes, System.Type[] optionalParameterTypes) { }
public virtual void EmitWriteLine(System.Reflection.Emit.LocalBuilder localBuilder) { }
public virtual void EmitWriteLine(System.Reflection.FieldInfo fld) { }
public virtual void EmitWriteLine(string value) { }
public virtual void EndExceptionBlock() { }
public virtual void EndScope() { }
public virtual void MarkLabel(System.Reflection.Emit.Label loc) { }
public virtual void ThrowException(System.Type excType) { }
public virtual void UsingNamespace(string usingNamespace) { }
}
[System.Runtime.InteropServices.StructLayoutAttribute(System.Runtime.InteropServices.LayoutKind.Sequential)]
public partial struct Label
{
public override bool Equals(object obj) { throw null; }
public bool Equals(System.Reflection.Emit.Label obj) { throw null; }
public override int GetHashCode() { throw null; }
public static bool operator ==(System.Reflection.Emit.Label a, System.Reflection.Emit.Label b) { throw null; }
public static bool operator !=(System.Reflection.Emit.Label a, System.Reflection.Emit.Label b) { throw null; }
}
public sealed partial class LocalBuilder : System.Reflection.LocalVariableInfo
{
internal LocalBuilder() { }
public override bool IsPinned { get { throw null; } }
public override int LocalIndex { get { throw null; } }
public override System.Type LocalType { get { throw null; } }
}
public partial class ParameterBuilder
{
internal ParameterBuilder() { }
public virtual int Attributes { get { throw null; } }
public bool IsIn { get { throw null; } }
public bool IsOptional { get { throw null; } }
public bool IsOut { get { throw null; } }
public virtual string Name { get { throw null; } }
public virtual int Position { get { throw null; } }
[System.Security.SecuritySafeCriticalAttribute]
public virtual void SetConstant(object defaultValue) { }
[System.Security.SecuritySafeCriticalAttribute]
public void SetCustomAttribute(System.Reflection.ConstructorInfo con, byte[] binaryAttribute) { }
[System.Security.SecuritySafeCriticalAttribute]
public void SetCustomAttribute(System.Reflection.Emit.CustomAttributeBuilder customBuilder) { }
}
public sealed partial class SignatureHelper
{
internal SignatureHelper() { }
public void AddArgument(System.Type clsArgument) { }
[System.Security.SecuritySafeCriticalAttribute]
public void AddArgument(System.Type argument, bool pinned) { }
[System.Security.SecuritySafeCriticalAttribute]
public void AddArgument(System.Type argument, System.Type[] requiredCustomModifiers, System.Type[] optionalCustomModifiers) { }
public void AddArguments(System.Type[] arguments, System.Type[][] requiredCustomModifiers, System.Type[][] optionalCustomModifiers) { }
public void AddSentinel() { }
public override bool Equals(object obj) { throw null; }
public static System.Reflection.Emit.SignatureHelper GetFieldSigHelper(System.Reflection.Module mod) { throw null; }
public override int GetHashCode() { throw null; }
public static System.Reflection.Emit.SignatureHelper GetLocalVarSigHelper() { throw null; }
public static System.Reflection.Emit.SignatureHelper GetLocalVarSigHelper(System.Reflection.Module mod) { throw null; }
public static System.Reflection.Emit.SignatureHelper GetMethodSigHelper(System.Reflection.CallingConventions callingConvention, System.Type returnType) { throw null; }
[System.Security.SecuritySafeCriticalAttribute]
public static System.Reflection.Emit.SignatureHelper GetMethodSigHelper(System.Reflection.Module mod, System.Reflection.CallingConventions callingConvention, System.Type returnType) { throw null; }
[System.Security.SecuritySafeCriticalAttribute]
public static System.Reflection.Emit.SignatureHelper GetMethodSigHelper(System.Reflection.Module mod, System.Type returnType, System.Type[] parameterTypes) { throw null; }
[System.Security.SecuritySafeCriticalAttribute]
public static System.Reflection.Emit.SignatureHelper GetPropertySigHelper(System.Reflection.Module mod, System.Reflection.CallingConventions callingConvention, System.Type returnType, System.Type[] requiredReturnTypeCustomModifiers, System.Type[] optionalReturnTypeCustomModifiers, System.Type[] parameterTypes, System.Type[][] requiredParameterTypeCustomModifiers, System.Type[][] optionalParameterTypeCustomModifiers) { throw null; }
public static System.Reflection.Emit.SignatureHelper GetPropertySigHelper(System.Reflection.Module mod, System.Type returnType, System.Type[] parameterTypes) { throw null; }
public static System.Reflection.Emit.SignatureHelper GetPropertySigHelper(System.Reflection.Module mod, System.Type returnType, System.Type[] requiredReturnTypeCustomModifiers, System.Type[] optionalReturnTypeCustomModifiers, System.Type[] parameterTypes, System.Type[][] requiredParameterTypeCustomModifiers, System.Type[][] optionalParameterTypeCustomModifiers) { throw null; }
public byte[] GetSignature() { throw null; }
public override string ToString() { throw null; }
}
}

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 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.ILGeneration.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\System.Runtime\ref\System.Runtime.csproj" />
<ProjectReference Include="..\..\System.Reflection\ref\System.Reflection.csproj" />
<ProjectReference Include="..\..\System.Reflection.Primitives\ref\System.Reflection.Primitives.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.ILGeneration</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>

File diff suppressed because it is too large Load Diff

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 Xunit;
namespace System.Reflection.Emit.Tests
{
public class ILGeneratorDeclareLocal
{
public static Type[] TestData => new Type[]
{
typeof(int),
typeof(object),
typeof(TestClassLocal1),
typeof(TestStructLocal1),
typeof(TestDelegateLocal1),
typeof(TestEnumLocal1),
typeof(TestExceptionLocal1),
typeof(void)
};
[Fact]
public void Basic()
{
TypeBuilder type = Helpers.DynamicType(TypeAttributes.NotPublic);
MethodBuilder method = type.DefineMethod("TestMethod", MethodAttributes.Public | MethodAttributes.Static);
ILGenerator generator = method.GetILGenerator();
VerifyDeclareLocal(generator);
}
[Fact]
public void BeginScope()
{
TypeBuilder type = Helpers.DynamicType(TypeAttributes.NotPublic);
MethodBuilder method = type.DefineMethod("TestMethod", MethodAttributes.Public | MethodAttributes.Static);
ILGenerator generator = method.GetILGenerator();
generator.BeginScope();
VerifyDeclareLocal(generator);
}
[Fact]
public void BeginExceptionBlock()
{
TypeBuilder type = Helpers.DynamicType(TypeAttributes.NotPublic);
MethodBuilder method = type.DefineMethod("TestMethod", MethodAttributes.Public | MethodAttributes.Static);
ILGenerator generator = method.GetILGenerator();
generator.BeginExceptionBlock();
VerifyDeclareLocal(generator);
}
[Fact]
public void BeginCatchBlock()
{
TypeBuilder type = Helpers.DynamicType(TypeAttributes.NotPublic);
MethodBuilder method = type.DefineMethod("PosTest4_Method", MethodAttributes.Public | MethodAttributes.Static);
ILGenerator generator = method.GetILGenerator();
generator.BeginExceptionBlock();
generator.BeginCatchBlock(typeof(TestExceptionLocal1));
VerifyDeclareLocal(generator);
}
[Fact]
public void BeginFinallyBlock()
{
TypeBuilder type = Helpers.DynamicType(TypeAttributes.NotPublic);
MethodBuilder method = type.DefineMethod("PosTest5_Method", MethodAttributes.Public | MethodAttributes.Static);
ILGenerator generator = method.GetILGenerator();
generator.BeginExceptionBlock();
generator.BeginFinallyBlock();
VerifyDeclareLocal(generator);
}
[Fact]
public void AbstractPublicMethod()
{
TypeBuilder type = Helpers.DynamicType(TypeAttributes.Abstract | TypeAttributes.Public);
MethodBuilder method = type.DefineMethod("PosTest6_Method", MethodAttributes.Abstract | MethodAttributes.Public);
ILGenerator generator = method.GetILGenerator();
VerifyDeclareLocal(generator);
}
[Fact]
public void DeclareLocal_NullLocalType_ThrowsArgumentNullException()
{
ModuleBuilder module = Helpers.DynamicModule();
MethodBuilder method = module.DefineGlobalMethod("Method", MethodAttributes.Public | MethodAttributes.Static, typeof(Type), new Type[0]);
ILGenerator ilGenerator = method.GetILGenerator();
Assert.Throws<ArgumentNullException>("localType", () => ilGenerator.DeclareLocal(null));
Assert.Throws<ArgumentNullException>("localType", () => ilGenerator.DeclareLocal(null, false));
}
[Fact]
public void DeclareLocal_TypeCreated_ThrowsInvalidOperationException()
{
TypeBuilder type = Helpers.DynamicType(TypeAttributes.NotPublic);
MethodBuilder method = type.DefineMethod("Method", MethodAttributes.Public | MethodAttributes.Static, typeof(Type), new Type[0]);
ILGenerator ilGenerator = method.GetILGenerator();
ilGenerator.Emit(OpCodes.Ret);
type.CreateTypeInfo();
Assert.Throws<InvalidOperationException>(() => ilGenerator.DeclareLocal(typeof(int)));
}
[Fact]
public void DeclareLocal_GlobalFunctionsCreated_ThrowsInvalidOperationException()
{
ModuleBuilder module = Helpers.DynamicModule();
MethodBuilder method = module.DefineGlobalMethod("method1", MethodAttributes.Public | MethodAttributes.Static, typeof(Type), new Type[0]);
ILGenerator ilGenerator = method.GetILGenerator();
ilGenerator.Emit(OpCodes.Ret);
module.CreateGlobalFunctions();
Assert.Throws<InvalidOperationException>(() => ilGenerator.DeclareLocal(typeof(int)));
}
private void VerifyDeclareLocal(ILGenerator generator)
{
for (int i = 0; i < TestData.Length; i++)
{
LocalBuilder local = generator.DeclareLocal(TestData[i]);
Assert.Equal(TestData[i], local.LocalType);
Assert.Equal(i, local.LocalIndex);
Assert.False(local.IsPinned);
}
}
public class TestClassLocal1 { }
public struct TestStructLocal1 { }
public delegate void TestDelegateLocal1(TestStructLocal1 ts);
public enum TestEnumLocal1 { DEFAULT }
public class TestExceptionLocal1 : Exception { }
}
}

View File

@@ -0,0 +1,25 @@
// 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 ILGeneratorDefineLabel
{
[Fact]
public void DefineLabel_DoesNotThrow()
{
TypeBuilder type = Helpers.DynamicType(TypeAttributes.Public);
MethodBuilder method = type.DefineMethod("meth1", MethodAttributes.Public | MethodAttributes.Static, typeof(bool), new Type[0]);
ILGenerator ilGenerator = method.GetILGenerator();
// We use labels in other tests in the code so no need to verify that they were placed correctly here.
for (int i = 0; i < 17; ++i)
{
ilGenerator.DefineLabel();
}
}
}
}

View File

@@ -0,0 +1,206 @@
// 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 ILGeneratorEmit1
{
[Fact]
public void PosTest1()
{
ModuleBuilder module = Helpers.DynamicModule();
TypeBuilder type = module.DefineType("C1", TypeAttributes.Public);
MethodBuilder method1 = type.DefineMethod("meth1", MethodAttributes.Public, typeof(int), new Type[0]);
int expectedRet = 1;
// Generate code for the method that we are going to use as MethodInfo in ILGenerator.Emit()
ILGenerator ilgen = method1.GetILGenerator();
ilgen.Emit(OpCodes.Ldc_I4, expectedRet);
ilgen.Emit(OpCodes.Ret);
// Create the type where this method is in
Type createdType1 = type.CreateTypeInfo().AsType();
MethodInfo createdMethod1 = createdType1.GetMethod("meth1");
TypeBuilder type2 = module.DefineType("C2", TypeAttributes.Public);
MethodBuilder method2 = type2.DefineMethod("meth2", MethodAttributes.Public | MethodAttributes.Static, typeof(int), new Type[0]);
// Generate code for the method which will be invoking the first method
ILGenerator ilgen2 = method2.GetILGenerator();
ilgen2.Emit(OpCodes.Newobj, createdType1.GetConstructor(new Type[0]));
ilgen2.Emit(OpCodes.Call, createdMethod1);
ilgen2.Emit(OpCodes.Ret);
// Create the type whose method will be invoking the MethodInfo method
Type createdType2 = type2.CreateTypeInfo().AsType();
MethodInfo createdMethod2 = createdType2.GetMethod("meth2");
// meth2 should invoke meth1 which should return value from meth1
Assert.Equal(expectedRet, createdMethod2.Invoke(null, null));
}
[Fact]
public void PosTest2()
{
ModuleBuilder module = Helpers.DynamicModule();
TypeBuilder type1 = module.DefineType("C1", TypeAttributes.Public);
MethodBuilder method1 = type1.DefineMethod("meth1", MethodAttributes.Public, typeof(int), new Type[0]);
int expectedRet = 12;
// Generate code for the method that we are going to use as MethodInfo in ILGenerator.Emit()
ILGenerator ilGenerator1 = method1.GetILGenerator();
ilGenerator1.Emit(OpCodes.Ldc_I4, expectedRet);
ilGenerator1.Emit(OpCodes.Ret);
// Create the type where this method is in
Type createdType1 = type1.CreateTypeInfo().AsType();
MethodInfo createdMethod1 = createdType1.GetMethod("meth1");
TypeBuilder type2 = module.DefineType("C2", TypeAttributes.Public);
MethodBuilder method2 = type2.DefineMethod("meth2", MethodAttributes.Public, typeof(int), new Type[0]);
// Generate code for the method which will be invoking the first method
ILGenerator ilGenerator2 = method2.GetILGenerator();
ilGenerator2.Emit(OpCodes.Newobj, createdType1.GetConstructor(new Type[0]));
ilGenerator2.Emit(OpCodes.Callvirt, createdMethod1);
ilGenerator2.Emit(OpCodes.Ret);
// Create the type whose method will be invoking the MethodInfo method
Type createdType2 = type2.CreateTypeInfo().AsType();
MethodInfo createdMethod2 = createdType2.GetMethod("meth2");
// meth2 should invoke meth1 which should return value 'methodRet'
Assert.Equal(expectedRet, createdMethod2.Invoke(Activator.CreateInstance(createdType2), null));
}
[Fact]
public void PosTest3()
{
ModuleBuilder modbuild = Helpers.DynamicModule();
TypeBuilder type1 = modbuild.DefineType("C1", TypeAttributes.Public);
MethodBuilder method1 = type1.DefineMethod("meth1", MethodAttributes.Public, typeof(int), new Type[0]);
method1.DefineGenericParameters("T");
int expectedRet = 101;
// Generate code for the method that we are going to use as MethodInfo in ILGenerator.Emit()
ILGenerator ilGenerator = method1.GetILGenerator();
ilGenerator.Emit(OpCodes.Ldc_I4, expectedRet);
ilGenerator.Emit(OpCodes.Ret);
// Create the type where this method is in
Type createdType1 = type1.CreateTypeInfo().AsType();
MethodInfo createdMethod1 = createdType1.GetMethod("meth1");
MethodInfo genericMethod = createdMethod1.MakeGenericMethod(typeof(int));
TypeBuilder type2 = modbuild.DefineType("C2", TypeAttributes.Public);
MethodBuilder method2 = type2.DefineMethod("meth2", MethodAttributes.Public | MethodAttributes.Static, typeof(int), new Type[0]);
// Generate code for the method which will be invoking the first method
ILGenerator ilGenerator2 = method2.GetILGenerator();
ilGenerator2.Emit(OpCodes.Newobj, createdType1.GetConstructor(new Type[0]));
ilGenerator2.Emit(OpCodes.Callvirt, genericMethod);
ilGenerator2.Emit(OpCodes.Ret);
// Create the type whose method will be invoking the MethodInfo method
Type createdType2 = type2.CreateTypeInfo().AsType();
MethodInfo createdMethod2 = createdType2.GetMethod("meth2");
// meth2 should invoke meth1 which should return value 'methodRet'
Assert.Equal(expectedRet, createdMethod2.Invoke(null, null));
}
[Fact]
public void PosTest4()
{
ModuleBuilder module = Helpers.DynamicModule();
TypeBuilder type1 = module.DefineType("C1", TypeAttributes.Public);
type1.DefineGenericParameters(new string[] { "T" });
MethodBuilder method1 = type1.DefineMethod("meth1", MethodAttributes.Public, typeof(long), new Type[0]);
long expectedRet = 500000;
// Generate code for the method that we are going to use as MethodInfo in ILGenerator.Emit()
ILGenerator ilGenerator1 = method1.GetILGenerator();
ilGenerator1.Emit(OpCodes.Ldc_I8, expectedRet);
ilGenerator1.Emit(OpCodes.Ret);
// Create the type where this method is in
Type createdType1 = type1.CreateTypeInfo().AsType();
Type genericType = createdType1.MakeGenericType(typeof(int));
MethodInfo genericMethod = genericType.GetMethod("meth1");
TypeBuilder type2 = module.DefineType("C2", TypeAttributes.Public);
MethodBuilder method2 = type2.DefineMethod("meth2", MethodAttributes.Public | MethodAttributes.Static, typeof(long), new Type[0]);
// Generate code for the method which will be invoking the first method
ILGenerator ilGenerator2 = method2.GetILGenerator();
ilGenerator2.Emit(OpCodes.Newobj, genericType.GetConstructor(new Type[0]));
ilGenerator2.Emit(OpCodes.Callvirt, genericMethod);
ilGenerator2.Emit(OpCodes.Ret);
// Create the type whose method will be invoking the MethodInfo method
Type createdType2 = type2.CreateTypeInfo().AsType();
MethodInfo createdMethod2 = createdType2.GetMethod("meth2");
// meth2 should invoke meth1 which should return value 'methodRet'
Assert.Equal(expectedRet, createdMethod2.Invoke(null, null));
}
[Fact]
public void PosTest5()
{
ModuleBuilder module = Helpers.DynamicModule();
TypeBuilder type1 = module.DefineType("C1", TypeAttributes.Public);
type1.DefineGenericParameters("T");
MethodBuilder method1 = type1.DefineMethod("meth1", MethodAttributes.Public, typeof(int), new Type[0]);
method1.DefineGenericParameters("U");
int expectedRet = 1;
// Generate code for the method that we are going to use as MethodInfo in ILGenerator.Emit()
ILGenerator ilGenerator1 = method1.GetILGenerator();
ilGenerator1.Emit(OpCodes.Ldc_I4, expectedRet);
ilGenerator1.Emit(OpCodes.Ret);
// create the type where this method is in
Type createdType1 = type1.CreateTypeInfo().AsType();
Type genericType = createdType1.MakeGenericType(typeof(int));
MethodInfo createdMethod1 = genericType.GetMethod("meth1");
MethodInfo genericMethod = createdMethod1.MakeGenericMethod(typeof(string));
TypeBuilder type2 = module.DefineType("C2", TypeAttributes.Public);
MethodBuilder method2 = type2.DefineMethod("meth2", MethodAttributes.Public | MethodAttributes.Static, typeof(int), new Type[0]);
// Generate code for the method which will be invoking the first method
ILGenerator ilGenerator2 = method2.GetILGenerator();
ilGenerator2.Emit(OpCodes.Newobj, genericType.GetConstructor(new Type[0]));
ilGenerator2.Emit(OpCodes.Callvirt, genericMethod);
ilGenerator2.Emit(OpCodes.Ret);
// Create the type whose method will be invoking the MethodInfo method
Type createdType2 = type2.CreateTypeInfo().AsType();
MethodInfo createdMethod2 = createdType2.GetMethod("meth2");
// meth2 should invoke meth1 which should return value 'methodRet'
Assert.Equal(expectedRet, createdMethod2.Invoke(null, null));
}
[Fact]
public void Emit_OpCodes_MethodInfo_NullMethod_ThrowsArgumentNullException()
{
ModuleBuilder module = Helpers.DynamicModule();
MethodBuilder method = module.DefineGlobalMethod("Method", MethodAttributes.Public | MethodAttributes.Static, typeof(Type), new Type[0]);
ILGenerator ilGenerator = method.GetILGenerator();
Assert.Throws<ArgumentNullException>("meth", () => ilGenerator.Emit(OpCodes.Call, (MethodInfo)null));
}
}
}

View File

@@ -0,0 +1,176 @@
// 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 ILGeneratorEmit2
{
[Fact]
public void PosTest1()
{
TypeBuilder type = Helpers.DynamicType(TypeAttributes.NotPublic);
MethodBuilder method = type.DefineMethod("Method", MethodAttributes.Public | MethodAttributes.Static);
ILGenerator generator = method.GetILGenerator();
LocalBuilder arg = generator.DeclareLocal(typeof(int));
generator.Emit(OpCodes.Ldarg_0, arg);
generator.Emit(OpCodes.Nop, arg);
// Try emit opcode which takes multiple args
generator.Emit(OpCodes.Add, arg);
}
[Fact]
public void PosTest2()
{
TypeBuilder type = Helpers.DynamicType(TypeAttributes.NotPublic);
MethodBuilder method = type.DefineMethod("Method", MethodAttributes.Public | MethodAttributes.Static);
ILGenerator generator = method.GetILGenerator();
LocalBuilder arg = generator.DeclareLocal(typeof(ILGeneratorEmit2));
generator.Emit(OpCodes.Ldnull, arg);
generator.BeginScope();
generator.Emit(OpCodes.Ldarg_0, arg);
generator.Emit(OpCodes.Nop, arg);
// Try emit opcode which takes multiple args
generator.Emit(OpCodes.Add, arg);
}
[Fact]
public void PosTest3()
{
TypeBuilder type = Helpers.DynamicType(TypeAttributes.NotPublic);
MethodBuilder method = type.DefineMethod("Method", MethodAttributes.Public | MethodAttributes.Static);
ILGenerator generator = method.GetILGenerator();
LocalBuilder arg = generator.DeclareLocal(typeof(object));
generator.BeginExceptionBlock();
generator.Emit(OpCodes.Ldnull, arg);
generator.Emit(OpCodes.Ldarg_0, arg);
generator.Emit(OpCodes.Nop, arg);
// Try emit opcode which takes multiple args
generator.Emit(OpCodes.Add, arg);
}
[Fact]
public void PosTest4()
{
TypeBuilder type = Helpers.DynamicType(TypeAttributes.NotPublic);
MethodBuilder method = type.DefineMethod("Method", MethodAttributes.Public | MethodAttributes.Static);
ILGenerator generator = method.GetILGenerator();
LocalBuilder arg = generator.DeclareLocal(typeof(int));
generator.BeginExceptionBlock();
generator.Emit(OpCodes.Throw, arg);
generator.BeginCatchBlock(typeof(Exception));
generator.Emit(OpCodes.Ldnull, arg);
generator.Emit(OpCodes.Ldarg_0, arg);
generator.Emit(OpCodes.Nop, arg);
// Try emit opcode which takes multiple args
generator.Emit(OpCodes.Add, arg);
}
[Fact]
public void PosTest5()
{
TypeBuilder type = Helpers.DynamicType(TypeAttributes.NotPublic);
MethodBuilder method = type.DefineMethod("Method", MethodAttributes.Public | MethodAttributes.Static);
ILGenerator generator = method.GetILGenerator();
LocalBuilder arg = generator.DeclareLocal(typeof(int));
generator.BeginExceptionBlock();
generator.Emit(OpCodes.Throw, arg);
generator.BeginFinallyBlock();
generator.Emit(OpCodes.Ldnull, arg);
generator.Emit(OpCodes.Ldarg_0, arg);
generator.Emit(OpCodes.Nop, arg);
// Try emit opcode which takes multiple args
generator.Emit(OpCodes.Add, arg);
}
[Fact]
public void PosTest6()
{
TypeBuilder type = Helpers.DynamicType(TypeAttributes.NotPublic);
MethodBuilder method = type.DefineMethod("Method", MethodAttributes.Public | MethodAttributes.Static);
ILGenerator generator = method.GetILGenerator();
LocalBuilder arg = generator.DeclareLocal(typeof(int));
generator.BeginExceptionBlock();
generator.Emit(OpCodes.Throw, arg);
generator.BeginFaultBlock();
generator.Emit(OpCodes.Ldnull, arg);
generator.Emit(OpCodes.Ldarg_0, arg);
generator.Emit(OpCodes.Nop, arg);
// Try emit opcode which takes multiple args
generator.Emit(OpCodes.Add, arg);
}
[Fact]
public void PosTest7()
{
TypeBuilder type = Helpers.DynamicType(TypeAttributes.NotPublic);
MethodBuilder method = type.DefineMethod("Method", MethodAttributes.Public | MethodAttributes.Static);
ILGenerator generator = method.GetILGenerator();
LocalBuilder arg = generator.DeclareLocal(typeof(int));
generator.Emit(OpCodes.Ldarg_0, arg);
generator.Emit(OpCodes.Nop, arg);
// Try emit opcode which takes multiple args
generator.Emit(OpCodes.Add, arg);
}
[Fact]
public void Emit_OpCodes_LocalBuilder_NullLocal_ThrowsArgumentNullException()
{
TypeBuilder type = Helpers.DynamicType(TypeAttributes.NotPublic);
MethodBuilder method = type.DefineMethod("Method", MethodAttributes.Public | MethodAttributes.Static);
ILGenerator generator = method.GetILGenerator();
Assert.Throws<ArgumentNullException>("local", () => generator.Emit(OpCodes.Ldarg_0, (LocalBuilder)null));
}
[Fact]
public void Emit_OpCodes_LocalBuilder_LocalFromDifferentMethod_ThrowsArgumentException()
{
TypeBuilder type = Helpers.DynamicType(TypeAttributes.NotPublic);
MethodBuilder method1 = type.DefineMethod("Method1", MethodAttributes.Public | MethodAttributes.Static);
MethodBuilder method2 = type.DefineMethod("Method2", MethodAttributes.Public | MethodAttributes.Static);
ILGenerator generator = method1.GetILGenerator();
LocalBuilder local = method2.GetILGenerator().DeclareLocal(typeof(int));
Assert.Throws<ArgumentException>("local", () => generator.Emit(OpCodes.Ldarg_0, local));
}
[Fact]
public void Emit_OpCodes_LocalBuilder_TooManyLocals_ThrowsInvalidOperationException()
{
TypeBuilder type = Helpers.DynamicType(TypeAttributes.NotPublic);
MethodBuilder method = type.DefineMethod("NegTest3_Method", MethodAttributes.Public | MethodAttributes.Static);
ILGenerator generator = method.GetILGenerator();
for (int i = 0; i <= byte.MaxValue; ++i)
{
generator.DeclareLocal(typeof(int));
}
LocalBuilder arg = generator.DeclareLocal(typeof(int));
Assert.Throws<InvalidOperationException>(() => generator.Emit(OpCodes.Br_S, arg));
}
}
}

View File

@@ -0,0 +1,252 @@
// 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 struct EmitStruct3 { }
public class ILGeneratorEmit3
{
[Fact]
public void PosTest1()
{
TypeBuilder type = Helpers.DynamicType(TypeAttributes.Public);
MethodBuilder method = type.DefineMethod("meth1", MethodAttributes.Public | MethodAttributes.Static, typeof(bool), new Type[0]);
ILGenerator ilGenerator = method.GetILGenerator();
LocalBuilder lb0 = ilGenerator.DeclareLocal(typeof(int));
LocalBuilder lb1 = ilGenerator.DeclareLocal(typeof(byte));
LocalBuilder lb2 = ilGenerator.DeclareLocal(typeof(double));
LocalBuilder lb3 = ilGenerator.DeclareLocal(typeof(bool));
LocalBuilder lb4 = ilGenerator.DeclareLocal(typeof(bool));
Label label1 = ilGenerator.DefineLabel();
Label label2 = ilGenerator.DefineLabel();
Label label3 = ilGenerator.DefineLabel();
Label label4 = ilGenerator.DefineLabel();
// emit the locals and check that we get correct values stored
ilGenerator.Emit(OpCodes.Ldc_I4, 5);
ilGenerator.Emit(OpCodes.Stloc, lb0);
ilGenerator.Emit(OpCodes.Ldloc, lb0);
ilGenerator.Emit(OpCodes.Ldc_I4, 5);
ilGenerator.Emit(OpCodes.Ceq);
ilGenerator.Emit(OpCodes.Stloc, lb4);
ilGenerator.Emit(OpCodes.Ldloc, lb4);
ilGenerator.Emit(OpCodes.Brtrue_S, label1);
ilGenerator.Emit(OpCodes.Ldc_I4, 0);
ilGenerator.Emit(OpCodes.Stloc, lb3);
ilGenerator.Emit(OpCodes.Br_S, label4);
ilGenerator.MarkLabel(label1);
ilGenerator.Emit(OpCodes.Ldc_I4, 1);
ilGenerator.Emit(OpCodes.Stloc, lb1);
ilGenerator.Emit(OpCodes.Ldloc, lb1);
ilGenerator.Emit(OpCodes.Ldc_I4, 1);
ilGenerator.Emit(OpCodes.Ceq);
ilGenerator.Emit(OpCodes.Stloc, lb4);
ilGenerator.Emit(OpCodes.Ldloc, lb4);
ilGenerator.Emit(OpCodes.Brtrue_S, label2);
ilGenerator.Emit(OpCodes.Ldc_I4, 0);
ilGenerator.Emit(OpCodes.Stloc, lb3);
ilGenerator.Emit(OpCodes.Br_S, label4);
ilGenerator.MarkLabel(label2);
ilGenerator.Emit(OpCodes.Ldc_R8, 2.5);
ilGenerator.Emit(OpCodes.Stloc, lb2);
ilGenerator.Emit(OpCodes.Ldloc, lb2);
ilGenerator.Emit(OpCodes.Ldc_R8, 2.5);
ilGenerator.Emit(OpCodes.Ceq);
ilGenerator.Emit(OpCodes.Stloc, lb4);
ilGenerator.Emit(OpCodes.Ldloc, lb4);
ilGenerator.Emit(OpCodes.Brtrue_S, label3);
ilGenerator.Emit(OpCodes.Ldc_I4, 0);
ilGenerator.Emit(OpCodes.Stloc, lb3);
ilGenerator.Emit(OpCodes.Br_S, label4);
// Should return true if all checks were correct
ilGenerator.MarkLabel(label3);
ilGenerator.Emit(OpCodes.Ldc_I4, 1);
ilGenerator.Emit(OpCodes.Stloc, lb3);
ilGenerator.Emit(OpCodes.Br_S, label4);
ilGenerator.MarkLabel(label4);
ilGenerator.Emit(OpCodes.Ldloc, lb3);
ilGenerator.Emit(OpCodes.Ret);
// Create the type where this method is in
Type createdType = type.CreateTypeInfo().AsType();
MethodInfo createdMethod = createdType.GetMethod("meth1");
Assert.True((bool)createdMethod.Invoke(null, null));
}
[Fact]
public void PosTest2()
{
TypeBuilder tb = Helpers.DynamicType(TypeAttributes.Public);
MethodBuilder method = tb.DefineMethod("meth1", MethodAttributes.Public | MethodAttributes.Static, typeof(bool), new Type[0]);
ILGenerator ilGenerator = method.GetILGenerator();
LocalBuilder lb0 = ilGenerator.DeclareLocal(typeof(EmitStruct3));
// Emit the locals
ilGenerator.Emit(OpCodes.Ldloca, lb0);
ilGenerator.Emit(OpCodes.Initobj, typeof(EmitStruct3));
ilGenerator.Emit(OpCodes.Ldc_I4, 1);
ilGenerator.Emit(OpCodes.Ret);
// Create the type where this method is in
Type createdType = tb.CreateTypeInfo().AsType();
MethodInfo createdMethod = createdType.GetMethod("meth1");
Assert.True((bool)createdMethod.Invoke(null, null));
}
[Fact]
public void PosTest3()
{
TypeBuilder type = Helpers.DynamicType(TypeAttributes.Public);
MethodBuilder method = type.DefineMethod("meth1", MethodAttributes.Public | MethodAttributes.Static, typeof(bool), new Type[0]);
ILGenerator ilGenerator = method.GetILGenerator();
// Emit locals
LocalBuilder local = ilGenerator.DeclareLocal(typeof(int));
ilGenerator.Emit(OpCodes.Nop, local);
ilGenerator.Emit(OpCodes.Ldc_I4, 1);
ilGenerator.Emit(OpCodes.Ret);
// create the type where this method is in
Type createdType = type.CreateTypeInfo().AsType();
MethodInfo createdMethod = createdType.GetMethod("meth1");
Assert.True((bool)createdMethod.Invoke(null, null));
}
[Fact]
public void PosTest4()
{
TypeBuilder type = Helpers.DynamicType(TypeAttributes.Public);
MethodBuilder method = type.DefineMethod("meth1", MethodAttributes.Public | MethodAttributes.Static, typeof(bool), new Type[0]);
ILGenerator ilGenerator = method.GetILGenerator();
LocalBuilder lb0 = ilGenerator.DeclareLocal(typeof(int));
LocalBuilder lb1 = ilGenerator.DeclareLocal(typeof(byte));
LocalBuilder lb2 = ilGenerator.DeclareLocal(typeof(double));
LocalBuilder lb3 = ilGenerator.DeclareLocal(typeof(bool));
LocalBuilder lb4 = ilGenerator.DeclareLocal(typeof(bool));
Label label1 = ilGenerator.DefineLabel();
Label label2 = ilGenerator.DefineLabel();
Label label3 = ilGenerator.DefineLabel();
Label label4 = ilGenerator.DefineLabel();
// Emit the locals using Stloc_0, Stloc_1, Stloc_2, Stloc_3, Stloc_S,
// Ldloc_0, Ldloc_1, Ldloc_2, Ldloc_3, Ldloc_S,
ilGenerator.Emit(OpCodes.Ldc_I4, 5);
ilGenerator.Emit(OpCodes.Stloc_0, lb0);
ilGenerator.Emit(OpCodes.Ldloc_0, lb0);
ilGenerator.Emit(OpCodes.Ldc_I4, 5);
ilGenerator.Emit(OpCodes.Ceq);
ilGenerator.Emit(OpCodes.Stloc_S, lb4);
ilGenerator.Emit(OpCodes.Ldloc_S, lb4);
ilGenerator.Emit(OpCodes.Brtrue_S, label1);
ilGenerator.Emit(OpCodes.Ldc_I4, 0);
ilGenerator.Emit(OpCodes.Stloc_3, lb3);
ilGenerator.Emit(OpCodes.Br_S, label4);
ilGenerator.MarkLabel(label1);
ilGenerator.Emit(OpCodes.Ldc_I4, 1);
ilGenerator.Emit(OpCodes.Stloc_1, lb1);
ilGenerator.Emit(OpCodes.Ldloc_1, lb1);
ilGenerator.Emit(OpCodes.Ldc_I4, 1);
ilGenerator.Emit(OpCodes.Ceq);
ilGenerator.Emit(OpCodes.Stloc_S, lb4);
ilGenerator.Emit(OpCodes.Ldloc_S, lb4);
ilGenerator.Emit(OpCodes.Brtrue_S, label2);
ilGenerator.Emit(OpCodes.Ldc_I4, 0);
ilGenerator.Emit(OpCodes.Stloc_3, lb3);
ilGenerator.Emit(OpCodes.Br_S, label4);
ilGenerator.MarkLabel(label2);
ilGenerator.Emit(OpCodes.Ldc_R8, 2.5);
ilGenerator.Emit(OpCodes.Stloc_2, lb2);
ilGenerator.Emit(OpCodes.Ldloc_2, lb2);
ilGenerator.Emit(OpCodes.Ldc_R8, 2.5);
ilGenerator.Emit(OpCodes.Ceq);
ilGenerator.Emit(OpCodes.Stloc_S, lb4);
ilGenerator.Emit(OpCodes.Ldloc_S, lb4);
ilGenerator.Emit(OpCodes.Brtrue_S, label3);
ilGenerator.Emit(OpCodes.Ldc_I4, 0);
ilGenerator.Emit(OpCodes.Stloc_3, lb3);
ilGenerator.Emit(OpCodes.Br_S, label4);
ilGenerator.MarkLabel(label3);
ilGenerator.Emit(OpCodes.Ldc_I4, 1);
ilGenerator.Emit(OpCodes.Stloc_3, lb3);
ilGenerator.Emit(OpCodes.Br_S, label4);
ilGenerator.MarkLabel(label4);
ilGenerator.Emit(OpCodes.Ldloc_3, lb3);
ilGenerator.Emit(OpCodes.Ret);
// Create the type where this method is in
Type createdType = type.CreateTypeInfo().AsType();
MethodInfo createdMethod = createdType.GetMethod("meth1");
Assert.True((bool)createdMethod.Invoke(null, null));
}
[Fact]
public void Emit_OpCodes_LocalBuilder_NullLocal_ThrowsArgumentNullException()
{
TypeBuilder type = Helpers.DynamicType(TypeAttributes.NotPublic);
MethodBuilder method = type.DefineMethod("Method", MethodAttributes.Public | MethodAttributes.Static);
ILGenerator generator = method.GetILGenerator();
Assert.Throws<ArgumentNullException>("local", () => generator.Emit(OpCodes.Ldloc_0, (LocalBuilder)null));
}
[Fact]
public void Emit_OpCodes_LocalBuilder_LocalFromDifferentMethod_ThrowsArgumentException()
{
TypeBuilder type = Helpers.DynamicType(TypeAttributes.NotPublic);
MethodBuilder method1 = type.DefineMethod("Method1", MethodAttributes.Public | MethodAttributes.Static);
MethodBuilder method2 = type.DefineMethod("Method2", MethodAttributes.Public | MethodAttributes.Static);
ILGenerator generator = method1.GetILGenerator();
LocalBuilder local = method2.GetILGenerator().DeclareLocal(typeof(string));
Assert.Throws<ArgumentException>("local", () => generator.Emit(OpCodes.Ldloc_0, local));
}
[Fact]
public void Emit_OpCodes_LocalBuilder_TooManyLocals_ThrowsInvalidOperationException()
{
TypeBuilder type = Helpers.DynamicType(TypeAttributes.NotPublic);
MethodBuilder method = type.DefineMethod("NegTest3_Method", MethodAttributes.Public | MethodAttributes.Static);
ILGenerator generator = method.GetILGenerator();
for (int i = 0; i <= byte.MaxValue; ++i)
{
generator.DeclareLocal(typeof(string));
}
LocalBuilder arg = generator.DeclareLocal(typeof(string));
Assert.Throws<InvalidOperationException>(() => generator.Emit(OpCodes.Ldloc_S, arg));
}
}
}

View File

@@ -0,0 +1,50 @@
// 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 ILGeneratorEmitWriteLine
{
[Fact]
public void EmitWriteLineTests()
{
ModuleBuilder module = Helpers.DynamicModule();
TypeBuilder type1 = module.DefineType("C1", TypeAttributes.Public);
MethodBuilder method1 = type1.DefineMethod("meth1", MethodAttributes.Public, typeof(int), new Type[0]);
FieldBuilder field = type1.DefineField("field1", typeof(int), FieldAttributes.Public | FieldAttributes.Static);
int expectedRet = 1;
// Generate code for the method that we are going to use as MethodInfo in ILGenerator.Emit()
ILGenerator ilGenerator1 = method1.GetILGenerator();
ilGenerator1.Emit(OpCodes.Ldc_I4, expectedRet);
ilGenerator1.Emit(OpCodes.Ret);
// Create the type where this method is in
Type createdType1 = type1.CreateTypeInfo().AsType();
FieldInfo createdField = createdType1.GetField("field1");
TypeBuilder type2 = module.DefineType("C2", TypeAttributes.Public);
MethodBuilder method2 = type2.DefineMethod("meth2", MethodAttributes.Public | MethodAttributes.Static, typeof(int), new Type[0]);
// Generate code for the method which will be invoking the first method
ILGenerator ilGenerator2 = method2.GetILGenerator();
LocalBuilder local = ilGenerator2.DeclareLocal(typeof(bool));
ilGenerator2.EmitWriteLine(createdField);
ilGenerator2.EmitWriteLine("emitWriteLine");
ilGenerator2.EmitWriteLine(local);
ilGenerator2.Emit(OpCodes.Ldc_I4_1);
ilGenerator2.Emit(OpCodes.Ret);
// Create the type whose method will be invoking the MethodInfo method
Type createdType2 = type2.CreateTypeInfo().AsType();
MethodInfo createdMethod = createdType2.GetMethod("meth2");
// meth2 should invoke meth1 which should return value from meth1
Assert.Equal(expectedRet, createdMethod.Invoke(null, null));
}
}
}

View File

@@ -0,0 +1,56 @@
// 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 ExceptionEmitTests
{
[Fact]
public void TestExceptionEmitCalls()
{
TypeBuilder type = Helpers.DynamicType(TypeAttributes.NotPublic);
MethodBuilder methodBuilder = type.DefineMethod("DoThrow", MethodAttributes.Public | MethodAttributes.Static, typeof(int), new Type[] { typeof(bool) });
ILGenerator ilGenerator = methodBuilder.GetILGenerator();
Type overflow = typeof(OverflowException);
LocalBuilder tmp1 = ilGenerator.DeclareLocal(typeof(int));
Label dontThrow = ilGenerator.DefineLabel();
// Begin the try block.
Label exBlock = ilGenerator.BeginExceptionBlock();
ilGenerator.Emit(OpCodes.Ldarg_0);
ilGenerator.Emit(OpCodes.Brfalse_S, dontThrow);
// Throw the exception now on the stack.
ilGenerator.ThrowException(overflow);
ilGenerator.MarkLabel(dontThrow);
// Start the catch block for OverflowException.
ilGenerator.BeginCatchBlock(overflow);
// Since our function has to return an integer value, we'll load -1 onto
// the stack to indicate an error, and store it in local variable tmp1.
ilGenerator.Emit(OpCodes.Ldc_I4_M1);
ilGenerator.Emit(OpCodes.Stloc_S, tmp1);
// End the exception handling block.
ilGenerator.EndExceptionBlock();
// Return
ilGenerator.Emit(OpCodes.Ldloc_S, tmp1);
ilGenerator.Emit(OpCodes.Ret);
Type createdType = type.CreateTypeInfo().AsType();
MethodInfo createdMethod = createdType.GetMethod("DoThrow");
Assert.Equal(-1, createdMethod.Invoke(null, new object[] { true })); // Throws
Assert.Equal(0, createdMethod.Invoke(null, new object[] { false })); // Doesn't throw
}
}
}

View File

@@ -0,0 +1,34 @@
// 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 ILOffsetTests
{
[Fact]
public void ILOffset_NothingEmitted_ReturnsZero()
{
ModuleBuilder module = Helpers.DynamicModule();
MethodBuilder method = module.DefineGlobalMethod("method1", MethodAttributes.Public | MethodAttributes.Static, typeof(Type), new Type[0]);
ILGenerator ilGenerator = method.GetILGenerator();
// Method has not been emitted.
Assert.Equal(0, ilGenerator.ILOffset);
}
[Fact]
public void ILOffset_SomethingEmitted_ReturnsNonZero()
{
ModuleBuilder module = Helpers.DynamicModule();
MethodBuilder method = module.DefineGlobalMethod("method1", MethodAttributes.Public | MethodAttributes.Static, typeof(Type), new Type[0]);
ILGenerator ilGenerator = method.GetILGenerator();
ilGenerator.Emit(OpCodes.Ret);
Assert.Equal(1, ilGenerator.ILOffset);
}
}
}

View File

@@ -0,0 +1,53 @@
// 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 LabelEquals
{
[Fact]
public void Equals_SameInstance_ReturnsTrue()
{
Label label = new Label();
Assert.True(label.Equals(label));
Assert.True(label.Equals((object)label));
}
[Fact]
public void Equals_DifferentLabel_ReturnsFalse()
{
TypeBuilder type = Helpers.DynamicType(TypeAttributes.NotPublic);
MethodBuilder method = type.DefineMethod("Method", MethodAttributes.Public);
ILGenerator ilGenerator = method.GetILGenerator();
Label label1 = ilGenerator.DefineLabel();
Label label2 = ilGenerator.DefineLabel();
Assert.False(label1.Equals(label2));
Assert.False(label1.Equals((object)label2));
}
[Theory]
[InlineData(1)]
[InlineData("label")]
[InlineData(" ")]
[InlineData(null)]
public void Equals_ObjectNotLabel_ReturnsFalse(object obj)
{
Label label = new Label();
Assert.False(label.Equals(obj));
}
[Fact]
public void Equals_EqualityOperators()
{
Label lb1 = new Label();
Label lb2 = new Label();
Assert.True(lb1 == lb2);
Assert.False(lb1 != lb2);
}
}
}

View File

@@ -0,0 +1,34 @@
// 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 LabelGetHashCode
{
[Fact]
public void GetHashCode_NewInstance_ReturnsZero()
{
Label label1 = new Label();
Label label2 = new Label();
Assert.Equal(0, label1.GetHashCode());
Assert.Equal(label2.GetHashCode(), label1.GetHashCode());
}
[Fact]
public void GetHashCode_CreatedByILGenerator_ReturnsIndex()
{
TypeBuilder type = Helpers.DynamicType(TypeAttributes.NotPublic);
MethodBuilder method = type.DefineMethod("Method", MethodAttributes.Public);
ILGenerator ilGenerator = method.GetILGenerator();
for (int i = 0; i < 1000; i++)
{
Label label = ilGenerator.DefineLabel();
Assert.Equal(i, label.GetHashCode());
}
}
}
}

View File

@@ -0,0 +1,24 @@
// 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 LocalBuilderIsPinned
{
[Theory]
[InlineData(true)]
[InlineData(false)]
public void IsPinned(bool pinned)
{
TypeBuilder type = Helpers.DynamicType(TypeAttributes.NotPublic);
MethodBuilder method = type.DefineMethod("Method", MethodAttributes.Public);
ILGenerator ilGenerator = method.GetILGenerator();
ilGenerator.Emit(OpCodes.Ret);
LocalBuilder localBuilder = ilGenerator.DeclareLocal(typeof(string), pinned);
Assert.Equal(pinned, localBuilder.IsPinned);
}
}
}

Some files were not shown because too many files have changed in this diff Show More