示例
Sample.cs
public class Employee
{
public int Salary;
}
public class Student
{
public int Marks;
}
//a class with generic method
public class Sample
{
//generic method
public void PrintData<T>(T obj) where T : class
{
if (obj.GetType() == typeof(Student))
{
Student temp = obj as Student;
System.Console.WriteLine(temp.Marks);
}
else if (obj.GetType() == typeof(Employee))
{
Employee temp = obj as Employee;
System.Console.WriteLine(temp.Salary);
}
}
}
Program.cs
class Program
{
static void Main()
{
//create objects
Sample sample = new Sample();
Employee emp = new Employee() { Salary = 1000 };
Student stu = new Student() { Marks = 80 };
//call the generic method
sample.PrintData<Employee>(emp);
sample.PrintData<Student>(stu);
System.Console.ReadKey();
}
}