C# 如何从另一个类文件调用方法/函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16230412/
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
How to call a method/function from another class file
提问by Dennis
I tried to make this function (and others) and put it in a separate class file in my project that's under "/Helpers/UploadFiles.cs"
我试图制作这个函数(和其他函数)并将它放在我的项目中的一个单独的类文件中,该文件位于“/Helpers/UploadFiles.cs”下
namespace Artikelhantering.Helpers
{
public class UploadFiles
{
private void EnsureDirectoriesExist(string SKU)
{
// if the directory doesn't exist - create it.
if (!System.IO.Directory.Exists("//servername/wwwroot/prodimg/" + SKU))
{
System.IO.Directory.CreateDirectory("//servername/wwwroot/prodimg/" + SKU);
}
}
}
Then in the controller I added using Artikelhantering.Helpers;, it's also added to the namespace section of the web.config file and also to global.asa.cx.
然后在我使用 Artikelhantering.Helpers添加的控制器中;,它还添加到 web.config 文件的命名空间部分以及 global.asa.cx。
Then I figured I could call it from an ActionResult in my controller like this
然后我想我可以像这样从控制器中的 ActionResult 调用它
[ChildActionOnly]
public ActionResult _EnumerateFolder(string SKU)
{
// create directory if it does not exist
EnsureDirectoriesExist(SKU);
ViewBag.SKU = SKU;
var folder = Directory.EnumerateFiles("//servername/wwwroot/prodimg/" + SKU);
return View(folder);
}
But all I get is:
但我得到的只是:
Error 2 The name 'EnsureDirectoriesExist' does not exist in the current context
错误 2 当前上下文中不存在名称“EnsureDirectoriesExist”
Tried calling it by writing it as UploadFiles.EnsureDirectoriesExist(); but that doesn't work either. How am I supposed to call these methods without having them all in the same file? I would like to organize this better.
尝试通过将其编写为 UploadFiles.EnsureDirectoriesExist() 来调用它;但这也不起作用。我应该如何调用这些方法而不将它们全部放在同一个文件中?我想更好地组织这个。
采纳答案by Thorsten Dittmar
The method is private. You can not access private members of other classes.
该方法是私有的。您不能访问其他类的私有成员。
Also some other problems here:
这里还有一些其他问题:
- The method you wrote is an instance method, so you need to have an instance of the class to call the method.
- If you want to call it using
UploadFiles.EnsureDirectoryExists(), you need to make it a class method (static). - I'm not sure whether you can create a new directory the way you try to do it. If you are trying to create the directory on the same machine that this code is running on, use local file names.
- 你写的方法是一个实例方法,所以你需要有一个类的实例来调用这个方法。
- 如果要使用 调用它
UploadFiles.EnsureDirectoryExists(),则需要将其设为类方法 (static)。 - 我不确定您是否可以按照您尝试的方式创建新目录。如果您尝试在运行此代码的同一台机器上创建目录,请使用本地文件名。
Sample code for 1):
1) 的示例代码:
UploadFiles uf = new UploadFiles();
uf.EnsureDirectoryExists();
Sample code for 2):
2) 的示例代码:
public class UploadFiles
{
public static void EnsureDirectoriesExist(string SKU)
{
// if the directory doesn't exist - create it.
if (!System.IO.Directory.Exists("//servername/wwwroot/prodimg/" + SKU))
{
System.IO.Directory.CreateDirectory("//servername/wwwroot/prodimg/" + SKU);
}
}
}
I furthermore suggest that you google for a C# tutorial that provides you with information on what classes are and how they can be used.
此外,我还建议您在 google 上搜索 C# 教程,该教程为您提供有关类是什么以及如何使用它们的信息。
回答by Hossein Narimani Rad
Mark your class as staticthen try this:
将您的类标记为静态然后试试这个:
public static class UploadFiles
{
public void EnsureDirectoriesExist(string SKU)
{
//your code
}
}
Then:
然后:
public ActionResult _EnumerateFolder(string SKU)
{
UploadFiles.EnsureDirectoriesExist(SKU);
//your code
}
回答by lexeRoy
First, change the access modifier of EnsureDirectoriesExistto public then
try to change your ActionResult _EnumerateFoldermethod with the code below:
首先,将访问修饰符更改EnsureDirectoriesExist为 public 然后尝试ActionResult _EnumerateFolder使用以下代码更改您的方法:
public ActionResult _EnumerateFolder(string SKU)
{
// create directory if it does not exist
new UploadFiles.EnsureDirectoriesExist(SKU);
ViewBag.SKU = SKU;
var folder = Directory.EnumerateFiles("//servername/wwwroot/prodimg/" + SKU);
return View(folder);
}
回答by Don Angelo Annoni
make your directory method publicand static. Then you can call it something like this
使您的目录方法public和static。然后你可以这样称呼它
Artikelhantering.Helpers::UploadFiles.EnsureDirectoriesExist(SKU);
If you can't change the signature... you can make a public wrapper method and call it the same way. If you cannot make the method static, then you should create first an instance of your class and finally call the new public wrapper method.
如果您无法更改签名……您可以创建一个公共包装方法并以相同的方式调用它。如果您不能使该方法成为静态,那么您应该首先创建您的类的一个实例,最后调用新的公共包装方法。
回答by Anatolii Gabuza
- First thing that is not correct here is a method accessibility levels. In order to invoke method from outside of the class body it should be public.
- Also, way that you are invoking this method is also incorrect. To do it in desired way you should make your class staticto avoid creating an instance of a class to invoke method.
- 这里不正确的第一件事是方法可访问性级别。为了从类主体外部调用方法,它应该是public。
- 此外,您调用此方法的方式也不正确。要以所需的方式执行此操作,您应该使类静态以避免创建类的实例来调用方法。
So:
所以:
public static class Helper
{
public static void EnsureDirectoriesExist(string SKU)
{
...
}
}

