2017-08-24 102 views
-3

我有一个类文件,其中包含一个函数来散列输入字符串。无法从另一个类调用静态方法

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功能,智能检测类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(); 
} 
+0

这只是一个普通的Windows C#的形式,这对密码输入,从而散列函数。 –

+1

使HashingSystem成为公共类 –

+0

如果两个类不在同一个Assembly(编译单元/ Visual Studio项目)中,则将HashingSystem公开。 –

回答

4

你需要调用static成员对class,而不是针对实例class。所以,你需要使用:

HashingSystem.sha256("texthere"); 

此外,考虑改变:

class HashingSystem 

到:

public class HashingSystem 

类是internal默认。 我建议你始终明确可见度(即始终指定internal,publicprivate)。

+0

它是公共静态类,但将其称为“HashingSystem.sha256(string)”给我一个错误;名称'sha256'在当前上下文中不存在 –

+0

请更新您的文章以包含调用代码**的整个**方法以及实际的HashingSystem类(因为您的文章中的HashingSystem类绝对是**不**公开)。还请包括您收到的编译器错误的屏幕截图。 – mjwills

+0

有一个类哈希系统。我曾试图让公开课没有工作。 –

3

你想要做这个吗?

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