【问题标题】:Func<> and Action<> can't be found找不到 Func<> 和 Action<>
【发布时间】:2023-03-25 01:50:01
【问题描述】:

我有一个代表:

    delegate ResultType Caller<ResultType>(IPtxLogic logic);

这是一个函数:

private ResultType ExecuteLogicMethod2<ResultType>(string sessionId,
    Caller<ResultType> action) 
    where ResultType : IResult, new()
    {...}

我想用这样的东西替换函数签名:

private ResultType ExecuteLogicMethod2<ResultType>(string sessionId,
    Action<IPtxLogic, ResultType> action)
    where ResultType : IResult, new()
    {...}

编译器告诉我:

使用泛型类型“System.Action”需要“1”类型参数

为什么?如何访问“标准”.NET 类?

谢谢。欢迎任何想法。

附:我已经创建了自定义委托来解决问题,但是......我不想为我的每个“自定义操作”创建自定义委托......

delegate ResultType Action<ParameterType, ResultType>(ParameterType param);

附言我正在使用 .NET 3.5

【问题讨论】:

    标签: .net delegates


    【解决方案1】:

    确保引用System.Core.dllusing System.Linq;

    三思而后行。您正在使用 Action 代替 Func

    你需要Func&lt;IPtxLogic, ResultType&gt;

    【讨论】:

    • ActionFuncSystem 命名空间中。然而,OP 的错误消息表明他们已经获得了 using System;,但缺少对 System.Core.dll 的引用。
    • @Tim Robinson:不,不是那样的 :)
    【解决方案2】:

    leppie,你原来的想法是对的:

    1. 我需要将“System.Core”程序集添加到项目中!
    2. 我需要使用 Func // 但这不是问题的核心

    谢谢。

    这些类型包含在 3.5 中,开箱即用: http://msdn.microsoft.com/en-us/library/bb534960.aspx

    版本信息 .NET 框架 支持:4、3.5

    【讨论】:

      猜你喜欢
      • 2016-01-14
      • 2011-01-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-02-18
      相关资源
      最近更新 更多