带有方法重载的可选参数

时间:2013-06-06 16:53:18

标签: c# c#-4.0 optional-parameters

我有一个这样的方法:

public static void Fill<T> (this DropDownList control,
   Expression<Func<T, object>> value,      
   Expression<Func<T, bool>> whereClause = null,
   Expression<Func<T, object>> orderClause = null,
   string selectedvalue = null) where T : class
{}

到目前为止一直很好...... 但我需要向Where和Order子句添加List选项,所以我添加了3个新方法:

public static void Fill<T> (this DropDownList control,
   Expression<Func<T, object>> value,      
   IList<Expression<Func<T, bool>>> listWhereClause = null,
   IList<Expression<Func<T, object>>> listOrderClause = null,
   string selectedvalue = null) where T : class
{}

public static void Fill<T> (this DropDownList control,
   Expression<Func<T, object>> value,      
   IList<Expression<Func<T, bool>>> listWhereClause = null,
   Expression<Func<T, object>>> orderClause = null,
   string selectedvalue = null) where T : class
{}

public static void Fill<T> (this DropDownList control,
   Expression<Func<T, object>> value,      
   Expression<Func<T, bool>>> whereClause = null,
   IList<Expression<Func<T, object>>> listOrderClause = null,
   string selectedvalue = null) where T : class
{}

问题是我现在出现了模糊调用错误... 如何解决这个问题的最佳选择(不增加方法数)?

1 个答案:

答案 0 :(得分:5)

删除重载的默​​认值(null)。因为您使用默认的null值添加了重载,所以编译器根本不知道要使用哪个。