diff --git a/src/AsmResolver.DotNet/Builder/DotNetDirectoryBuffer.CodedIndices.cs b/src/AsmResolver.DotNet/Builder/DotNetDirectoryBuffer.CodedIndices.cs
index 52e6c3df8..09a9f3afa 100644
--- a/src/AsmResolver.DotNet/Builder/DotNetDirectoryBuffer.CodedIndices.cs
+++ b/src/AsmResolver.DotNet/Builder/DotNetDirectoryBuffer.CodedIndices.cs
@@ -41,7 +41,7 @@ private uint AddResolutionScope(IResolutionScope? scope, bool allowDuplicates, b
TableIndex.AssemblyRef => AddAssemblyReference(scope as AssemblyReference, allowDuplicates, preserveRid),
TableIndex.TypeRef => AddTypeReference(scope as TypeReference, allowDuplicates, preserveRid),
TableIndex.ModuleRef => AddModuleReference(scope as ModuleReference, allowDuplicates, preserveRid),
- TableIndex.Module => 0,
+ TableIndex.Module => new MetadataToken(TableIndex.Module, 1),
_ => throw new ArgumentOutOfRangeException(nameof(scope))
};
diff --git a/src/AsmResolver.DotNet/DefaultMetadataResolver.cs b/src/AsmResolver.DotNet/DefaultMetadataResolver.cs
index 0d625ed02..e80d5cae4 100644
--- a/src/AsmResolver.DotNet/DefaultMetadataResolver.cs
+++ b/src/AsmResolver.DotNet/DefaultMetadataResolver.cs
@@ -166,8 +166,11 @@ public TypeResolution(IAssemblyResolver resolver)
public TypeDefinition? ResolveTypeReference(TypeReference? reference)
{
- var scope = reference?.Scope;
- if (reference?.Name is null || scope is null || _scopeStack.Contains(scope))
+ if (reference is null)
+ return null;
+
+ var scope = reference.Scope ?? reference.Module;
+ if (reference.Name is null || scope is null || _scopeStack.Contains(scope))
return null;
_scopeStack.Push(scope);
@@ -241,13 +244,6 @@ public TypeResolution(IAssemblyResolver resolver)
private TypeDefinition? FindTypeInModule(ModuleDefinition module, Utf8String? ns, Utf8String name)
{
- for (int i = 0; i < module.ExportedTypes.Count; i++)
- {
- var exportedType = module.ExportedTypes[i];
- if (exportedType.IsTypeOfUtf8(ns, name))
- return ResolveExportedType(exportedType);
- }
-
for (int i = 0; i < module.TopLevelTypes.Count; i++)
{
var type = module.TopLevelTypes[i];
@@ -255,6 +251,13 @@ public TypeResolution(IAssemblyResolver resolver)
return type;
}
+ for (int i = 0; i < module.ExportedTypes.Count; i++)
+ {
+ var exportedType = module.ExportedTypes[i];
+ if (exportedType.IsTypeOfUtf8(ns, name))
+ return ResolveExportedType(exportedType);
+ }
+
return null;
}
diff --git a/src/AsmResolver.DotNet/ReferenceImporter.cs b/src/AsmResolver.DotNet/ReferenceImporter.cs
index 1f0821c10..c9c2b26c7 100644
--- a/src/AsmResolver.DotNet/ReferenceImporter.cs
+++ b/src/AsmResolver.DotNet/ReferenceImporter.cs
@@ -30,20 +30,12 @@ public ModuleDefinition TargetModule
get;
}
- private static void AssertTypeIsValid(ITypeDefOrRef? type)
- {
- if (type is null)
- throw new ArgumentNullException(nameof(type));
- if (type.Scope is null)
- throw new ArgumentException("Cannot import types that are not added to a module.");
- }
-
///
/// Imports a resolution scope.
///
/// The resolution scope to import.
/// The imported resolution scope.
- public IResolutionScope ImportScope(IResolutionScope? scope)
+ public IResolutionScope ImportScope(IResolutionScope scope)
{
if (scope is null)
throw new ArgumentNullException(nameof(scope));
@@ -181,14 +173,16 @@ public ITypeDefOrRef ImportType(ITypeDefOrRef type)
/// The imported type.
protected virtual ITypeDefOrRef ImportType(TypeDefinition type)
{
- AssertTypeIsValid(type);
-
+ if (type is null)
+ throw new ArgumentNullException(nameof(type));
if (type.IsImportedInModule(TargetModule))
return type;
+ if (((ITypeDescriptor) type).Scope is not { } scope)
+ throw new ArgumentException("Cannot import a type that has not been added to a module.");
return new TypeReference(
TargetModule,
- ImportScope(((ITypeDescriptor) type).Scope),
+ ImportScope(scope),
type.Namespace,
type.Name);
}
@@ -200,12 +194,18 @@ protected virtual ITypeDefOrRef ImportType(TypeDefinition type)
/// The imported type.
protected virtual ITypeDefOrRef ImportType(TypeReference type)
{
- AssertTypeIsValid(type);
-
+ if (type is null)
+ throw new ArgumentNullException(nameof(type));
if (type.IsImportedInModule(TargetModule))
return type;
- return new TypeReference(TargetModule, ImportScope(type.Scope!), type.Namespace, type.Name);
+ return new TypeReference(
+ TargetModule,
+ type.Scope is not null
+ ? ImportScope(type.Scope)
+ : null,
+ type.Namespace,
+ type.Name);
}
///
@@ -215,7 +215,8 @@ protected virtual ITypeDefOrRef ImportType(TypeReference type)
/// The imported type.
protected virtual ITypeDefOrRef ImportType(TypeSpecification type)
{
- AssertTypeIsValid(type);
+ if (type is null)
+ throw new ArgumentNullException(nameof(type));
if (type.Signature is null)
throw new ArgumentNullException(nameof(type));
diff --git a/src/AsmResolver.DotNet/Serialized/SerializedTypeReference.cs b/src/AsmResolver.DotNet/Serialized/SerializedTypeReference.cs
index 4e0cb839b..75bf2122a 100644
--- a/src/AsmResolver.DotNet/Serialized/SerializedTypeReference.cs
+++ b/src/AsmResolver.DotNet/Serialized/SerializedTypeReference.cs
@@ -41,7 +41,7 @@ public SerializedTypeReference(ModuleReaderContext context, MetadataToken token,
protected override IResolutionScope? GetScope()
{
if (_row.ResolutionScope == 0)
- return _context.ParentModule;
+ return null;
var tablesStream = _context.TablesStream;
var decoder = tablesStream.GetIndexEncoder(CodedIndex.ResolutionScope);
diff --git a/test/AsmResolver.DotNet.Tests/MetadataResolverTest.cs b/test/AsmResolver.DotNet.Tests/MetadataResolverTest.cs
index 06d4d5ca7..e2dd55a28 100644
--- a/test/AsmResolver.DotNet.Tests/MetadataResolverTest.cs
+++ b/test/AsmResolver.DotNet.Tests/MetadataResolverTest.cs
@@ -4,6 +4,7 @@
using AsmResolver.DotNet.Signatures;
using AsmResolver.DotNet.TestCases.NestedClasses;
using AsmResolver.PE.DotNet.Cil;
+using AsmResolver.PE.DotNet.Metadata.Tables;
using Xunit;
namespace AsmResolver.DotNet.Tests
@@ -52,6 +53,7 @@ public void ResolveSystemObjectNetCore()
var reference = new TypeReference(module.CorLibTypeFactory.CorLibScope, "System", "Object");
var definition = _coreResolver.ResolveType(reference);
+ Assert.NotNull(definition);
Assert.True(definition.IsTypeOf(reference.Namespace, reference.Name));
}
@@ -69,8 +71,8 @@ public void ResolveType()
{
var module = ModuleDefinition.FromFile(typeof(TopLevelClass1).Assembly.Location);
- var topLevelClass1 = new TypeReference(new AssemblyReference(module.Assembly),
- typeof(TopLevelClass1).Namespace, typeof(TopLevelClass1).Name);
+ var topLevelClass1 = new TypeReference(new AssemblyReference(module.Assembly!),
+ typeof(TopLevelClass1).Namespace, nameof(TopLevelClass1));
var definition = _coreResolver.ResolveType(topLevelClass1);
Assert.Equal((ITypeDefOrRef) topLevelClass1, definition, _comparer);
@@ -104,7 +106,7 @@ public void ResolveTypeReferenceThenChangeDefAndResolveAgain()
ITypeDefOrRef expected = new TypeReference(module.CorLibTypeFactory.CorLibScope, "System", "Object");
var reference = new TypeReference(module.CorLibTypeFactory.CorLibScope, "System", "Object");
- var definition = _fwResolver.ResolveType(reference);
+ var definition = _fwResolver.ResolveType(reference)!;
Assert.Equal(expected, definition, _comparer);
definition.Name = "String";
Assert.NotEqual(expected, _fwResolver.ResolveType(reference), _comparer);
@@ -115,9 +117,9 @@ public void ResolveNestedType()
{
var module = ModuleDefinition.FromFile(typeof(TopLevelClass1).Assembly.Location);
- var topLevelClass1 = new TypeReference(new AssemblyReference(module.Assembly),
- typeof(TopLevelClass1).Namespace, typeof(TopLevelClass1).Name);
- var nested1 = new TypeReference(topLevelClass1,null, typeof(TopLevelClass1.Nested1).Name);
+ var topLevelClass1 = new TypeReference(new AssemblyReference(module.Assembly!),
+ typeof(TopLevelClass1).Namespace, nameof(TopLevelClass1));
+ var nested1 = new TypeReference(topLevelClass1,null, nameof(TopLevelClass1.Nested1));
var definition = _coreResolver.ResolveType(nested1);
@@ -129,16 +131,52 @@ public void ResolveNestedNestedType()
{
var module = ModuleDefinition.FromFile(typeof(TopLevelClass1).Assembly.Location);
- var topLevelClass1 = new TypeReference(new AssemblyReference(module.Assembly),
- typeof(TopLevelClass1).Namespace, typeof(TopLevelClass1).Name);
- var nested1 = new TypeReference(topLevelClass1,null, typeof(TopLevelClass1.Nested1).Name);
- var nested1nested1 = new TypeReference(nested1,null, typeof(TopLevelClass1.Nested1.Nested1Nested1).Name);
+ var topLevelClass1 = new TypeReference(new AssemblyReference(module.Assembly!),
+ typeof(TopLevelClass1).Namespace, nameof(TopLevelClass1));
+ var nested1 = new TypeReference(topLevelClass1,null, nameof(TopLevelClass1.Nested1));
+ var nested1nested1 = new TypeReference(nested1,null, nameof(TopLevelClass1.Nested1.Nested1Nested1));
var definition = _fwResolver.ResolveType(nested1nested1);
Assert.Equal((ITypeDefOrRef) nested1nested1, definition, _comparer);
}
+ [Fact]
+ public void ResolveTypeWithModuleScope()
+ {
+ var module = ModuleDefinition.FromBytes(Properties.Resources.TypeRefModuleScope);
+ var reference = module.LookupMember(new MetadataToken(TableIndex.TypeRef, 2));
+
+ var definition = reference.Resolve();
+
+ Assert.NotNull(definition);
+ Assert.Same(module, definition.Module);
+ }
+
+ [Fact]
+ public void ResolveTypeWithNullScopeCurrentModule()
+ {
+ var module = ModuleDefinition.FromBytes(Properties.Resources.TypeRefNullScope_CurrentModule);
+ var reference = module.LookupMember(new MetadataToken(TableIndex.TypeRef, 2));
+
+ var definition = reference.Resolve();
+
+ Assert.NotNull(definition);
+ Assert.Same(module, definition.Module);
+ }
+
+ [Fact]
+ public void ResolveTypeWithNullScopeExportedType()
+ {
+ var module = ModuleDefinition.FromBytes(Properties.Resources.TypeRefNullScope_ExportedType);
+ var reference = module.LookupMember(new MetadataToken(TableIndex.TypeRef, 1));
+
+ var definition = reference.Resolve();
+
+ Assert.NotNull(definition);
+ Assert.Equal("mscorlib", definition.Module!.Assembly!.Name);
+ }
+
[Fact]
public void ResolveConsoleWriteLineMethod()
{
@@ -187,13 +225,13 @@ public void ResolveExportedMemberReference()
var resolver = (AssemblyResolverBase) module.MetadataResolver.AssemblyResolver;
resolver.AddToCache(assembly1, assembly1);
resolver.AddToCache(assembly2, assembly2);
- resolver = (AssemblyResolverBase) assembly1.ManifestModule.MetadataResolver.AssemblyResolver;
+ resolver = (AssemblyResolverBase) assembly1.ManifestModule!.MetadataResolver.AssemblyResolver;
resolver.AddToCache(assembly1, assembly1);
resolver.AddToCache(assembly2, assembly2);
// Resolve
- var instructions = module.ManagedEntryPointMethod.CilMethodBody.Instructions;
- Assert.NotNull(((IMethodDescriptor) instructions[0].Operand).Resolve());
+ var instructions = module.ManagedEntryPointMethod!.CilMethodBody!.Instructions;
+ Assert.NotNull(((IMethodDescriptor) instructions[0].Operand!).Resolve());
}
[Fact]
@@ -208,10 +246,10 @@ public void MaliciousExportedTypeLoop()
var resolver = (AssemblyResolverBase) module.MetadataResolver.AssemblyResolver;
resolver.AddToCache(assembly1, assembly1);
resolver.AddToCache(assembly2, assembly2);
- resolver = (AssemblyResolverBase) assembly1.ManifestModule.MetadataResolver.AssemblyResolver;
+ resolver = (AssemblyResolverBase) assembly1.ManifestModule!.MetadataResolver.AssemblyResolver;
resolver.AddToCache(assembly1, assembly1);
resolver.AddToCache(assembly2, assembly2);
- resolver = (AssemblyResolverBase) assembly2.ManifestModule.MetadataResolver.AssemblyResolver;
+ resolver = (AssemblyResolverBase) assembly2.ManifestModule!.MetadataResolver.AssemblyResolver;
resolver.AddToCache(assembly1, assembly1);
resolver.AddToCache(assembly2, assembly2);
diff --git a/test/AsmResolver.DotNet.Tests/Properties/Resources.Designer.cs b/test/AsmResolver.DotNet.Tests/Properties/Resources.Designer.cs
index e67584486..5f2f0400f 100644
--- a/test/AsmResolver.DotNet.Tests/Properties/Resources.Designer.cs
+++ b/test/AsmResolver.DotNet.Tests/Properties/Resources.Designer.cs
@@ -387,5 +387,26 @@ public static byte[] ArgListTest {
return ((byte[])(obj));
}
}
+
+ public static byte[] TypeRefModuleScope {
+ get {
+ object obj = ResourceManager.GetObject("TypeRefModuleScope", resourceCulture);
+ return ((byte[])(obj));
+ }
+ }
+
+ public static byte[] TypeRefNullScope_CurrentModule {
+ get {
+ object obj = ResourceManager.GetObject("TypeRefNullScope_CurrentModule", resourceCulture);
+ return ((byte[])(obj));
+ }
+ }
+
+ public static byte[] TypeRefNullScope_ExportedType {
+ get {
+ object obj = ResourceManager.GetObject("TypeRefNullScope_ExportedType", resourceCulture);
+ return ((byte[])(obj));
+ }
+ }
}
}
diff --git a/test/AsmResolver.DotNet.Tests/Properties/Resources.resx b/test/AsmResolver.DotNet.Tests/Properties/Resources.resx
index 8ebc736a8..5edb05e41 100644
--- a/test/AsmResolver.DotNet.Tests/Properties/Resources.resx
+++ b/test/AsmResolver.DotNet.Tests/Properties/Resources.resx
@@ -165,4 +165,13 @@
..\Resources\ArgListTest.exe;System.Byte[], mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
+
+ ..\Resources\TypeRefModuleScope.exe;System.Byte[], mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
+
+
+ ..\Resources\TypeRefNullScope_CurrentModule.exe;System.Byte[], mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
+
+
+ ..\Resources\TypeRefNullScope_ExportedType.exe;System.Byte[], mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
+
diff --git a/test/AsmResolver.DotNet.Tests/Resources/TypeRefModuleScope.exe b/test/AsmResolver.DotNet.Tests/Resources/TypeRefModuleScope.exe
new file mode 100644
index 000000000..e9d316359
Binary files /dev/null and b/test/AsmResolver.DotNet.Tests/Resources/TypeRefModuleScope.exe differ
diff --git a/test/AsmResolver.DotNet.Tests/Resources/TypeRefNullScope_CurrentModule.exe b/test/AsmResolver.DotNet.Tests/Resources/TypeRefNullScope_CurrentModule.exe
new file mode 100644
index 000000000..3ade50d67
Binary files /dev/null and b/test/AsmResolver.DotNet.Tests/Resources/TypeRefNullScope_CurrentModule.exe differ
diff --git a/test/AsmResolver.DotNet.Tests/Resources/TypeRefNullScope_ExportedType.exe b/test/AsmResolver.DotNet.Tests/Resources/TypeRefNullScope_ExportedType.exe
new file mode 100644
index 000000000..5bf8a8be2
Binary files /dev/null and b/test/AsmResolver.DotNet.Tests/Resources/TypeRefNullScope_ExportedType.exe differ
diff --git a/test/AsmResolver.DotNet.Tests/Signatures/TypeSignatureTest.cs b/test/AsmResolver.DotNet.Tests/Signatures/TypeSignatureTest.cs
index 5c26a3518..d3afe8f78 100644
--- a/test/AsmResolver.DotNet.Tests/Signatures/TypeSignatureTest.cs
+++ b/test/AsmResolver.DotNet.Tests/Signatures/TypeSignatureTest.cs
@@ -4,6 +4,7 @@
using AsmResolver.DotNet.Signatures.Types;
using AsmResolver.DotNet.TestCases.Generics;
using AsmResolver.DotNet.TestCases.Types;
+using AsmResolver.PE.DotNet.Metadata.Tables;
using AsmResolver.PE.DotNet.Metadata.Tables.Rows;
using Xunit;
@@ -619,5 +620,38 @@ public void IgnorePinnedModifiers()
Assert.True(type1.IsCompatibleWith(type2));
Assert.True(type2.IsCompatibleWith(type1));
}
+
+ [Fact]
+ public void GetModuleOfTypeDefOrRef()
+ {
+ var module = ModuleDefinition.FromBytes(Properties.Resources.HelloWorld);
+ var signature = module.GetOrCreateModuleType().ToTypeSignature();
+ Assert.Same(module, signature.Module);
+ }
+
+ [Fact]
+ public void GetModuleOfTypeDefOrRefWithNullScope()
+ {
+ var module = ModuleDefinition.FromBytes(Properties.Resources.TypeRefNullScope_CurrentModule);
+ var signature = module
+ .LookupMember(new MetadataToken(TableIndex.TypeRef, 2))
+ .ToTypeSignature();
+
+ Assert.Null(signature.Scope);
+ Assert.Same(module, signature.Module);
+ }
+
+ [Fact]
+ public void GetModuleOfSpecificationTypeWithNullScope()
+ {
+ var module = ModuleDefinition.FromBytes(Properties.Resources.TypeRefNullScope_CurrentModule);
+ var signature = module
+ .LookupMember(new MetadataToken(TableIndex.TypeRef, 2))
+ .ToTypeSignature()
+ .MakeSzArrayType();
+
+ Assert.Null(signature.Scope);
+ Assert.Same(module, signature.Module);
+ }
}
}
diff --git a/test/AsmResolver.DotNet.Tests/TypeReferenceTest.cs b/test/AsmResolver.DotNet.Tests/TypeReferenceTest.cs
index a0a5ae2e5..002801db9 100644
--- a/test/AsmResolver.DotNet.Tests/TypeReferenceTest.cs
+++ b/test/AsmResolver.DotNet.Tests/TypeReferenceTest.cs
@@ -1,4 +1,5 @@
using System;
+using System.Linq;
using AsmResolver.DotNet.Signatures;
using AsmResolver.DotNet.Signatures.Types;
using AsmResolver.PE.DotNet.Metadata.Tables;
@@ -15,15 +16,150 @@ public class TypeReferenceTest
public void ReadAssemblyRefScope()
{
var module = ModuleDefinition.FromBytes(Properties.Resources.HelloWorld);
- var typeRef = (TypeReference) module.LookupMember(new MetadataToken(TableIndex.TypeRef, 13));
- Assert.Equal("mscorlib", typeRef.Scope.Name);
+ var typeRef = module.LookupMember(new MetadataToken(TableIndex.TypeRef, 13));
+
+ var scope = Assert.IsAssignableFrom(typeRef.Scope);
+ Assert.Equal("mscorlib", scope.Name);
+ }
+
+ [Fact]
+ public void WriteAssemblyRefScope()
+ {
+ var module = new ModuleDefinition("SomeModule");
+ module.GetOrCreateModuleType().Fields.Add(new FieldDefinition(
+ "SomeField",
+ FieldAttributes.Static,
+ new TypeDefOrRefSignature(new TypeReference(
+ new AssemblyReference("SomeAssembly", new Version(1, 0, 0, 0)),
+ "SomeNamespace",
+ "SomeName")
+ ).ImportWith(module.DefaultImporter)
+ ));
+
+ var image = module.ToPEImage();
+
+ var newModule = ModuleDefinition.FromImage(image);
+ var typeRef = newModule.GetOrCreateModuleType().Fields.First().Signature!.FieldType.GetUnderlyingTypeDefOrRef()!;
+
+ var scope = Assert.IsAssignableFrom(typeRef.Scope);
+ Assert.Equal("SomeAssembly", scope.Name);
+ }
+
+ [Fact]
+ public void ReadTypeRefScope()
+ {
+ var module = ModuleDefinition.FromBytes(Properties.Resources.HelloWorld);
+ var typeRef = module.LookupMember(new MetadataToken(TableIndex.TypeRef, 4));
+
+ var scope = Assert.IsAssignableFrom(typeRef.Scope);
+ Assert.Equal("DebuggableAttribute", scope.Name);
+ }
+
+ [Fact]
+ public void WriteTypeRefScope()
+ {
+ var module = new ModuleDefinition("SomeModule");
+ module.GetOrCreateModuleType().Fields.Add(new FieldDefinition(
+ "SomeField",
+ FieldAttributes.Static,
+ new TypeDefOrRefSignature(new TypeReference(
+ new TypeReference(
+ new AssemblyReference("SomeAssembly", new Version(1, 0, 0, 0)),
+ "SomeNamespace",
+ "SomeName"),
+ null,
+ "SomeNestedType"
+ )).ImportWith(module.DefaultImporter)
+ ));
+
+ var image = module.ToPEImage();
+
+ var newModule = ModuleDefinition.FromImage(image);
+ var typeRef = newModule.GetOrCreateModuleType().Fields.First().Signature!.FieldType.GetUnderlyingTypeDefOrRef()!;
+
+ var scope = Assert.IsAssignableFrom(typeRef.Scope);
+ Assert.Equal("SomeName", scope.Name);
+ }
+
+ [Fact]
+ public void ReadModuleScope()
+ {
+ var module = ModuleDefinition.FromBytes(Properties.Resources.TypeRefModuleScope);
+ var typeRef = module.LookupMember(new MetadataToken(TableIndex.TypeRef, 2));
+
+ var scope = Assert.IsAssignableFrom(typeRef.Scope);
+ Assert.Same(module, scope);
+ }
+
+ [Fact]
+ public void WriteModuleScope()
+ {
+ var module = new ModuleDefinition("SomeModule");
+ module.GetOrCreateModuleType().Fields.Add(new FieldDefinition(
+ "SomeField",
+ FieldAttributes.Static,
+ new TypeDefOrRefSignature(new TypeReference(
+ module,
+ "SomeNamepace",
+ "SomeName")
+ ).ImportWith(module.DefaultImporter)
+ ));
+
+ var image = module.ToPEImage();
+
+ var newModule = ModuleDefinition.FromImage(image);
+ var typeRef = newModule.GetOrCreateModuleType().Fields.First().Signature!.FieldType.GetUnderlyingTypeDefOrRef()!;
+
+ var scope = Assert.IsAssignableFrom(typeRef.Scope);
+ Assert.Equal(module.Name, scope.Name);
+ }
+
+ [Fact]
+ public void WriteNullScope()
+ {
+ var module = new ModuleDefinition("SomeModule");
+ module.GetOrCreateModuleType().Fields.Add(new FieldDefinition(
+ "SomeField",
+ FieldAttributes.Static,
+ new TypeDefOrRefSignature(new TypeReference(
+ null,
+ "SomeNamespace",
+ "SomeName")
+ ).ImportWith(module.DefaultImporter)
+ ));
+
+ var image = module.ToPEImage();
+
+ var newModule = ModuleDefinition.FromImage(image);
+ var typeRef = newModule.GetOrCreateModuleType().Fields.First().Signature!.FieldType.GetUnderlyingTypeDefOrRef()!;
+
+ Assert.Null(typeRef.Scope);
+ }
+
+ [Fact]
+ public void ReadNullScopeCurrentModule()
+ {
+ var module = ModuleDefinition.FromBytes(Properties.Resources.TypeRefNullScope_CurrentModule);
+ var typeRef = module.LookupMember(new MetadataToken(TableIndex.TypeRef, 2));
+
+ Assert.Null(typeRef.Scope);
+ }
+
+ [Fact]
+ public void ReadNullScopeExportedType()
+ {
+ var module = ModuleDefinition.FromBytes(Properties.Resources.TypeRefNullScope_ExportedType);
+ var typeRef = module.LookupMember(new MetadataToken(TableIndex.TypeRef, 1));
+
+ Assert.Null(typeRef.Scope);
}
[Fact]
public void ReadName()
{
var module = ModuleDefinition.FromBytes(Properties.Resources.HelloWorld);
- var typeRef = (TypeReference) module.LookupMember(new MetadataToken(TableIndex.TypeRef, 13));
+ var typeRef = module.LookupMember(new MetadataToken(TableIndex.TypeRef, 13));
+
Assert.Equal("Console", typeRef.Name);
}
@@ -31,7 +167,8 @@ public void ReadName()
public void ReadNamespace()
{
var module = ModuleDefinition.FromBytes(Properties.Resources.HelloWorld);
- var typeRef = (TypeReference) module.LookupMember(new MetadataToken(TableIndex.TypeRef, 13));
+ var typeRef = module.LookupMember(new MetadataToken(TableIndex.TypeRef, 13));
+
Assert.Equal("System", typeRef.Namespace);
}
@@ -41,6 +178,7 @@ public void CorLibTypeToTypeSignatureShouldReturnCorLibTypeSignature()
var module = new ModuleDefinition("SomeModule");
var reference = new TypeReference(module, module.CorLibTypeFactory.CorLibScope, "System", "Object");
var signature = Assert.IsAssignableFrom(reference.ToTypeSignature());
+
Assert.Equal(ElementType.Object, signature.ElementType);
}
@@ -50,6 +188,7 @@ public void NonCorLibTypeToTypeSignatureShouldReturnTypeDefOrRef()
var module = new ModuleDefinition("SomeModule");
var reference = new TypeReference(module, module.CorLibTypeFactory.CorLibScope, "System", "Array");
var signature = Assert.IsAssignableFrom(reference.ToTypeSignature());
+
Assert.Equal(signature.Type, reference, Comparer);
}
}
diff --git a/test/TestBinaries/DotNet/HelloWorld/HelloWorld.csproj b/test/TestBinaries/DotNet/HelloWorld/HelloWorld.csproj
index 329c82d3e..53d2dbb55 100644
--- a/test/TestBinaries/DotNet/HelloWorld/HelloWorld.csproj
+++ b/test/TestBinaries/DotNet/HelloWorld/HelloWorld.csproj
@@ -9,6 +9,10 @@
AnyCPU
+
+
+ none
+
diff --git a/test/TestBinaries/DotNet/HelloWorld/Program.cs b/test/TestBinaries/DotNet/HelloWorld/Program.cs
index 575ec4cc4..6159d9786 100644
--- a/test/TestBinaries/DotNet/HelloWorld/Program.cs
+++ b/test/TestBinaries/DotNet/HelloWorld/Program.cs
@@ -1,4 +1,5 @@
using System;
+using System.Linq;
namespace HelloWorld
{