【问题标题】:C++/CLI use of Action<...,...> and Func<...> unsupported?不支持 C++/CLI 使用 Action<...,...> 和 Func<...>?
【发布时间】:2011-01-12 17:20:29
【问题描述】:

C++/CLI 的 System 命名空间中似乎不支持 Action 和 Func 委托。至少不适用于多个通用参数,例如:

System::Action<int, int>^ action = nullptr;
System::Func<int, int>^ func = nullptr;

两者都会导致错误,例如:

error C2977: 'System::Action' : too many generic arguments  
error C2955: 'System::Action' : use of class generic requires generic argument list 

只有单个参数 Action 有效:

System::Action<int>^ action = nullptr;

任何人都知道为什么或缺少什么来完成这项工作。我正在使用 Visual Studio 2008,该项目的目标框架为 3.5。

【问题讨论】:

  • 你有对 System.Core 的引用吗?

标签: .net generics .net-3.5 delegates c++-cli


【解决方案1】:

以下是在mscorlib中定义的-

Action
Action<T>

但以下是在 System.Core 中定义的。

Action<T1, T2>
Func<T1, T2>

您可能缺少对该程序集的引用。

【讨论】:

    【解决方案2】:

    ActionFunc 类型的位置因您使用的框架版本而异。

    在 3.5 框架中,Func 的所有定义(通用或非通用)都驻留在 System.Core.dll 中。对于具有 2 个或更多通用参数的 Action 版本也是如此。 ActionAction&lt;T1,T2&gt; 在 mscorlib 中。

    在 4.0 框架中,FuncAction 的所有定义都移至 mscorlib。 System.Core 中插入了类型转发器,它们现在指向 mscorlib。

    Silverlight 4.0 更接近于 3.5 框架解决方案。

    确保您有对适合您的解决方案的 DLL 的引用。

    【讨论】:

    • 确实如此。添加 System.Core.dll 解决了这些问题。谢谢。
    • 你,提到 System.Core 4.0 中的类型转发器。你知道自己怎么可能做到这一点吗?
    猜你喜欢
    • 2023-03-25
    • 1970-01-01
    • 1970-01-01
    • 2016-01-14
    • 1970-01-01
    • 2023-01-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多