diff --git a/src/AsmResolver.DotNet/TypeDefinition.cs b/src/AsmResolver.DotNet/TypeDefinition.cs index c4d116a63..a6b19bb96 100644 --- a/src/AsmResolver.DotNet/TypeDefinition.cs +++ b/src/AsmResolver.DotNet/TypeDefinition.cs @@ -94,7 +94,8 @@ public TypeDefinition(Utf8String? ns, Utf8String? name, TypeAttributes attribute public Utf8String? Namespace { get => _namespace.GetValue(this); - set => _namespace.SetValue(value); + set => _namespace.SetValue(Utf8String.IsNullOrEmpty(value) ? null : value); + // According to the specification, the namespace should always be null or non-empty. } string? ITypeDescriptor.Namespace => Namespace; diff --git a/src/AsmResolver.DotNet/TypeReference.cs b/src/AsmResolver.DotNet/TypeReference.cs index dd6e3f6e1..37b19b40c 100644 --- a/src/AsmResolver.DotNet/TypeReference.cs +++ b/src/AsmResolver.DotNet/TypeReference.cs @@ -84,7 +84,8 @@ public Utf8String? Name public Utf8String? Namespace { get => _namespace.GetValue(this); - set => _namespace.SetValue(value); + set => _namespace.SetValue(Utf8String.IsNullOrEmpty(value) ? null : value); + // According to the specification, the namespace should always be null or non-empty. } string? ITypeDescriptor.Namespace => Namespace; diff --git a/test/AsmResolver.DotNet.Tests/Signatures/SignatureComparerTest.cs b/test/AsmResolver.DotNet.Tests/Signatures/SignatureComparerTest.cs index 565d3b81a..83d9dd708 100644 --- a/test/AsmResolver.DotNet.Tests/Signatures/SignatureComparerTest.cs +++ b/test/AsmResolver.DotNet.Tests/Signatures/SignatureComparerTest.cs @@ -59,6 +59,14 @@ public void MatchTopLevelTypeRefTypeRefDifferentNamespace() Assert.NotEqual(reference1, reference2, _comparer); } + [Fact] + public void MatchTopLevelTypeRefTypeRefWithEmptyNamespace() + { + var reference1 = new TypeReference(_someAssemblyReference, null, "SomeType"); + var reference2 = new TypeReference(_someAssemblyReference, "", "SomeType"); + Assert.Equal(reference1, reference2, _comparer); + } + [Fact] public void MatchTopLevelTypeRefTypeDef() { diff --git a/test/AsmResolver.DotNet.Tests/TypeDefinitionTest.cs b/test/AsmResolver.DotNet.Tests/TypeDefinitionTest.cs index 95ae5c149..15750d4ff 100644 --- a/test/AsmResolver.DotNet.Tests/TypeDefinitionTest.cs +++ b/test/AsmResolver.DotNet.Tests/TypeDefinitionTest.cs @@ -773,5 +773,19 @@ public void AddSameNestedTypeToDifferentTypesShouldThrow() Assert.Throws(() => type2.NestedTypes.Add(nestedType)); } + [Fact] + public void NamespaceShouldBeNullIfEmptyStringGiven() + { + var type = new TypeDefinition(string.Empty, "SomeType", TypeAttributes.Public); + Assert.Null(type.Namespace); + } + + [Fact] + public void NamespaceShouldBeNullIfEmptyStringSet() + { + var type = new TypeDefinition("SomeNamespace", "SomeType", TypeAttributes.Public); + type.Namespace = string.Empty; + Assert.Null(type.Namespace); + } } } diff --git a/test/AsmResolver.DotNet.Tests/TypeReferenceTest.cs b/test/AsmResolver.DotNet.Tests/TypeReferenceTest.cs index 16ae99f06..d2211247c 100644 --- a/test/AsmResolver.DotNet.Tests/TypeReferenceTest.cs +++ b/test/AsmResolver.DotNet.Tests/TypeReferenceTest.cs @@ -189,5 +189,22 @@ public void NonCorLibTypeToTypeSignatureShouldReturnTypeDefOrRef() Assert.Equal(signature.Type, reference, Comparer); } + + [Fact] + public void NamespaceShouldBeNullIfEmptyStringGiven() + { + var module = new ModuleDefinition("SomeModule"); + var type = new TypeReference(module, string.Empty, "SomeType"); + Assert.Null(type.Namespace); + } + + [Fact] + public void NamespaceShouldBeNullIfEmptyStringSet() + { + var module = new ModuleDefinition("SomeModule"); + var type = new TypeReference(module, "SomeNamespace", "SomeType"); + type.Namespace = string.Empty; + Assert.Null(type.Namespace); + } } }