我可以在 Action 或 Func 委托中使用参数吗?

发布于 2024-09-29 21:28:59 字数 213 浏览 8 评论 0原文

当我尝试在操作委托中使用参数时...

private Action<string, params object[]> WriteToLogCallBack;

我收到此设计时错误:

类、结构或接口成员声明中的标记“params”无效

任何帮助!

When I'm trying to use params in an Action delegate...

private Action<string, params object[]> WriteToLogCallBack;

I received this design time error:

Invalid token 'params' in class, struct, or interface member declaration

Any help!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

两相知 2024-10-06 21:28:59

这个解决方法怎么样?

private Action<string, object[]> writeToLogCallBack;
public void WriteToLogCallBack(string s, params object[] args)
{
  if(writeToLogCallBack!=null)
    writeToLogCallBack(s,args);
}

或者您可以定义自己的委托类型:

delegate void LogAction(string s, params object[] args);

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);
旧街凉风 2024-10-06 21:28:59

可变参数类型在 C# 中是不可能的。

这就是为什么有许多 Action<...>Func<...>Tuple<...>< 声明的原因例如,/代码>。不过,这将是一个有趣的功能。 C++0x 有它们

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.

墨洒年华 2024-10-06 21:28:59

你可以试试这个。它允许任意数量的参数,如果传递错误数量或类型的参数,您将收到编译时错误。

public delegate T ParamsAction<T>(params object[] oArgs);

public static T LogAction<T>(string s, ParamsAction<T> oCallback)
{
    Log(s);
    T result = oCallback();
    return result;
}

Foo foo = LogAction<Foo>("Hello world.", aoArgs => GetFoo(1,"",'',1.1));

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 result;
}

Foo foo = LogAction<Foo>("Hello world.", aoArgs => GetFoo(1,"",'',1.1));
缺⑴份安定 2024-10-06 21:28:59

您可以在委托的实际声明中使用 params,但不能在委托类型中使用。 Action 的通用参数只是类型,而不是调用委托时要传递的实际参数。 params 不是类型,而是关键字。

You can use params in the actual declaration of a delegate, but not in type of one. The generic parameters to an Action are only types, not the actual arguments to be passed when invoking the delegate. params is not a type, it is a keyword.

债姬 2024-10-06 21:28:59

我对 Bryan 的上述代码做了一个小的扩展,以展示如何包装多个方法调用。
我使用它来将包含数据库调用的多个方法包装到单个事务中。
谢谢布莱恩:-)
(可以在LINQPad中运行以下命令进行测试)

//Wrapper code
public delegate void MyAction(params object[] objArgs);
public static void RunActions(params MyAction[] actnArgs)
{
    Console.WriteLine("WrapperBefore: Begin transaction code\n");
    actnArgs.ToList().ForEach( actn =>  actn() );
    Console.WriteLine("\nWrapperAfter: Commit transaction code");
}

//Methods being called
public void Hash  (string s, int i, int j)  => Console.WriteLine("   Hash-method call: " + s + "###" + i.ToString() + j.ToString());
public void Slash (int i, string s)         => Console.WriteLine("   Slash-method call: " + i.ToString()+ @"////" + s);

//Actual calling code
void Main()
{  
  RunActions( objArgs => Hash("One", 2, 1)
             ,objArgs => Slash(3, "four")   );
}

//Resulting output: 
// 
//  WrapperBefore: Begin transaction code
//  
//  Hash-method call: One###21
//  Slash-method call: 3////four
//  
//  WrapperAfter: Commit transaction code  

I have done a minor extension to the above code from Bryan, to show how to wrap multiple method calls.
I am using this to wrap multiple methods that contain database calls, into a single transaction.
Thanks Bryan :-)
(You can run the following in LINQPad to test)

//Wrapper code
public delegate void MyAction(params object[] objArgs);
public static void RunActions(params MyAction[] actnArgs)
{
    Console.WriteLine("WrapperBefore: Begin transaction code\n");
    actnArgs.ToList().ForEach( actn =>  actn() );
    Console.WriteLine("\nWrapperAfter: Commit transaction code");
}

//Methods being called
public void Hash  (string s, int i, int j)  => Console.WriteLine("   Hash-method call: " + s + "###" + i.ToString() + j.ToString());
public void Slash (int i, string s)         => Console.WriteLine("   Slash-method call: " + i.ToString()+ @"////" + s);

//Actual calling code
void Main()
{  
  RunActions( objArgs => Hash("One", 2, 1)
             ,objArgs => Slash(3, "four")   );
}

//Resulting output: 
// 
//  WrapperBefore: Begin transaction code
//  
//  Hash-method call: One###21
//  Slash-method call: 3////four
//  
//  WrapperAfter: Commit transaction code  
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文