Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8904939
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T02:15:51+00:00 2026-06-15T02:15:51+00:00

In C# is it possible to constrain the type parameter of a generic method

  • 0

In C# is it possible to constrain the type parameter of a generic method such that it is restricted to an unclosed type of an interface in a type safe manner?
Let me elaborate…

For example I have the following psuedo code:

    public bool Validate<TValidator>(object validatable)
        where TValidator : IValidator<>
    {
        // code that finds all implementations of IValidator that closes on 
        // recursive base types / interfaces of the validatable object
        // return true if validates
    }

I have several interfaces that implement IValidator such as ICreateValidator and IDeleteValidator. I can easily enough gather the validators I want in a reflection heavy not quite as generic as I’d like manner that close on all type T, and all base classes of T as well as interfaces implemented by T.

The desire would be able to call something along the lines of

    var validates = Validate<IDeleteValidator<>>(concreteDomainObject);

Which would provide me with the unclosed type of IDeleteValidator<> allowing me to internally find each implementations of IDeleteValidaor where T is either the concreteDomainObject type, an interface that the concreteDomainObject type implements, or recursive base types concreteDomainObject type extends all the way up to object.

Sure I could close the interface on the domain object, it just doesn’t make sense to me that I should have to call

    Type unclosedValidatorType = typeof (TValidator).GetGenericTypeDefinition()

Given that I both could not be assured that TValidator would be a generic type at compile time, and that the explicitly closed TValidator would not be used except to strip it’s closing type this seems like the wrong way of doing things to me.

The psuedo code I provided obviously function, but is there a type safe equivalent to constrian the type of generic method to an unclosed type of given interface?

edited to correct some terminology

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T02:15:52+00:00Added an answer on June 15, 2026 at 2:15 am

    In C# is it possible to constrain the type of a generic method such that it is restricted to an unclosed type of an interface in a type safe manner?

    No. Type arguments have to be closed, so it would make no sense to constrain the type parameter to type which couldn’t be expressed as type arguments.

    Forget the constraint part to start with – this simple code won’t work:

    using System;
    
    class Test
    {
        static void Main()
        {
            Foo<Action<>>();
        }
    
        static void Foo<T>()
        {
            Console.WriteLine(typeof(T));
        }
    }
    

    Errors:

    Test.cs(7,20): error CS1525: Invalid expression term '>'
    Test.cs(7,23): error CS1525: Invalid expression term ')'
    Test.cs(7,24): error CS1026: ) expected
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is it possible to constrain the type of generic to, say, two different classes?
Scala: Is it possible to constrain a type parameter to be non abstract? Are
Is it possible to have a type constraint that is an or of two
Is possible to calculate average of three encrypted integer? No constrain on the method
Is it possible to constraint a method so that it receives only int ,
Is it possible to enumerate which types that is available in a generic constraint?
It is possible in C# to add variance annotation to type parameter, constrained to
Is it possible to constrain the type of capture of a lambda given as
How can I specify the bounds of an unapplied higher order type parameter, such
Update 1 I realised that the original restriction of a simple generic type meant

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.