类型未定义 - C# 类到 VB.NET

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30187316/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 19:07:13  来源:igfitidea点击:

Type Is not defined - C# class to VB.NET

c#.netvb.netentity-framework

提问by Eon

I am sitting with a rather strange error - which I believe I followed the correct steps to solve.

我坐在一个相当奇怪的错误中 - 我相信我遵循了正确的步骤来解决。

Basically, My compiler complains about the following:

基本上,我的编译器抱怨以下内容:

Type 'InteractionsEntities' is not defined

Type 'InteractionsEntities' is not defined

Here is an explanation of all I have done. My goal is to use a class compiled in C# within my vb.net project.

这是我所做的一切的解释。我的目标是在我的 vb.net 项目中使用一个用 C# 编译的类。

Step 1: Right clicked my VB.NET project - Add > Reference

第 1 步:右键单击我的 VB.NET 项目 - 添加 > 引用

Step 2: Out of the project list, I selected my C# project containing the InteractionsEntitiesclass and pressed Ok.

第 2 步:在项目列表中,我选择了包含InteractionsEntities该类的C# 项目,然后按确定。

Step 3: I added the line of code in my vb.net module: Imports CsharpProject.CsharpNamespace

第 3 步:我在 vb.net 模块中添加了以下代码行:Imports CsharpProject.CsharpNamespace

Step 4: Within my module, I added a variable: Private context = new InteractionsEntities-- note on this point, Intellisense was able to find the class I required.

第 4 步:在我的模块中,我添加了一个变量:Private context = new InteractionsEntities--注意这一点,Intellisense 能够找到我需要的类。

Step 5: To ensure my Project can use entity framework, I used the nuget package manager to install entity framework.

第 5 步:为了确保我的项目可以使用实体框架,我使用了 nuget 包管理器来安装实体框架。

So following the steps I listed above, I have the following code:

所以按照我上面列出的步骤,我有以下代码:

Imports CsharpProject.CsharpNamespace

Module Module1
    Dim context = New InteractionsEntities()
    Sub Main(properties As String())
            Dim documents = context.Documents.Select(Function(x) x)
            For Each document In documents
                Console.WriteLine(document.Name)
            Next
    End Sub

What is strange is that Intellisense was able to detect that InteractionsEntities existed in my C# namespace. Upon Installing the entityframework nuget package, the error would disappear - and I was able to access properties within the context variable. The moment I hit "rebuild all" - the error Type 'InteractionsEntities' is not definedreturns. Hovering over the namespace (which also gets marked as erroneous now) and clicking the Error Corrections Optionsyields no Correction Suggestions.

奇怪的是,Intellisense 能够检测到 InteractionsEntities 存在于我的 C# 命名空间中。安装 entityframework nuget 包后,错误将消失 - 我能够访问上下文变量中的属性。我点击“全部重建”的那一刻 - 错误Type 'InteractionsEntities' is not defined返回。将鼠标悬停在命名空间(现在也被标记为错误)并单击Error Corrections Options不会产生更正建议。

Have I missed a step inbetween? Why is my VB.NET project complaining that the class does not exist when it does? I have tested with other classes under the c# namespace too (which has nothing to do with the entity framework) and the same effect occurs.

我错过了中间的一步吗?为什么我的 VB.NET 项目在它存在时抱怨该类不存在?我也用 c# 命名空间下的其他类进行了测试(与实体框架无关),出现相同的效果。

回答by Eon

Wow ok. I found the problem.

哇好吧。我发现了问题。

When this happens, ensure that your C# project and VB.NET project are at least compiled in the same .NET Framework version.

发生这种情况时,请确保您的 C# 项目和 VB.NET 项目至少在相同的 .NET Framework 版本中编译。

My inspection in my project was the following:

我在我的项目中的检查如下:

  1. Right-click C# Project > Properties > Application > Look at target Framework (my case it was 4.5.1)
  2. Repeat the steps for the VB.NET Project > Properties > Application > Look at target Framework (my case it was 4.5)
  3. Set the .NET Framework of the VB.NET Project to match that of the C# Project and click yes in the dialog box which pops up.
  4. Rebuild project
  1. 右键单击 C# 项目 > 属性 > 应用程序 > 查看目标框架(我的情况是 4.5.1)
  2. 对 VB.NET 项目 > 属性 > 应用程序 > 查看目标框架重复这些步骤(我的情况是 4.5)
  3. 将VB.NET项目的.NET Framework设置为与C#项目的.NET Framework匹配,在弹出的对话框中点击yes。
  4. 重建项目

This fixed my issue.

这解决了我的问题。