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

  • Home
  • SEARCH
  • 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 7576997
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T17:04:26+00:00 2026-05-30T17:04:26+00:00

is there any possibility to have a generic List<System.Type> and to have a constraint

  • 0

is there any possibility to have a generic List<System.Type> and to have a constraint on the type?
I want to store types in a list for a lookup, but only types where the class of this type implements a specific interface.

Something like this:

List<Type> : where typeof(Type) is IMyClass

Is that possible? If not do you have any suggestion on how to solve this issue?

Any help appreciated !

EDIT:

Sorry I haven’t been clearer on the subject, but Sign’s comment below is correct, I don’t have instances available, just types.

Assume the following:

class PluginA : IPlugin { } 
class PluginB : IPlugin { } 
class PluginC : ISomeOtherInterface { } 

var pluginTypes = new List<Type>()
pluginTypes.Add(typeof(PluginA) --> OK
pluginTypes.Add(typeof(PluginB) --> OK
pluginTypes.Add(typeof(PluginC) --> should fail

Yes I could wrap this, but hoped that there would be a better variant which checks during compiletime or hints with intellisense what types are allowed.

  • 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-05-30T17:04:27+00:00Added an answer on May 30, 2026 at 5:04 pm

    if I understood you correctly, you want a list of System.Type which checks that its elements implement a certain interface. This is easy to accomplish. Just implement IList<Type> by wrapping most the List<Type> functionality and add a couple of checkups.

    public class TypeFilteredList : IList<Type>
    {
        private Type filterType;
        private List<Type> types = new List<Type>();
    
        public TypeFilteredList(Type filterType)
        {
            this.filterType = filterType;
        }
    
        private void CheckType(Type item)
        {
            if (item != null && !filterType.IsAssignableFrom(item))
                throw new ArgumentException("item");
        }
    
        public void Add(Type item)
        {
            CheckType(item);
            types.Add(item);
        }
    
        public void Insert(int index, Type item)
        {
            CheckType(item);
            types.Insert(index, item);
        }
    

    …

    }
    

    this code will work for base classes as well as interfaces.

    Example usage:

    TypeFilteredList list = new TypeFilteredList(typeof(IInterface));
    list.Add(typeof(Implementation));
    list.Add(typeof(int)); // ArgumentException
    

    If you however don’t need IList functionality, you can implement IEnumerable<Type> or ISet<Type> (wrapping HashSet<T>). List leaves an option to add the same type several times, which it seems to me is something, you don’t want.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there any possibility in the prefix-function of a given pattern to have something
Is there any way or possibility to update an apps code and have the
Is there any possibility to use old Ada code in Android? I have an
I was wondering, is there any possibility to create a table where i have
Is there any possibility to use an after update trigger only in the case
Is there any possibility in SQL to remove (only one) duplicate entries of composed
Is there any possibility to see doc variables? It's Microsoft Word 2003, I have
Is there any possibility to show only a part of an CCSprite? It seams
Is there any possibility to operate with locksreen image in windowsPhone? For example, writing
Is there any possibility that GetPropInfo returns nil even if the given class is

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.