IssueRaiser

Type.SatisfiesGenericConstraints proposal

## Background and Motivation Today, the only way at runtime to determine if a generic type definition can successfully close based on a set of generic parameters is to call `Type.MakeGenericType` and catch any exceptions: ```csharp var genericTypeDefinition = typeof(List<>); Type closedType = null; try { closedType = genericTypeDefinition.MakeGenericType(typeof(int)); } catch (ArgumentException) { } bool canClose = closedType == null; ``` In libraries that use open generic types heavily (such as DI containers), either the logic for determining whether or not an open generic type *can* be closed is duplicated, or this exception is caught. Many times, the libraries will do both - cover the basic cases but still rely on exceptions. This is especially useful in generic constraints, where I have a collection of open generic types, and I only want to resolve the closed types that satisfy the generic constraint at runtime. ## Proposed API Expose an API to check if a given set of types can successfully satisfy the generic constraints of an open generic type: ```diff namespace System { public abstract class Type { + public virtual bool SatisfiesGenericConstraints(params Type[] typeArguments); } ``` Or can check a specific generic parameter: ```diff namespace System { public abstract class Type { + public virtual bool SatisfiesConstraints(Type parameter); } ``` ## Usage Examples Simple example: ```csharp class MyType<T> where T : cl

Posted by jbogard on Nov 30, 2018, 2:45 PM

Tags

Related issues

Raise real-world problems, find people facing the same issue, and discuss solutions.4,000 issues0 comments

github (Software)

Type.SatisfiesGenericConstraints proposal

jbogardNov 30, 2018, 2:45 PM

## Background and Motivation Today, the only way at runtime to determine if a generic type definition can successfully close based on a set of generic parameters is to call `Type.MakeGenericType` and catch any exceptions: ```csharp var genericTypeDefinition = typeof(List<>); Type closedType = null; try { closedType = genericTypeDefinition.MakeGenericType(typeof(int)); } catch (ArgumentException) { } bool canClose = closedType == null; ``` In libraries that use open generic types heavily (such as DI containers), either the logic for determining whether or not an open generic type *can* be closed is duplicated, or this exception is caught. Many times, the libraries will do both - cover the basic cases but still rely on exceptions. This is especially useful in generic constraints, where I have a collection of open generic types, and I only want to resolve the closed types that satisfy the generic constraint at runtime. ## Proposed API Expose an API to check if a given set of types can successfully satisfy the generic constraints of an open generic type: ```diff namespace System { public abstract class Type { + public virtual bool SatisfiesGenericConstraints(params Type[] typeArguments); } ``` Or can check a specific generic parameter: ```diff namespace System { public abstract class Type { + public virtual bool SatisfiesConstraints(Type parameter); } ``` ## Usage Examples Simple example: ```csharp class MyType<T> where T : cl

Comments (0)

No comments yet. Start the discussion.

Comment on this issue