C# 无法从另一个调用类中的静态方法
我有一个类文件,其中包含一个散列输入字符串的函数C# 无法从另一个调用类中的静态方法,c#,winforms,C#,Winforms,我有一个类文件,其中包含一个散列输入字符串的函数 using System; using System.Security.Cryptography; using System.Linq; using System.Text; using System.Threading.Tasks; namespace XHD_Console { public class HashingSystem { public static string Sha256(string te
using System;
using System.Security.Cryptography;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace XHD_Console
{
public class HashingSystem
{
public static string Sha256(string text)
{
string hashString = string.Empty;
//code for hashing here, contains some things i'd rather not release.
return hashString;
}
}
}
我想从窗体调用sha256函数,intellisense检测类HashingSystem,但不检测函数。有什么原因吗?我已经读到它需要是静态的,这样做,但没有用。两个类都在同一名称空间中,但类hashingsystem有自己的文件hashingsystem.cs
要调用该函数,请执行以下操作:
private void submit_Click(object sender, EventArgs e){
this.EnteredPassword = HashingSystem.sha256(input_Password.Text);
this.DialogResult = DialogResult.OK;
this.Close();
}
您需要针对
类
调用静态
成员,而不是针对类的实例
。因此,您需要使用:
HashingSystem.sha256("texthere");
也可考虑改变:
class HashingSystem
致:
默认情况下,类为。我建议您始终明确可见性(即始终指定
内部
、公共
或私有
)。您是否尝试这样做
HashingSystem hs = new HashingSystem();
hs.sha256("Hello World"); //This wont work as static methods cannot be called via instances
用下面的方法代替
HashingSystem.sha256("Hello world");//Calling directly via class
这只是一个普通的windows C#表单,用于输入密码,因此,hash function.make HashingSystem是一个公共类如果它们真的在同一个名称空间和项目中,那么它们是否内部正确并不重要。OP最有可能创建HashingSystem的实例,而不是静态调用它的公共静态类的可能副本,而是像
HashingSystem.sha256那样调用它(string)
给了我一个错误;当前上下文中不存在名称“sha256”。请更新您的帖子,以包含调用代码的整个方法,以及实际的HashingSystem
类(因为您帖子中的HashingSystem
类肯定不是公开的)。另外,请附上您收到的编译器错误的屏幕截图。有一个类哈希系统。我尝试将该类公开,但没有成功。
HashingSystem.sha256("Hello world");//Calling directly via class