static void Main(string[] args) {
TestClass cls = new TestClass(); cls.GetType("123");
Student stu = new 泛型方法.Student(); stu.Name = "王五"; stu.Age = 23;
//只有在使用才能确定类型 cls.GetParam(stu);
Console.ReadKey();
}
public class TestClass {
/// /// object是任何类型的父类,都可以用子类的元素代替,实质进行类型的转换消耗了性能 /// /// public void GetType(object tParam) { // tParam参数只有四个方法调用, Console.WriteLine("类型为:"+tParam.GetType()); } /// /// 泛型方法,T参数类型只有在使用时候才能确定类型,泛型方法进行约束,则T类型必须是People或其子类,否则程序会报错 /// /// /// public void ShowGeneric(T tParam) where T :People { Console.WriteLine("类型为:" + tParam.GetType() + " ,姓名为:" + tParam.Name); //SayHi()是People父类的方法 tParam.SayHi(); } /// /// 泛型方法没有约束的 /// /// /// public void GetParam(T tParam) { // is 用于判断类型 if (tParam is Student) { Student stu = tParam as Student; Console.WriteLine("类型为:" + tParam.GetType() + "其他的" + stu.Name); } else Console.WriteLine("类型为:" + tParam.GetType()); }}
转载于:https://www.cnblogs.com/yang12311/p/10500106.html