Home
Browsez les 647 exemples
Téléchargez les 647 exemples
Téléchargez des chapitres
Achetez sur amazon.fr
Niveau: Débutant/Intermédiaire
ISBN-2-84177-339-6
50 Euros
|
Exemple 13-41 extrait du chapitre
La généricité
Exemple 13-40< > Exemple 13-42
Cet exemple peut être compilé avec la ligne de commande: csc.exe /target:exe Exemple_13_41.cs Erreurs: 0 Avertissements: 0 Remarque:
Exemple_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);
// Résoud les types paramètres.
MethodInfo methodGenericClosed =
methodGenericOpen.MakeGenericMethod (
new Type[] { typeof(Foo), typeof(int) } );
System.Diagnostics.Debug.Assert (
methodGenericClosed.GetGenericMethodDefinition() ==
methodGenericOpen );
methodGenericClosed.Invoke (
null, // null car méthode statique -> pas d'instance
new object[0]); // new object[0] car pas de paramètres
}
}
Copyright Patrick Smacchia 2006 2007
|