'Class'不包含'Method'的定义

时间:2021-07-16 16:54:41

In class Employee I've got some methods, which work fine. Now I wanted to add new method, for example

在Employee类中,我有一些方法,工作正常。现在我想添加新方法,例如

public void ExampleMethod()
{         
}

Then I call it

然后我称之为

Employee employee = new Employee();
employee.ExampleMethod();

ExampleMethod is present in IntelliSense displayed after dot, when I hit F12 it also redirect me to that method, but while starting project I've got an error: 'Employee' does not contain a definition for 'ExampleMethod' and no extension method 'ExampleMethod' accepting a first argument of type 'Employee' could be found (are you missing a using directive or an assembly reference?)

ExampleMethod出现在dot之后显示的IntelliSense中,当我点击F12时它也会将我重定向到该方法,但是在启动项目时我遇到了一个错误:'Employee'不包含'ExampleMethod'的定义而且没有扩展方法'ExampleMethod '可以找到'Employee'类型的第一个参数(你是否缺少using指令或汇编引用?)

I don't know why this error is caused, because all other earlier methods work fine. What's the problem?

我不知道为什么会导致这个错误,因为所有其他早期方法都可以正常工作。有什么问题?

8 个解决方案

#1


20  

There are three possibilities:

有三种可能性:

1) If you are referring old DLL then it cant be used. So you have refer new DLL

1)如果您指的是旧DLL,则无法使用它。所以你已经引用了新的DLL

2) If you are using it in different namespace and trying to use the other namespace's dll then it wont refer this method.

2)如果您在不同的命名空间中使用它并尝试使用其他命名空间的dll,那么它将不会引用此方法。

3) You may need to rebuild the project

3)您可能需要重建项目

I think third option might be the cause for you. Please post more information in order to understand exact problem of yours.

我认为第三种选择可能是你的原因。请发布更多信息,以便了解您的确切问题。

#2


5  

Create class with namespace name might resovle your issue

使用命名空间名称创建类可能会解决您的问题

namespace.Employee employee = new namespace.Employee(); 
employee.ExampleMethod();

#3


2  

I had the same problem. Turns out the project I was referencing did not get build. When I went to the build configuration manager in visual studio and enabled the reference project , the issue got resolved.

我有同样的问题。原来我引用的项目没有得到构建。当我去Visual Studio中的构建配置管理器并启用参考项目时,问题得到了解决。

#4


1  

I just ran into this problem; the issue seems different from the other answers posted here, so I'll mention it in case it helps someone.

我刚遇到这个问题;问题似乎与此处发布的其他答案不同,所以我会提到它以防万一。

In my case, I have an internal base class defined in one assembly ("A"), an internal derived class defined in a second assembly ("B"), and a test assembly ("TEST"). I exposed internals defined in assembly "B" to "TEST" using InternalsVisibleToAttribute, but neglected to do so for assembly "A". This produced the error mentioned at top with no further indication of the problem; using InternalsVisibleToAttribute to expose assembly "A" to "TEST" resolved the issue.

在我的例子中,我有一个在一个程序集(“A”)中定义的内部基类,在第二个程序集(“B”)中定义的内部派生类,以及一个测试程序集(“TEST”)。我使用InternalsVisibleToAttribute将程序集“B”中定义的内部结构暴露给“TEST”,但忽略了对程序集“A”执行此操作。这产生了顶部提到的错误,没有进一步指出问题;使用InternalsVisibleToAttribute将程序集“A”暴露给“TEST”解决了这个问题。

#5


0  

I had the same issue when working in a solution with multiple projects that share code. Turned out that I forgot to update the DLL in the folder of the 2nd project.

在使用共享代码的多个项目的解决方案中工作时,我遇到了同样的问题。原来我忘了更新第二个项目的文件夹中的DLL。

My suggestion is to take a good like at the 'project' column in the Error list window and make sure that project also uses the right DLL.

我的建议是在错误列表窗口的“项目”列中做一个好的,并确保该项目也使用正确的DLL。

Best Regards Dominic Rooijackers

最诚挚的问候Dominic Rooijackers

#6


0  

I had the same problem. I changed the Version of Assembly in AssemblyInfo.cs in the Properties Folder. But, I don't have any idea why this problem happened. Maybe the compiler doesn't understand that this dll is newer, just changing the version of Assembly.

我有同样的问题。我更改了属性文件夹中AssemblyInfo.cs中的Assembly版本。但是,我不知道为什么会出现这个问题。也许编译器不明白这个dll是新的,只是改变了Assembly的版本。

#7


0  

Today I encountered a problem with the exact same symptoms as you describe. I closed all files and restarted VS only to find out that some files disappeared from the Solution Explorer.

今天我遇到了与你描述的症状完全相同的问题。我关闭了所有文件并重新启动了VS,但发现某些文件从解决方案资源管理器中消失了。

The following solved my problem: by selecting the current project in the Solution Explorer, a little icon Show all files appears on the top bar. Right-clicking the file and selecting Include In Project does the thing.

以下解决了我的问题:通过在解决方案资源管理器中选择当前项目,顶部栏上会出现一个小图标显示所有文件。右键单击该文件并选择Include In Project即可。

Credit

信用

#8


0  

If you are using a class from another project, the project needs to re-build and create re-the dll. Make sure "Build" is checked for that project on Build -> Configuration Manager in Visual Studio. So the reference project will re-build and update the dll.

如果您正在使用其他项目中的类,则项目需要重新构建并创建重新创建的dll。确保在Visual Studio中的Build - > Configuration Manager上检查该项目的“Build”。因此参考项目将重新构建并更新dll。

#1


20  

There are three possibilities:

有三种可能性:

1) If you are referring old DLL then it cant be used. So you have refer new DLL

1)如果您指的是旧DLL,则无法使用它。所以你已经引用了新的DLL

2) If you are using it in different namespace and trying to use the other namespace's dll then it wont refer this method.

2)如果您在不同的命名空间中使用它并尝试使用其他命名空间的dll,那么它将不会引用此方法。

3) You may need to rebuild the project

3)您可能需要重建项目

I think third option might be the cause for you. Please post more information in order to understand exact problem of yours.

我认为第三种选择可能是你的原因。请发布更多信息,以便了解您的确切问题。

#2


5  

Create class with namespace name might resovle your issue

使用命名空间名称创建类可能会解决您的问题

namespace.Employee employee = new namespace.Employee(); 
employee.ExampleMethod();

#3


2  

I had the same problem. Turns out the project I was referencing did not get build. When I went to the build configuration manager in visual studio and enabled the reference project , the issue got resolved.

我有同样的问题。原来我引用的项目没有得到构建。当我去Visual Studio中的构建配置管理器并启用参考项目时,问题得到了解决。

#4


1  

I just ran into this problem; the issue seems different from the other answers posted here, so I'll mention it in case it helps someone.

我刚遇到这个问题;问题似乎与此处发布的其他答案不同,所以我会提到它以防万一。

In my case, I have an internal base class defined in one assembly ("A"), an internal derived class defined in a second assembly ("B"), and a test assembly ("TEST"). I exposed internals defined in assembly "B" to "TEST" using InternalsVisibleToAttribute, but neglected to do so for assembly "A". This produced the error mentioned at top with no further indication of the problem; using InternalsVisibleToAttribute to expose assembly "A" to "TEST" resolved the issue.

在我的例子中,我有一个在一个程序集(“A”)中定义的内部基类,在第二个程序集(“B”)中定义的内部派生类,以及一个测试程序集(“TEST”)。我使用InternalsVisibleToAttribute将程序集“B”中定义的内部结构暴露给“TEST”,但忽略了对程序集“A”执行此操作。这产生了顶部提到的错误,没有进一步指出问题;使用InternalsVisibleToAttribute将程序集“A”暴露给“TEST”解决了这个问题。

#5


0  

I had the same issue when working in a solution with multiple projects that share code. Turned out that I forgot to update the DLL in the folder of the 2nd project.

在使用共享代码的多个项目的解决方案中工作时,我遇到了同样的问题。原来我忘了更新第二个项目的文件夹中的DLL。

My suggestion is to take a good like at the 'project' column in the Error list window and make sure that project also uses the right DLL.

我的建议是在错误列表窗口的“项目”列中做一个好的,并确保该项目也使用正确的DLL。

Best Regards Dominic Rooijackers

最诚挚的问候Dominic Rooijackers

#6


0  

I had the same problem. I changed the Version of Assembly in AssemblyInfo.cs in the Properties Folder. But, I don't have any idea why this problem happened. Maybe the compiler doesn't understand that this dll is newer, just changing the version of Assembly.

我有同样的问题。我更改了属性文件夹中AssemblyInfo.cs中的Assembly版本。但是,我不知道为什么会出现这个问题。也许编译器不明白这个dll是新的,只是改变了Assembly的版本。

#7


0  

Today I encountered a problem with the exact same symptoms as you describe. I closed all files and restarted VS only to find out that some files disappeared from the Solution Explorer.

今天我遇到了与你描述的症状完全相同的问题。我关闭了所有文件并重新启动了VS,但发现某些文件从解决方案资源管理器中消失了。

The following solved my problem: by selecting the current project in the Solution Explorer, a little icon Show all files appears on the top bar. Right-clicking the file and selecting Include In Project does the thing.

以下解决了我的问题:通过在解决方案资源管理器中选择当前项目,顶部栏上会出现一个小图标显示所有文件。右键单击该文件并选择Include In Project即可。

Credit

信用

#8


0  

If you are using a class from another project, the project needs to re-build and create re-the dll. Make sure "Build" is checked for that project on Build -> Configuration Manager in Visual Studio. So the reference project will re-build and update the dll.

如果您正在使用其他项目中的类,则项目需要重新构建并创建重新创建的dll。确保在Visual Studio中的Build - > Configuration Manager上检查该项目的“Build”。因此参考项目将重新构建并更新dll。