C# “类”不包含“方法”的定义

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12475142/
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-08-09 23:22:58  来源:igfitidea点击:

'Class' does not contain a definition for 'Method'

c#asp.netmethods

提问by vanilla161

In class EmployeeI'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 存在于点后显示的 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?

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

采纳答案by VIRA

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.

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

回答by Pranay Rana

Create class with namespace name might resovle your issue

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

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

回答by Serge

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 中的构建配置管理器并启用参考项目时,问题得到了解决。

回答by Dominic RooiHymaners

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 look at the 'project' column in the Error list window and make sure that project also uses the right DLL.

我的建议是仔细查看错误列表窗口中的“项目”列,并确保该项目也使用正确的 DLL。

回答by Renato Carvalho

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

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

回答by Tilwaen

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 disappearedfrom the Solution Explorer.

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

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

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

Credit

信用

回答by ket

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 InternalsVisibleToAttributeto expose assembly "A" to "TEST" resolved the issue.

就我而言,我在一个程序集(“A”)中定义了一个内部基类,在第二个程序集(“B”)中定义了一个内部派生类,以及一个测试程序集(“TEST”)。我使用 将程序集“B”中定义的内部组件暴露给“TEST” InternalsVisibleToAttribute,但忽略了对程序集“A”这样做。这产生了上面提到的错误,没有进一步说明问题;使用InternalsVisibleToAttribute将程序集“A”暴露给“TEST”解决了该问题。

回答by Heshan

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 Managerin Visual Studio. So the reference project will re-build and update the dll.

如果您使用的是来自另一个项目的类,则该项目需要重新构建并重新创建 dll。确保在Visual Studio 中的Build -> Configuration Manager上为该项目选中了“Build” 。所以参考项目会重新构建和更新dll。

回答by Priyanka Yemul

I had the same problem, "Rebuild Solution" and then "Clean Solution" didn't work.I solved that by checking my DLL references.

我遇到了同样的问题,“重建解决方案”和“清洁解决方案”不起作用。我通过检查我的 DLL 引用解决了这个问题。