1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
using System; using System.Reflection; namespace test { class Program { static void Main(string[] args) { MethodInfo methodInfo = typeof(GenericsTest).GetMethod("SingTest"); MethodInfo genericsMethodInfo = methodInfo.MakeGenericMethod(typeof(FireBird)); genericsMethodInfo.Invoke(new GenericsTest(), new object[] { new FireBird() }); Console.ReadKey(); } class GenericsTest { public void SingTest(T bird) where T : Bird { Console.WriteLine("test"); bird.Sing(); } } } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
using System; using System.Text; namespace test { class FireBird { public string Name { get; set; } = "hino-tori"; public void Sing() { Console.WriteLine($"{Name} is singing."); } } } |