简体   繁体   English

如何在 Visual Studio 中调用另一个 C# 库项目的方法?

[英]How to call a method from another C# library project in Visual Studio?

When I am trying to call a method of another project class file, I am getting the following error当我尝试调用另一个项目 class 文件的方法时,出现以下错误在此处输入图像描述

  • I have 2 C# library projects inside same solution file.我在同一个解决方案文件中有 2 个 C# 库项目。

  • Inside each project I have a class file.在每个项目中,我都有一个 class 文件。

  • I have added reference of one project inside other.我在另一个项目中添加了一个项目的参考。

There are NO compile time or build errors.没有编译时间或构建错误。 During runtime, it is throwing the following exception (Image attached above)在运行时,它抛出以下异常(上图)

My question is, how does one able to call a class method of one project from another method?我的问题是,如何从另一个方法调用一个项目的 class 方法?

Thanks in advance:)提前致谢:)

Is your current project class file exist in the actual directory?您当前的项目 class 文件是否存在于实际目录中? The physical file might not be on the disk itself so it has this error message.物理文件可能不在磁盘本身上,因此它有此错误消息。

Simply put if your physical file exist in your directory, and if you wanna call that method in, be sure to put the namespace at the top and import that reference from your solution explorer.简单地说,如果您的物理文件存在于您的目录中,并且如果您想在其中调用该方法,请务必将命名空间放在顶部并从您的解决方案资源管理器中导入该引用。

Also, Do remember to include your method class as public.另外,请记住将您的方法 class 列为公开。

Example Below:下面的例子:

public class GetUserDetails
{
//methods
}

First check the target framework and verify that the two C# library projects have the same framework.首先查看目标框架,验证C#这两个库工程是否有相同的框架。

Did you miss deploying or referencing any assembly (DatabaseUtil.dll) in the old version?您是否错过了在旧版本中部署或引用任何程序集 (DatabaseUtil.dll)?

Check to make sure that the referenced assembly exists in your bin folder.检查以确保引用的程序集存在于您的 bin 文件夹中。 If it does exist, check if it is 32-bit or 64-bit.如果确实存在,请检查它是 32 位还是 64 位。

Go to References in Solution Explorer in Visual Studio. Go 到 Visual Studio 中解决方案资源管理器中的引用。 Select the assembly which is getting complained. Select 收到投诉的程序集。 Set its Copy Local to true in its Properties page.在其属性页面中将其 Copy Local 设置为 true。 And I suggest you try the suggestions in this thread .我建议您尝试此线程中的建议。

If the above methods are not successful, please provide the relevant code to reproduce the problem.如果以上方法都不成功,请提供相关代码重现问题。

Daniel Zhang张勇

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

相关问题 如何在Visual Studio中(从另一个非静态方法)在导入的项目中调用非静态方法 - How to call a non-static method in an imported project (from another non-static method) in Visual Studio 如何在Visual Studio 2010中引用C#类库项目? - How to reference a C# Class Library project in Visual Studio 2010? 在Visual C#中从另一个类调用动态方法 - Call dynamic method from another class in visual c# 如何从Visual Studio C#中的不同解决方案启动另一个项目? - How to launch another project from a different solution in Visual Studio C#? 如何使用 Visual Studio C# 中其他项目的 class - How to use class from other project in visual studio C# Visual Studio中C#IOS项目中的c ++库 - c++ library in C# IOS project in Visual studio 如何在“ Visual Studio 2013”​​中创建由其他库项目组成的统一c#库项目 - How to create an unifying c# library project in “visual studio 2013” consisting of other library projects 如何在 Visual Studio 中从 C# 调用 PowerShell cmdlet - How to call PowerShell cmdlets from C# in Visual Studio 如何从另一个文件调用C#中的另一个方法 - how to call another method in C# from another file 如何在C#中的另一个方法中从同一类中调用一个方法? - How to call a method from same class in another method in C#?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM