C# 操作>>与 Func<>范围

发布于 2024-12-15 08:11:32 字数 743 浏览 1 评论 0 原文

我有以下方法,我无法找出要调用的正确语法:

public T GetAndProcessDependants<C>(Func<object> aquire, 
    Action<IEnumerable<C>, Func<C, object>> dependencyAction) {}

我试图这样调用它:

var obj = MyClass.GetAndProcessDependants<int>(() => DateTime.Now, 
    (() => someList, (id) => { return DoSomething(x); }) }

编辑: 谢谢大家,你们帮助我点亮了我的脑海。这就是我所做的:

var obj = MyClass.GetAndProcessDependants<int>(
            () => DateTime.Now,
            (list, f) => 
            {
                list = someList;
                f = id => { return DoSomething(id); };
            });

不知道为什么我对此有疑问。我想这就是那些日子之一..

谢谢

I have the following method that I can't figure out correct syntax to call:

public T GetAndProcessDependants<C>(Func<object> aquire, 
    Action<IEnumerable<C>, Func<C, object>> dependencyAction) {}

I'm trying to call it like this:

var obj = MyClass.GetAndProcessDependants<int>(() => DateTime.Now, 
    (() => someList, (id) => { return DoSomething(x); }) }

Edited:
thx everyone, you guys helped turned on a light bulb in my head. here is what i did:

var obj = MyClass.GetAndProcessDependants<int>(
            () => DateTime.Now,
            (list, f) => 
            {
                list = someList;
                f = id => { return DoSomething(id); };
            });

not sure why i even an issue with this. it's one of those days i guess..

thx

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

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

发布评论

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

评论(4

蓝色星空 2024-12-22 08:11:32

你的 lambda 语法完全错误。

您需要创建一个带有两个参数的单个 lambda 表达式:

(list, id) => DoSomething(...)

Your lambda syntax is totally wrong.

You need to create a single lambda expression with two parameters:

(list, id) => DoSomething(...)
向日葵 2024-12-22 08:11:32

现在,当该函数需要两个参数时,它只接受一个参数!

您需要接受一个列表参数,例如 (list, id) => {}

Right now the function is only accepting a single argument, when it asks for two!

You need to accept a list argument, such as (list, id) => {}

初与友歌 2024-12-22 08:11:32

看看上面的描述,看起来调用应该是:

var obj = MyClass.GetAndProcessDependants<int>(() => DateTime.Now,
    (seq, fun) => { /* do something with seq and fun */ });

关键是因为您传递的是一个需要 FuncAction ,所以调用者(最有可能)是将是将 Func 传递到您的 Action 中。因此,您只需指定如何将 Func 应用于传入的序列(如果我正确读取原型)。

Just looking at the description above, it looks like the call should be:

var obj = MyClass.GetAndProcessDependants<int>(() => DateTime.Now,
    (seq, fun) => { /* do something with seq and fun */ });

The key is since you are passing an Action that takes a Func, the caller is (most likely) going to be the one passing that Func into your Action. So you just specify how that Func is applied to the sequence passed in (if I'm reading the prototype correctly).

漆黑的白昼 2024-12-22 08:11:32
var obj = MyClass.GetAndProcessDependants<int>(
    () => DateTime.Now, 
    (someList, id) => DoSomething(x)
);
var obj = MyClass.GetAndProcessDependants<int>(
    () => DateTime.Now, 
    (someList, id) => DoSomething(x)
);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文