-
Notifications
You must be signed in to change notification settings - Fork 4.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Make Generic Type allows you to create types with abstract classes when the constrant should block it #101962
Labels
Milestone
Comments
dotnet-policy-service
bot
added
the
untriaged
New issue has not been triaged by the area owner
label
May 7, 2024
I did a test it also happens with method reflection using System.Reflection;
public abstract class W {
/// This makes the bug happen
public W()
{ }
}
public sealed class Trains<T> where T : W, new() { }
internal class Program
{
public static T Create<T>() where T : W , new()
{
return new T();
}
static void Main(string[] args)
{
var shouldNotGive = typeof(Program).GetMethod("Create", BindingFlags.Static | BindingFlags.Public).MakeGenericMethod(typeof(W));
shouldNotGive.Invoke(null, Array.Empty<object>());// Will Try and run the method and get a MissingMethodException: Cannot dynamically create an instance of type 'W'. Reason: Cannot create an abstract class.
//var e = new Trains<W>(); // This line has expected must be a none-abstract type
var e = Activator.CreateInstance(typeof(Trains<>)
.MakeGenericType(typeof(W))); // This runs add creates the invalid type anyway
Console.WriteLine(e?.ToString());
}
} |
/cc @fanyang-mono |
Tagging subscribers to this area: @dotnet/area-system-reflection |
This is type loader problem. The repro happens to use reflection, but it would repro with plain IL too. #101963 has a proposed fix. |
dotnet-policy-service
bot
added
the
in-pr
There is an active PR which will close this issue when it is merged
label
Aug 2, 2024
Since #101963 is a breaking change, we'll apply it to .NET 10. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Labels
Description
You can create types with new() constraint which should block the use of abstract classes but it allows them any way though the use of the MakeGenericType and then you can instantiate them through activator
Reproduction Steps
Expected behavior
Probably for MakeGenericType to throw an invalid type exception
Actual behavior
It lets the type get created and it can be instated with the activator without a problem
Regression?
It happens from 6 - 8 did not test any more
Known Workarounds
You could add your own type check before hand
Configuration
.net 6 - 8
Other information
MakeGenericType probably just add a is abstract check for when it is a new() constraint
The text was updated successfully, but these errors were encountered: