【问题标题】:ways to call Func delegate inside Func delegate在 Func 委托中调用 Func 委托的方法
【发布时间】:2019-07-19 09:19:38
【问题描述】:

我是新的 Func 委托,在我现有的代码中有一个函数。有人可以解释如何使用 Func 委托吗?

public static ListColumn SetBColorFunction(this ListColumn column, Func<object, Func<object>, Color> colorFunction)
{
    column.BackColorFunction = colorFunction;
    return column;
}

现在我想知道调用上述函数的方法是什么?

【问题讨论】:

  • 你好,rocky,你的问题没有多大意义。你想知道如何在 C# 中调用函数吗?
  • 对不起,先生,我可能不够清楚。如您所见,函数中使用了 Func 委托。所以我只知道如果我想调用这个 SetBcolorFunction 那么我需要传递什么。
  • 谁能回答我的问题?

标签: delegates func c#-6.0


【解决方案1】:

你的方法描述:

  1. SetBColorFunction 是一个扩展方法,所以你不需要传递第一个参数。
  2. 它采用一个方法,该方法接受一个委托和一个Color 作为参数并返回一个object。委托可以采用返回 object 且不带任何参数的方法。

因此,有了这种理解,请尝试以下解决方案。

解决方案 1:

Color Callback(object o, Func<object> func)
{
    object obj = func();
    return new Color();
}

void Consumer()
{
    ListColumn listColumn = new ListColumn();
    listColumn.SetBColorFunction(Callback)
}

解决方案 2: 使用 lambda。

void Consumer()
{
    ListColumn listColumn = new ListColumn();
    listColumn.SetBColorFunction((obj, func) => {// Do your work and return object})
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-03
    • 2011-05-27
    • 1970-01-01
    • 1970-01-01
    • 2014-01-10
    相关资源
    最近更新 更多