I want to create a List<System.Type> listOfTypesOfGlasses , where Type must implement a specific interface IGlass , so I could load it with Types of glasses.
Is there a way to enforce in compile time this constraint(must implement IGlass) ?
C# list of "Type" that implements specific interface
2.3k views Asked by Yoav R. At
        	2
        	
        There are 2 answers
0
                
                        
                            
                        
                        
                            On
                            
                            
                                                    
                    
                No - typeof(IGlass) and typeof(Glass) are both Type objects - not different classes that can be filtered in a generic constraint.  There's no generic restriction that works off of the values stored. If you want to store instances of types that all implement IGlass that's possible, but you can't filter the actual Type objects that can be stored.
Implement a method/class that hides the list.
Edit: below is the original answer here assuming that
Typemeant a placeholder, notSystem.Type.All you should need to do isList<IGlass>.or
You should write your own wrapper class around
List<T>that has the constraint.or
Subclass
List<T>and put the generic constraint on it - however this practice is discouraged.