[Mono-dev] Distinction between generic type and instance type

Jonathan Pryor jonpryor at vt.edu
Wed Feb 6 15:49:28 UTC 2013


On Feb 6, 2013, at 10:28 AM, Stuart Golodetz <stuart at semmle.com> wrote:
> do you know whether the distinction being made by Mono between a generic type and the instance type corresponding to it is only of relevance to the compiler, or does it have real implications for an application-level developer?

It would be helpful if you used existing/"normal" terminology; I _think_ your'e talking about "open generic types" (e.g. List<T>) vs. "closed generic types (e.g. List<int>).

Assuming that's the case, then yes, this does have implications for app developers. Suppose you wanted to know if a type implemented IList<T>, but you didn't care about the T?

	// Does FooCollection implement IList<T>?
	class FooCollection : Collection<Foo> {
	}

Here, you do need to care about the difference between open and closed generic types:

	csharp> typeof(FooCollection).BaseType.GetInterfaces().Any(i => i == typeof(IList<Foo>));
	true

The above requires that we know the closed generic type IList<Foo>. What if we don't know/care about Foo specifically, but do care about IList<T>?

	csharp> typeof(FooCollection).BaseType.GetInterfaces().Any(i => i == typeof(IList<>));
	false

Oops.

So now the developer needs to know & care, and behave accordingly:

	csharp> typeof(FooCollection).BaseType.GetInterfaces().Any(i => i.IsGenericType && i.GetGenericTypeDefinition() == typeof(IList<>));
	true

 - Jon



More information about the Mono-devel-list mailing list