Can I use params in Action or Func delegates?
How about this workaround?
private Action<string, object[]> writeToLogCallBack;
public void WriteToLogCallBack(string s, params object[] args)
{
if(writeToLogCallBack!=null)
writeToLogCallBack(s,args);
}
Or you could define your own delegate type:
delegate void LogAction(string s, params object[] args);
Variadic type parameters are not possible in C#.
That's why there're many declarations for Action<...>
, Func<...>
, and Tuple<...>
, for example. It would be an interesting feature, though. C++0x has them.
You could try this. It allows for any number of arguments, and you'll get a compile time error if you pass the wrong number or type of arguments.
public delegate T ParamsAction<T>(params object[] oArgs);
public static T LogAction<T>(string s, ParamsAction<T> oCallback)
{
Log(s);
T result = oCallback();
return T;
}
Foo foo = LogAction<Foo>("Hello world.", aoArgs => GetFoo(1,"",'',1.1));