Home
Browse all 647 examples
Download all 647 examples
Download sample chapters
Reviews
Errata
Acknowledgments
Links on .NET
Paradoxal Press
Buy directly from Paradoxal Press at $33.99 (Save 43%)
Category: Programming
Level: Beginner to seasoned
900 pages
ISBN-10 097661322-0
ISBN-13 978-097661322-0
$59.99 USA
$79.99 CANADA
|
Listing 13-41 extracted from chapter
Generics
Listing 13-40< > Listing 13-42
This listing can be compiled with the command line: csc.exe /target:exe Example_13_41.cs Errors: 0 Warnings: 0
Example_13_41.cs
using System;
using System.Reflection;
class Program{
public class Bar{}
public class Foo : Bar, IDisposable{ public void Dispose() { } }
public static void Fct<U, V>()
where U : Bar, IDisposable, new()
where V : struct {
Console.WriteLine( typeof(U).Name );
Console.WriteLine( typeof(V).Name );
}
static void Main() {
Type typeProgram = typeof(Program);
MethodInfo methodGenericOpen = typeProgram.GetMethod("Fct",
BindingFlags.Static | BindingFlags.Public);
// Specify parameter types.
MethodInfo methodGenericClosed = methodGenericOpen.MakeGenericMethod(
new Type[] { typeof(Foo), typeof(int) } );
System.Diagnostics.Debug.Assert (
methodGenericClosed.GetGenericMethodDefinition() ==
methodGenericOpen );
methodGenericClosed.Invoke (
null, // It is null because we call a static method.
new object[0]); // No parameters in.
}
}
Copyright Patrick Smacchia 2006 2007
|