我如何实现这个委托?

发布于 2024-08-23 05:59:27 字数 455 浏览 8 评论 0原文

操作似乎不支持 params string[] 作为参数,所以我写了

delegate void WriteFn(string s, params string[] ls);

我有这个函数

void blah(WriteFn Write, string fmt, params string[] a)

现在我想编写一个函数,但我似乎无法弄清楚语法。类似于

{
    var sw = ...
    blah(new WriteFn(s, ls) { sw.write(s, ls); }, fmt, a);
    //not what i want to do but close enough. remember sw isnt a param in WriteFn

我该如何写这个?

Action doesnt seem to support params string[] as a param so i wrote

delegate void WriteFn(string s, params string[] ls);

i have this function

void blah(WriteFn Write, string fmt, params string[] a)

Now i would like to write an function but i cant seem to figure the syntax out. It something like

{
    var sw = ...
    blah(new WriteFn(s, ls) { sw.write(s, ls); }, fmt, a);
    //not what i want to do but close enough. remember sw isnt a param in WriteFn

How do i write this?

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

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

发布评论

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

评论(2

落叶缤纷 2024-08-30 05:59:27

你的问题不清楚。我们是否应该猜测 sw 是一个 StreamWriter ?如果是这样,看起来这会起作用:

blah((s, ls) => sw.Write(s, ls), fmt, a);

Your question is not clear. Are we suppose to guess that sw is a StreamWriter? If so, it looks like this would work:

blah((s, ls) => sw.Write(s, ls), fmt, a);
唔猫 2024-08-30 05:59:27

我认为你不能这样做,因为根据MSDN。

I think you can't do this because variable argument lists is NOT compatible with anonymous methods, according to MSDN.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文