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
- [Meta] Feedback on granular control over Copilot changes
- unexpected status 401 Unauthorized issue
- [MODEL] Measurable quality regression in Claude generation 5 (Fable 5 / Opus 5 / Sonnet 5): worse nonsense detection, ~2x verbosity, under-disclosed model fallb
- [CRITICAL] Subagent spawning and subagent pattern bugs trigger infinite recursion, infinite token usage, grossly inefficient token usage, and lost accumulated s
- Video stops playing after some minutes
- [MODEL] Claude verbose code comments by default — ignores instructions to stop.
Raise real-world problems, find people facing the same issue, and discuss solutions.4,000 issues0 comments
github (Software)
[Meta] Feedback on granular control over Copilot changes
unexpected status 401 Unauthorized issue
[MODEL] Measurable quality regression in Claude generation 5 (Fable 5 / Opus 5 / Sonnet 5): worse nonsense detection, ~2x verbosity, under-disclosed model fallb
[CRITICAL] Subagent spawning and subagent pattern bugs trigger infinite recursion, infinite token usage, grossly inefficient token usage, and lost accumulated s
Video stops playing after some minutes
[MODEL] Claude verbose code comments by default — ignores instructions to stop.
Scope Codex VS Code chats to the current workspace/project
Update on "Co-authored-by: Copilot" in commit messages
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
Comments (0)
No comments yet. Start the discussion.