views:

283

answers:

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);
+11  A: 

It looks like you're missing the params keyword...

public static DalRow EasyRetrieveSingle(this DalRow dalRow, params object[] parameters)
Yuliy
+6  A: 

You're missing the params keyword in your method declaration.

public static DalRow EasyRetrieveSingle(
    this DalRow dalRow, params object[] parameters)
                          ↑

This compiles and runs perfectly:

static class Extensions
{
    public static void Test(this Program program, params object[] args) { }
}

class Program
{
    static void Main()
    {
        new Program().Test(1, 5);
    }
}
dtb