dotnet / dotnet/winforms

The CodeDomSerializer does not properly deserialize a CodeTypeReference to a generic type

Open
#14,259 1 comment 0 reactions 1 assignee Claimed by @KlausLoeffelmann View on GitHub
needs-area-label untriaged
Dominant language
C#
Stars
4.9k
Forks
1.1k
Avg merge
1d 13m
Merged PRs (30d)
85

Description

### Root cause

[PR9099](https://github.com/dotnet/winforms/pull/9099)

### .NET version

8.0.23

### Did it work in .NET Framework?

Yes

### Did it work in any of the earlier releases of .NET Core or .NET 5+?

7.0.20

### Issue description

The CodeDomSerializerBase.GetTypeNameFromCodeTypeReference method returns an incorrect type name for the generic type, e.g. for the nullable int type it returns **[[System.Int32, System.Private.CoreLib, Version=8.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]]** instead of **System.Nullable`1[[System.Int32, System.Private.CoreLib, Version=8.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]]**. The subsequent call to the manager.GetType method fails to resolve the string to a type.

### Steps to reproduce

Run the following code:
```
class Program {
static void Main(string[] args) {
var serializer = new CodeDomSerializer();
var expr = new CodeTypeOfExpression(new CodeTypeReference(typeof(long?)));
var manager = new Manager();
var result = serializer.Deserialize(manager, expr);
}
}
class Manager : IDesignerSerializationManager {
//...
public object GetService(Type serviceType) {
if(serviceType == typeof(System.ComponentModel.Design.TypeDescriptionProviderService))
return null;
throw new NotImplementedException();
}

public Type GetType(string typeName) {
if(typeName == "System.Int64")
return typeof(long);
string expectedTypeName = $"System.Nullable`1[[{typeof(long).AssemblyQualifiedName}]]";
if(typeName != expectedTypeName)
throw new Exception("Type name is incorrect");
else
return typeof(long?);
}
//...
}
```

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.