使用参数和扩展方法的 C#
扩展方法中真的不支持 params 关键字吗?
我发现当我使用 params 关键字创建扩展方法时,我得到“X 没有重载方法需要 2 个参数”。智能感知可以识别扩展方法,甚至知道它需要一个对象数组。
这是一些示例代码:
public static DalRow EasyRetrieveSingle(this DalRow dalRow, object[] parameters)
{
Dictionary<string, object> dic = new Dictionary<string, object>();
for (int i = 0; i < parameters.Length; i += 2)
dic.Add(parameters[i].ToString(), parameters[i + 1]);
List<DalRow> list = DalRow.RetrieveByFieldValues(dalRow.Structure, null, dic).Cast<DalRow>().ToList();
if (list.Count == 0) return null;
return list[0];
}
这是一些调用它的示例代码(无济于事)
(new X()).EasyRetrieveSingle(1, 2);
Is the params keyword really not supported within extension methods?
I have found that when I create extension methods with the params keyword, that I get "No overloaded method for X takes 2 arguments". Intellisense recognizes the extension method and even knows that it needs an object array.
Here's some sample code:
public static DalRow EasyRetrieveSingle(this DalRow dalRow, object[] parameters)
{
Dictionary<string, object> dic = new Dictionary<string, object>();
for (int i = 0; i < parameters.Length; i += 2)
dic.Add(parameters[i].ToString(), parameters[i + 1]);
List<DalRow> list = DalRow.RetrieveByFieldValues(dalRow.Structure, null, dic).Cast<DalRow>().ToList();
if (list.Count == 0) return null;
return list[0];
}
Here's some sample code that calls it (to no avail)
(new X()).EasyRetrieveSingle(1, 2);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您似乎缺少
params
关键字...It looks like you're missing the
params
keyword...您在方法声明中缺少
params
关键字。这可以完美编译并运行:
You're missing the
params
keyword in your method declaration.This compiles and runs perfectly: