Christian Gudrian said:
Admittingly, one rarely uses that kind of types, but sometimes they can
come in quite handy:
Imagine a Collection class to which only instances of the class
CollectionItem or one of its descendants may be added. Furthermore the
programmer should be able to specify which CollectionItem (or
descendant) exactly he wishes to add to that Collection.
Hence the implementation of the Collection class has to check for two
things:
1. Does the item which is about to be added descend from
CollectionItem?
2. Is the type of this item equal to the desired type?
With meta types the first check would already be done at compile time.
This sounds alot like generics to me, which are coming in C# 2.0.
A generic class allows you to specify type parameters which you can use to
specify types throughout the class, for example
List<T> has the methods
Add(T item);
Remove(T item);
Insert(int index, T item);
List<string> has
Add(string item);
Remove(string item);
Insert(int index, string item);
Likewise, you could create your collection class:
public class Collection<T> where T : CollectionItem
{
Add(T item);
}
In this case, only CollectionItem and derived classes are valid types. So a
user could declare a variable typed as Collection<MyCollectionItem>, which
restricts that instance of Collection to allowing only MyCollectionItem and
derived classes.
Does that do what you want? I had taken your original question to mean being
able to specify a variable to be typed Type, but only accept a certain
subset of Type values.