Skip to content

Type.FullName contains Type.AssemblyQualifiedName for generic arguments #121994

@mdzieg

Description

@mdzieg

Description

For a type, FullName returns FQAN for generic type arguments. It is inconsistent with what FullName returns for non-generic types.

Reproduction Steps

using System;
					
public class Program
{
	public static void Main()
	{
		Console.WriteLine(typeof(int).FullName);
		Console.WriteLine(typeof(int?).FullName);
	}
}

Expected behavior

System.Int32
System.Nullable`1[[System.Int32]]

Actual behavior

System.Int32
System.Nullable`1[[System.Int32, System.Private.CoreLib, Version=8.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]]

Regression?

No response

Known Workarounds

This method returns the full type name with the assembly name.

	public static string GetStringRepresentation(this Type type)
	{
		if (type.IsGenericType)
		{
			var genericTypeDef = type.GetGenericTypeDefinition();
			var args = type.GetGenericArguments();

			var simplifiedArgs = string.Join(", ", args.Select(arg => $"[{arg.GetStringRepresentation()}]"));

			return $"{genericTypeDef.FullName}[{simplifiedArgs}], {type.Assembly.GetName().Name}";
		}

		return $"{type.FullName}, {type.Assembly.GetName().Name}";
	}

The following code:

		Console.WriteLine(typeof(int).GetStringRepresentation());
		Console.WriteLine(typeof(int?).GetStringRepresentation());

Results in:

System.Int32, System.Private.CoreLib
System.Nullable`1[[System.Int32, System.Private.CoreLib]], System.Private.CoreLib

Configuration

No response

Other information

Ideally there could be a property that returns FQAN, but without version and token. So this information could be serialized and used during deserialization even when the version of the assembly changes.

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-System.ReflectionquestionAnswer questions and provide assistance, not an issue with source code or documentation.

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions