日期:2014-05-16  浏览次数:20760 次

关于方法模版参数的匹配问题
有如下的两个方法:
public static string Get_Json_Str<T>(IList<T> list);和
 public static string Get_Json_Str<T>(T obj);

现在给出的参数是List<Student>,Student为已经个定义的类。
为什么会匹配到第二个方法而不是第一个呢?第一个不应该优先匹配到吗?
另外,编写这两个方法的目的是一个处理自定义的类类型,另个处理继承自IList接口的集合类型,想要实现这个目的应该如何定义方法呢?


------解决方案--------------------
当然匹配第二个了。第一个根本就不会匹配。

T如果是List<Student>

public static string Get_Json_Str<T>(T obj);
就是
public static string Get_Json_Str<T>(List<Student> obj);

public static string Get_Json_Str<T>(IList<T> list)
就是
public static string Get_Json_Str<T>(IList<List<Student>> list);