diff --git a/src/AsmResolver.DotNet/DefaultMetadataResolver.cs b/src/AsmResolver.DotNet/DefaultMetadataResolver.cs
index e80d5cae4..519e769da 100644
--- a/src/AsmResolver.DotNet/DefaultMetadataResolver.cs
+++ b/src/AsmResolver.DotNet/DefaultMetadataResolver.cs
@@ -177,6 +177,13 @@ public TypeResolution(IAssemblyResolver resolver)
switch (scope.MetadataToken.Table)
{
case TableIndex.AssemblyRef:
+ if (reference.Module?.Assembly is { } assembly)
+ {
+ // Are we referencing the current assembly the reference was declared in?
+ if (SignatureComparer.Default.Equals(scope.GetAssembly(), assembly))
+ return FindTypeInModule(reference.Module, reference.Namespace, reference.Name);
+ }
+
var assemblyDefScope = _assemblyResolver.Resolve((AssemblyReference) scope);
return assemblyDefScope is not null
? FindTypeInAssembly(assemblyDefScope, reference.Namespace, reference.Name)
diff --git a/src/AsmResolver.DotNet/Signatures/CustomAttributeArgument.cs b/src/AsmResolver.DotNet/Signatures/CustomAttributeArgument.cs
index 8683f8925..2ae57418d 100644
--- a/src/AsmResolver.DotNet/Signatures/CustomAttributeArgument.cs
+++ b/src/AsmResolver.DotNet/Signatures/CustomAttributeArgument.cs
@@ -19,7 +19,9 @@ public class CustomAttributeArgument
/// The type of the argument to read.
/// The input stream.
/// The argument.
- public static CustomAttributeArgument FromReader(in BlobReaderContext context, TypeSignature argumentType,
+ public static CustomAttributeArgument FromReader(
+ in BlobReaderContext context,
+ TypeSignature argumentType,
ref BinaryStreamReader reader)
{
var elementReader = CustomAttributeArgumentReader.Create();
diff --git a/src/AsmResolver.DotNet/TypeReference.cs b/src/AsmResolver.DotNet/TypeReference.cs
index 5e5294d7b..23abe8a89 100644
--- a/src/AsmResolver.DotNet/TypeReference.cs
+++ b/src/AsmResolver.DotNet/TypeReference.cs
@@ -140,7 +140,8 @@ public TypeSignature ToTypeSignature(bool isValueType)
}
///
- public bool IsImportedInModule(ModuleDefinition module) => Module == module;
+ public bool IsImportedInModule(ModuleDefinition module) =>
+ Module == module && (Scope?.IsImportedInModule(module) ?? false);
///
/// Imports the type reference using the provided reference importer object.
diff --git a/test/AsmResolver.DotNet.Tests/CustomAttributeTest.cs b/test/AsmResolver.DotNet.Tests/CustomAttributeTest.cs
index 352ddb998..79e4faa9c 100644
--- a/test/AsmResolver.DotNet.Tests/CustomAttributeTest.cs
+++ b/test/AsmResolver.DotNet.Tests/CustomAttributeTest.cs
@@ -85,7 +85,7 @@ private static CustomAttribute GetCustomAttributeTestCase(
attributeName += "`1";
var attribute = method.CustomAttributes
- .First(c => c.Constructor!.DeclaringType!.Name.Value.StartsWith(attributeName));
+ .First(c => c.Constructor!.DeclaringType!.Name!.Value.StartsWith(attributeName));
if (access)
{
@@ -197,10 +197,13 @@ public void FixedComplexTypeArgument(bool rebuild, bool access)
var argument = attribute.Signature.FixedArguments[0];
var factory = attribute.Constructor!.Module!.CorLibTypeFactory;
- var listRef = new TypeReference(factory.CorLibScope, "System.Collections.Generic", "KeyValuePair`2");
- var instance = new GenericInstanceTypeSignature(listRef, false,
- new SzArrayTypeSignature(factory.String),
- new SzArrayTypeSignature(factory.Int32));
+ var instance = factory.CorLibScope
+ .CreateTypeReference("System.Collections.Generic", "KeyValuePair`2")
+ .MakeGenericInstanceType(
+ false,
+ factory.String.MakeSzArrayType(),
+ factory.Int32.MakeSzArrayType()
+ );
Assert.Equal(instance, argument.Element as TypeSignature, _comparer);
}
@@ -293,7 +296,7 @@ public void GenericTypeArgument(bool rebuild, bool access)
var module = attribute.Constructor!.Module!;
var nestedClass = (TypeDefinition) module.LookupMember(typeof(TestGenericType<>).MetadataToken);
- var expected = new GenericInstanceTypeSignature(nestedClass, false, module.CorLibTypeFactory.Object);
+ var expected = nestedClass.MakeGenericInstanceType(false, module.CorLibTypeFactory.Object);
var element = Assert.IsAssignableFrom(argument.Element);
Assert.Equal(expected, element, _comparer);
@@ -312,9 +315,9 @@ public void ArrayGenericTypeArgument(bool rebuild, bool access)
var module = attribute.Constructor!.Module!;
var nestedClass = (TypeDefinition) module.LookupMember(typeof(TestGenericType<>).MetadataToken);
- var expected = new SzArrayTypeSignature(
- new GenericInstanceTypeSignature(nestedClass, false, module.CorLibTypeFactory.Object)
- );
+ var expected = nestedClass
+ .MakeGenericInstanceType(false, module.CorLibTypeFactory.Object)
+ .MakeSzArrayType();
var element = Assert.IsAssignableFrom(argument.Element);
Assert.Equal(expected, element, _comparer);
@@ -413,7 +416,7 @@ public void FixedInt32EmptyArrayAsObject(bool rebuild, bool access)
var attribute = GetCustomAttributeTestCase(nameof(CustomAttributesTestClass.FixedInt32ArrayAsObjectEmptyArgument),rebuild, access);
var argument = attribute.Signature!.FixedArguments[0];
- var boxedArgument =Assert.IsAssignableFrom(argument.Element);
+ var boxedArgument = Assert.IsAssignableFrom(argument.Element);
Assert.Equal(Array.Empty