Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BUGFIX: Import nested types by Reflection should retain declaring type and proper assembly fullname #365

Merged
merged 2 commits into from
Oct 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public DynamicCilOperandResolver(SerializedModuleDefinition contextModule, CilMe
{
_tokens = tokens ?? throw new ArgumentNullException(nameof(tokens));
_readerContext = contextModule.ReaderContext;
_importer = new ReferenceImporter(contextModule);
_importer = contextModule.DefaultImporter;
}

/// <inheritdoc />
Expand Down
10 changes: 5 additions & 5 deletions src/AsmResolver.DotNet.Dynamic/DynamicMethodDefinition.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,22 +36,22 @@ public class DynamicMethodDefinition : MethodDefinition
Module = module;
Name = methodBase.Name;
Attributes = (MethodAttributes)methodBase.Attributes;
Signature = new ReferenceImporter(module).ImportMethodSignature(ResolveSig(methodBase, module));
Signature = module.DefaultImporter.ImportMethodSignature(ResolveSig(methodBase, module));
CilMethodBody = CreateDynamicMethodBody(this, dynamicMethodObj);
}

private MethodSignature ResolveSig(MethodBase methodBase, ModuleDefinition module)
{
var imp = new ReferenceImporter(module);
var importer = module.DefaultImporter;
var returnType = methodBase is MethodInfo info
? imp.ImportTypeSignature(info.ReturnType)
? importer.ImportTypeSignature(info.ReturnType)
: module.CorLibTypeFactory.Void;

var parameters = methodBase.GetParameters();

var parameterTypes = new TypeSignature[parameters.Length];
for (int i = 0; i < parameterTypes.Length; i++)
parameterTypes[i] = imp.ImportTypeSignature(parameters[i].ParameterType);
parameterTypes[i] = importer.ImportTypeSignature(parameters[i].ParameterType);

return new MethodSignature(
methodBase.IsStatic ? 0 : CallingConventionAttributes.HasThis,
Expand Down Expand Up @@ -108,7 +108,7 @@ private static CilMethodBody CreateDynamicMethodBody(MethodDefinition method, ob
result.Instructions.AddRange(disassembler.ReadInstructions());

//Exception Handlers
DynamicMethodHelper.ReadReflectionExceptionHandlers(result, ehInfos, ehHeader, new ReferenceImporter(module));
DynamicMethodHelper.ReadReflectionExceptionHandlers(result, ehInfos, ehHeader, module.DefaultImporter);

return result;
}
Expand Down
6 changes: 6 additions & 0 deletions src/AsmResolver.DotNet/AssemblyReference.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,14 @@ public AssemblyReference(AssemblyDescriptor descriptor)
Version = descriptor.Version;
Attributes = descriptor.Attributes;
HasPublicKey = false;

PublicKeyOrToken = descriptor.GetPublicKeyToken();
if (PublicKeyOrToken?.Length == 0)
PublicKeyOrToken = null;

Culture = descriptor.Culture;
if (Utf8String.IsNullOrEmpty(Culture))
Culture = null;
}

/// <inheritdoc />
Expand Down
16 changes: 12 additions & 4 deletions src/AsmResolver.DotNet/ReferenceImporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -315,10 +315,18 @@ public virtual TypeSignature ImportTypeSignature(Type type)
if (corlibType != null)
return corlibType;

var reference = new TypeReference(TargetModule,
ImportAssembly(new ReflectionAssemblyDescriptor(TargetModule, type.Assembly.GetName())),
type.Namespace,
type.Name);
TypeReference reference;

if (type.IsNested)
{
var scope = (IResolutionScope) ImportType(type.DeclaringType!);
reference = new TypeReference(TargetModule, scope, null, type.Name);
}
else
{
var scope = ImportAssembly(new ReflectionAssemblyDescriptor(TargetModule, type.Assembly.GetName()));
reference = new TypeReference(TargetModule, scope, type.Namespace, type.Name);
}

return new TypeDefOrRefSignature(reference, type.IsValueType);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
using System;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Reflection.Emit;
using AsmResolver.DotNet.Code.Cil;
using AsmResolver.DotNet.Signatures;
using AsmResolver.DotNet.Signatures.Types;
using AsmResolver.DotNet.TestCases.Methods;
using AsmResolver.PE.DotNet.Cil;
using Xunit;
using MethodAttributes = AsmResolver.PE.DotNet.Metadata.Tables.Rows.MethodAttributes;

namespace AsmResolver.DotNet.Dynamic.Tests
{
Expand Down Expand Up @@ -89,5 +93,32 @@ public void ReadDynamicMethodInitializedByDynamicILInfo()
var instruction = Assert.Single(definition.CilMethodBody.Instructions);
Assert.Equal(CilOpCodes.Ret, instruction.OpCode);
}

[Fact]
public void ImportNestedType()
{
// https://github.com/Washi1337/AsmResolver/issues/363

var method = new DynamicMethod("Test", typeof(void), Type.EmptyTypes);
var cil = method.GetILGenerator();
cil.Emit(OpCodes.Call, typeof(NestedClass).GetMethod(nameof(NestedClass.TestMethod))!);
cil.Emit(OpCodes.Ret);

var contextModule = ModuleDefinition.FromFile(typeof(DynamicMethodDefinitionTest).Assembly.Location);
var definition = new DynamicMethodDefinition(contextModule, method);

Assert.NotNull(definition.CilMethodBody);
var reference = Assert.IsAssignableFrom<IMethodDescriptor>(definition.CilMethodBody.Instructions[0].Operand);
var declaringType = reference.DeclaringType;
Assert.NotNull(declaringType);
Assert.Equal(nameof(NestedClass), declaringType.Name);
Assert.NotNull(declaringType.DeclaringType);
Assert.Equal(nameof(DynamicMethodDefinitionTest), declaringType.DeclaringType.Name);
}

internal static class NestedClass
{
public static void TestMethod() => Console.WriteLine("TestMethod");
}
}
}
15 changes: 15 additions & 0 deletions test/AsmResolver.DotNet.Tests/ReferenceImporterTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using AsmResolver.DotNet.Signatures;
using AsmResolver.DotNet.Signatures.Types;
using AsmResolver.DotNet.TestCases.Fields;
using AsmResolver.DotNet.TestCases.NestedClasses;
using AsmResolver.PE.DotNet.Metadata.Tables.Rows;
using Xunit;

Expand Down Expand Up @@ -137,6 +138,20 @@ public void ImportNestedTypeDefinitionShouldImportParentType()
Assert.Equal(_module, reference.DeclaringType.Module);
}

[Fact]
public void ImportNestedTypeViaReflectionShouldImportParentType()
{
var module = ModuleDefinition.FromFile(typeof(TopLevelClass1).Assembly.Location);
var declaringType = module.TopLevelTypes.First(t => t.Name == nameof(TopLevelClass1));
var nested = declaringType.NestedTypes.First(t => t.Name == nameof(TopLevelClass1.Nested1));

var result = _importer.ImportType(typeof(TopLevelClass1.Nested1));

Assert.Equal(nested, result, Comparer);
Assert.Equal(_module, result.Module);
Assert.Equal(_module, result.DeclaringType?.Module);
}

[Fact]
public void ImportSimpleTypeFromReflectionShouldResultInTypeRef()
{
Expand Down