-
Notifications
You must be signed in to change notification settings - Fork 5.4k
Open
Labels
area-System.ReflectionquestionAnswer questions and provide assistance, not an issue with source code or documentation.Answer questions and provide assistance, not an issue with source code or documentation.
Milestone
Description
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.
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
area-System.ReflectionquestionAnswer questions and provide assistance, not an issue with source code or documentation.Answer questions and provide assistance, not an issue with source code or documentation.