简体   繁体   English

从.NET 4项目使用.NET 3.5库时出现奇怪的警告C4691

[英]Strange warning C4691 when using .NET 3.5 library from .NET 4 project

If I have a .NET 3.5, C# project with the following class: 如果我有.NET 3.5 C#项目,则具有以下类:

namespace ClassLibrary1
{
    public class Class1
    {
        public static void Method1(Func<int> func)
        {

        }

        public static void Method2()
        {
        }
    }
}

And I attempt to use it from a .NET 4.0, C++/CLI project like so: 我尝试从.NET 4.0,C ++ / CLI项目中使用它,如下所示:

#include "stdafx.h"

using namespace System;
using namespace ClassLibrary1;

int main(array<System::String ^> ^args)
{
    Class1::Method2();
    return 0;
}

The C++/CLI project compiles with the following warning: C ++ / CLI项目使用以下警告进行编译:

warning C4691: 'System::Func' : type referenced was expected in unreferenced assembly 'System.Core', type defined in current translation unit used instead 2> This diagnostic occurred while importing type 'ClassLibrary1::Class1 ' from assembly 'ClassLibrary1, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'. 警告C4691:'System :: Func':在未引用的程序集'System.Core'中预期已引用的类型,而是在当前的转换单元中定义的类型,而不是2>在从程序集'ClassLibrary1导入类型'ClassLibrary1 :: Class1'时发生此诊断,版本= 1.0.0.0,文化=中性,PublicKeyToken =空”。

Note that a similar .NET 4.0 C# project using this library will compile without warning, so this is specific to C++/CLI. 请注意,使用此库的类似.NET 4.0 C#项目将在没有警告的情况下进行编译,因此它特定于C ++ / CLI。 Referencing System.Core from the project doesn't remove the warning. 从项目中引用System.Core不会删除警告。 The warning seems useful in general so I'm reluctant to disable it, is there anything else I can do about it? 一般而言,该警告似乎很有用,因此我不愿意禁用它,对此我还能做点什么?

This was observed in Visual Studio 2012 and 2013. 这是在Visual Studio 2012和2013中观察到的。

This is possibly a framework version mismatch as your C# project is built in .net 3.5 and you are referencing it in a c++ project that's using .net 4.0 这可能是框架版本不匹配,因为您的C#项目是在.net 3.5中构建的,并且您正在使用.net 4.0的c ++项目中引用它

My suggestion would be to assign the same framework version for both the projects, either 3.5 or 4.0 我的建议是为这两个项目分配相同的框架版本,即3.5或4.0

Here is a link that explains this warning in a bit more detail. 这是一个链接 ,可以更详细地说明此警告。

The most likely reason for this is the covariance changes for .Net 4.0. 造成这种情况的最可能原因是.Net 4.0的协方差更改。 In this case, the .Net 3.5 definition is: 在这种情况下,.Net 3.5的定义是:

public delegate TResult Func<TResult>()

While the .Net 4.0 definition is: .Net 4.0的定义是:

public delegate TResult Func<out TResult>()

Using other .Net library items (I tried List<T> ) without any changes does not produce this error. 使用其他.Net库项目(我尝试过List<T> )而不进行任何更改不会产生此错误。

I would only be concerned about this warning if I was attempting to use the delegate in a covariant manner. 如果我尝试以协变方式使用委托,则只会担心此警告。 With a Func<int> that is unlikely. 使用Func<int>不太可能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM