dotnet / dotnet/runtime

Type.FullName contains Type.AssemblyQualifiedName for generic arguments

Open
#121,994 10 comments 1 reaction 0 assignees View on GitHub
area-System.Reflection question
Dominant language
C#
Stars
18.3k
Forks
5.6k
PR merge metrics
PR metrics pending

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.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.