如果知道要传递的参数,请Action<T>
为类型输入。例:
void LoopMethod (Action<int> code, int count) {
for (int i = 0; i < count; i++) {
code(i);
}
}
如果希望将参数传递给您的方法,请使该方法通用:
void LoopMethod<T> (Action<T> code, int count, T paramater) {
for (int i = 0; i < count; i++) {
code(paramater);
}
}
和调用者代码:
Action<string> s = Console.WriteLine;
LoopMethod(s, 10, "Hello World");
更新。您的代码应如下所示:
private void Include(IList<string> includes, Action<string> action)
{
if (includes != null)
{
foreach (var include in includes)
action(include);
}
}
public void test()
{
Action<string> dg = (s) => {
_context.Cars.Include(s);
};
this.Include(includes, dg);
}
Action
只是一个Action
还是它是Action<T>
或其他任何变体?您要多少个参数?