Skip to content

Type.GetType throws when passed Program&& #98426

Closed
@mrvoorhe

Description

@mrvoorhe

Description

With net7 Type.GetType("Program&&") will return null because a ref to a ref is not a valid type. Starting with net8, this code now throws.

Reproduction Steps

Create a console application targeting net8 with the following code

internal class Program
{
    public static void Main(string[] args)
    {
        var result = Type.GetType("Program&&");
        Console.WriteLine(result == null ? "null" : result);
    }
}

Expected behavior

The output should be

null

Actual behavior

The output is

Unhandled exception. System.TypeLoadException: Could not load type 'Program&' from assembly 'TypeGetTypeWorksWithByRefModifier, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'.
   at System.RuntimeTypeHandle.MakeByRef()
   at System.RuntimeType.MakeByRefType()
   at System.Reflection.TypeNameParser.ModifierTypeName.ResolveType(TypeNameParser& parser, String containingAssemblyIfAny)
   at System.Reflection.TypeNameParser.Parse()
   at System.Reflection.TypeNameParser.GetType(String typeName, Func`2 assemblyResolver, Func`4 typeResolver, Assembly requestingAssembly, Boolean throwOnError, Boolean ignoreCase, Boolean extensibleParser)
   at System.Type.GetType(String typeName)
   at Program.Main(String[] args) in C:\UnitySrc\dev\TestGround\TypeGetTypeWorksWithByRefModifier\TypeGetTypeWorksWithByRefModifier\Program.cs:line 8

Regression?

Yes this is a regression. .NET7, .NET Framework, and mono (a version from mono github repo) all return null. Our test that hit this is not new, so suspect earlier versions of .NET also returned null.

Known Workarounds

No response

Configuration

I was using 8.0.200. Windows, x64

Other information

No response

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions