-
Notifications
You must be signed in to change notification settings - Fork 4.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixed creating generic type with abstract type when it is has a defau…
…lt constractor constrant (#101963) * Fixed creating generic type with abstract type when it is has a default constructor constraint * Fix missing ; in test. --------- Co-authored-by: Ivan Diaz <[email protected]>
- Loading branch information
1 parent
78def3e
commit 1a305e6
Showing
6 changed files
with
98 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
88 changes: 88 additions & 0 deletions
88
src/tests/reflection/DisallowAbstractConstructors/DisallowAbstractConstructors.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using System; | ||
using System.Reflection; | ||
using Xunit; | ||
|
||
public class X | ||
{ | ||
public abstract class AbstractClassWithConstructor | ||
{ | ||
public AbstractClassWithConstructor() | ||
{ | ||
} | ||
} | ||
|
||
public static T TestConstructorMethod<T>() where T : new() | ||
{ | ||
return new T(); | ||
} | ||
|
||
public interface IItemCreator | ||
{ | ||
public object CreateItem(); | ||
} | ||
|
||
public sealed class ItemCreator<T> : IItemCreator where T : new() | ||
{ | ||
public object CreateItem() | ||
{ | ||
return new T(); | ||
} | ||
} | ||
|
||
[Fact] | ||
public static int TestEntryPoint() | ||
{ | ||
var ok = true; | ||
|
||
Type type = null; | ||
try | ||
{ | ||
type = typeof(ItemCreator<>).MakeGenericType(typeof(AbstractClassWithConstructor)); | ||
} | ||
catch | ||
{ | ||
//Could check if it is the proper type of exception | ||
} | ||
if (type == null) { | ||
Console.WriteLine("Wasn't able to load type as expected"); | ||
} | ||
else | ||
{ | ||
Console.WriteLine("Was able to make type which wasn't expected"); | ||
ok = false; | ||
} | ||
|
||
MethodInfo baseMethod = typeof(X).GetMethod(nameof(TestConstructorMethod), BindingFlags.Static | BindingFlags.Public); | ||
if (baseMethod == null) | ||
{ | ||
Console.WriteLine("baseMethod was null which wasn't expected"); | ||
ok = false; | ||
} | ||
MethodInfo method = null; | ||
try | ||
{ | ||
method = baseMethod.MakeGenericMethod(typeof(AbstractClassWithConstructor)); | ||
} | ||
catch | ||
{ | ||
//Could check if it is the proper method of exception | ||
} | ||
if (method == null) | ||
{ | ||
Console.WriteLine("Wasn't able to load method as expected"); | ||
} | ||
else | ||
{ | ||
Console.WriteLine("Was able to make method which wasn't expected"); | ||
ok = false; | ||
} | ||
|
||
Console.WriteLine(ok ? "PASS" : "FAIL"); | ||
return ok ? 100 : -1; | ||
} | ||
} | ||
|
||
|
5 changes: 5 additions & 0 deletions
5
src/tests/reflection/DisallowAbstractConstructors/DisallowAbstractConstructors.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
<ItemGroup> | ||
<Compile Include="DisallowAbstractConstructors.cs" /> | ||
</ItemGroup> | ||
</Project> |