无法在另一个类中调用静态方法

时间:2017-08-24 11:08:01

标签: 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 text)
        {
            string hashString = string.Empty;
            //code for hashing here, contains some things i'd rather not release.
            return hashString;
        }
    }
}

我想从一个表单调用sha256函数,intellisense检测类HashingSystem,但不检测函数。有原因吗?我读过它需要是静态的,这样做却无济于事。这两个类都在同一名称空间中,但是类哈希系统有自己的文件,hashingsystem.cs

调用该函数:

private void submit_Click(object sender, EventArgs e){
    this.EnteredPassword = HashingSystem.sha256(input_Password.Text);
    this.DialogResult = DialogResult.OK;
    this.Close();
}

2 个答案:

答案 0 :(得分:5)

您需要针对SELECT cm.*,c.* from `case_manager` cm join `case_type` c on cm.cas_type=c.case_id where cm.user_id=$id; 致电static成员,而不是针对class实例。所以你需要使用:

class

另外,请考虑更改:

HashingSystem.sha256("texthere");

为:

class HashingSystem

默认情况下,类为internal我建议您始终明确可见性(即始终指定public class HashingSystem internalpublic)。

答案 1 :(得分: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