4.9. Accessing Type Info

Now that you're actively building and consuming generic types, you might have an occasion when you'll want to access the specific type information for your generic types. Here's a simple example that dumps type information for a few generic classes:

[VB code]
Public Class OneParamType(Of T)
End Class

Public Class TwoParamType(Of T, U)
End Class

Public Class TypeDumper(Of T, U, V)
    Shared Sub DumpTypeInfo()
        Console.WriteLine(GetType(T))
        Console.WriteLine(GetType(U))
       Console.WriteLine(GetType(V))
        Console.WriteLine(GetType(OneParamType(Of String)))
        Console.WriteLine(GetType(OneParamType(Of T)))
        Console.WriteLine(GetType(TwoParamType(Of U, Integer)))
Console.WriteLine(GetType(TwoParamType(Of T, V)))
    End Sub

    Public Sub ShowTypeInfo()
        TypeDumper(Of String, Integer, Double).DumpTypeInfo()
    End Sub
End Class
[C# code]
using System;

public class OneParamType<T> {}

public class TwoParamType<T, U> {}

public class TypeDumper<T, U, V> {
    public static void DumpTypeInfo() {
        Console.WriteLine(typeof(T));
        Console.WriteLine(typeof(U));
        Console.WriteLine(typeof(V));
        Console.WriteLine(typeof(OneParamType<String>));
        Console.WriteLine(typeof(OneParamType<T>));
        Console.WriteLine(typeof(TwoParamType<U, int>));
        Console.WriteLine(typeof(TwoParamType<T, V>));
    }
    public static void ShowTypeInfo() {
        TypeDumper<String, int, Double>.DumpTypeInfo();
    }
}

This example creates a TypeDumper class that accepts three type arguments and includes a DumpTypeInfo() method that displays type information ...

Get Professional .NET 2.0 Generics now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.