这正是泛型的用武之地,示例代码:
class Program { static void Main(string[] args) { var person = new Employee(); Demo<Employee>(person); Console.ReadKey(); } private static void Demo<T>(Person person) where T : Person { T value = person as T; Console.WriteLine(value.GetType().Name); } } class Person { } class Employee : Person { }
运行结果:
Employee
呃,我把这个给忘了,谢谢。