dotnet / dotnet/linker

Deduce closed generic type usages based GetGenericTypeDefinition/MakeGenericType

Open
#3,080 6 comments 0 reactions 0 assignees View on GitHub
Dominant language
C#
Stars
392
Forks
128
Avg merge
2d 10h
Merged PRs (30d)
2

Description

How complicated would be to teach linker that `GetGenericTypeDefinition` produce generic type definition for well-known type, and that `MakeGenericType` produce well known specialized type.

```csharp
// Create closed generic of type int
var genericInt = typeof(GenericType);
var instance = Activator.CreateInstance(genericInt);
Console.WriteLine(instance.ToString());

// Create closed generic of type string
var genericString = typeof(GenericType);
instance = Activator.CreateInstance(genericString);
Console.WriteLine(instance.ToString());

// Create closed generic of type X
// works because string and X is reference type.
var genericDefinition = genericString.GetGenericTypeDefinition(); // typeof(GenericType<>);
var genericX = genericDefinition.MakeGenericType(typeof(X)); // typeof(GenericType);
instance = Activator.CreateInstance(genericX);
Console.WriteLine(instance.ToString());

// Not working, because code for GenericType is different then
// GenericType
var genericLong = genericDefinition.MakeGenericType(typeof(long));
instance = Activator.CreateInstance(genericLong);
Console.WriteLine(instance.ToString());

class X
{
public override string ToString() => "Mr X";
}

class GenericType
{
public override string ToString() => "Mr " + typeof(T).FullName;
}
```

and if complicated, why it's complicated?

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.