Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ internal partial struct TypeNameResolver
bool ignoreCase,
Assembly topLevelAssembly)
{
TypeName? parsed = TypeNameParser.Parse(typeName, throwOnError);
TypeName? parsed = TypeNameParser.Parse(typeName, throwOnError, new() { TopLevelAssemblyWasProvided = topLevelAssembly is not null });

if (parsed is null)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ public int MaxNodes
}
}
#pragma warning restore CA1822

internal bool TopLevelAssemblyWasProvided { get; set; }
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -188,14 +188,22 @@ private TypeNameParser(ReadOnlySpan<char> name, bool throwOnError, TypeNameParse
if (allowFullyQualifiedName && !TryParseAssemblyName(ref assemblyName))
{
#if SYSTEM_PRIVATE_CORELIB
// backward compat: throw FileLoadException for non-empty invalid strings
if (_throwOnError || !_inputString.TrimStart().StartsWith(","))
// Backward compatibility: throw for non-empty invalid assembly names.
if (!_inputString.TrimStart().StartsWith(","))
{
throw new IO.FileLoadException(SR.InvalidAssemblyName, _inputString.ToString());
// No matter what throwOnError is set to, if top level assembly was not provided, CLR throws FileLoadException for invalid assembly names.
if (!_parseOptions.TopLevelAssemblyWasProvided)
{
throw new IO.FileLoadException(SR.InvalidAssemblyName, _inputString.ToString());
}
// If top level was provided and user has requested for error, we throw ArgumentException.
else if (_throwOnError)
{
throw new ArgumentException(SR.Argument_AssemblyGetTypeCannotSpecifyAssembly);
}
}
#else
return null;
#endif
return null;
}

// No matter what was parsed, the full name string is allocated only once.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,8 @@ public void GetType_CoreAssembly()
{
Assert.Equal(typeof(int), Type.GetType("System.Int32", throwOnError: true));
Assert.Equal(typeof(int), Type.GetType("system.int32", throwOnError: true, ignoreCase: true));
Assert.Null(typeof(int).Assembly.GetType("a,b", throwOnError: false, ignoreCase: false));
Assert.Null(typeof(int).Assembly.GetType("a,b,c", throwOnError: false, ignoreCase: false));
}

[Fact]
Expand All @@ -294,7 +296,15 @@ public void GetType_InvalidAssemblyName()
Assert.Null(Type.GetType("MissingAssemblyName, "));
Assert.Null(Type.GetType("ExtraComma, ,"));
Assert.Null(Type.GetType("ExtraComma, , System.Runtime"));
Assert.Throws<FileLoadException>(() => Type.GetType("System.Object, System.Runtime, Version=x.y"));

// Backward compat: no matter what throwOnError is set to, CLR throws FileLoadException for invalid assembly names.
Assert.Throws<FileLoadException>(() => Type.GetType("System.Object, System.Runtime, Version=x.y", throwOnError: false));
Assert.Throws<FileLoadException>(() => Type.GetType("System.Object, System.Runtime, Version=x.y", throwOnError: true));

// Backward compat: when using Assembly.GetType, throwOnError is not ignored and ArgumentException can be thrown.
Assembly coreLib = typeof(int).Assembly;
Assert.Null(coreLib.GetType("System.Object, System.Runtime, Version=x.y", throwOnError: false));
Assert.Throws<ArgumentException>(() => coreLib.GetType("System.Object, System.Runtime, Version=x.y", throwOnError: true));
}

[ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsTypeEquivalenceSupported))]
Expand Down
Loading