简体   繁体   English

CLR 库编译但 C# 调用返回错误 CS0103,即使存在已编译的元数据

[英]CLR library compiles but C# call returns error CS0103 even with existing compiled metadata

I have a Visual Studio Solution with a Class Library (C#) and a CLR Class Library (C++).我有一个带有类库 (C#) 和 CLR 类库 (C++) 的 Visual Studio 解决方案。 The CLR class library builds properly and it as simple as, for the header file: CLR 类库正确构建,它就像头文件一样简单:

public ref class CLRClass
{
public:
    static void CLRMethod();
};

The cpp implements just an empty CLRMethod . cpp 只实现了一个空的CLRMethod In the C# library I added a reference to the CLR project and I am just calling the CLR method:在 C# 库中,我添加了对 CLR 项目的引用,我只是调用了 CLR 方法:

CLRClass.CLRMethod();

The C# library does not build, returns error: C# 库未构建,返回错误:

error CS0103: The name 'CLRClass' does not exist in the current context错误 CS0103:当前上下文中不存在名称“CLRClass”

But surprisingly if I F12 on the CLRClass in Visual Studio it does bring me to the correct metadata of the compile file in the dll, showing that the CLR library is correctly compiled and accessible:但令人惊讶的是,如果我在 Visual Studio 中的CLRClass上按 F12,它确实将我带到了 dll 中编译文件的正确元数据,表明 CLR 库已正确编译并可访问:

public class CLRClass
{
  public CLRClass();

  public static void CLRMethod();
}

I don't understand how the metadata is accessible but when compiling the C# library I get the CR0103 error, any suggestions on how to fix this?我不明白元数据是如何访问的,但是在编译 C# 库时出现 CR0103 错误,关于如何解决这个问题有什么建议吗?

It's a problem of C# .Net Target Framework Version.这是 C# .Net 目标框架版本的问题。 Notice that you have this warning in your reference:请注意,您的参考中有此警告: 包含 CLRClass 的 CLRLibrary 中的警告

This is because the C# Class Library targets a Framework version that is older (4.7.1) than the one that the C++/CLI framework (4.8).这是因为 C# 类库的目标框架版本 (4.7.1) 比 C++/CLI 框架 (4.8) 的框架版本旧。 Just retarget your C++ library.只需重新定位您的 C++ 库。

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

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