【问题标题】:Contructor is inaccessible due to its protection level构造函数由于其保护级别而无法访问
【发布时间】:2013-06-04 21:25:46
【问题描述】:

我的错误是:

错误 1 ​​'aCI.CheckTexture.CheckTexture()' 由于其原因无法访问 防护等级

我使用此代码检查一些文件 MD5/Hash :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;

namespace aCI
{
class CheckTexture
{   
    Thread Search;
    protected CheckTexture()
    {
        Search = new Thread(Scan);
        Search.Start();
    }

    protected void Scan()
    {
        if (GetMD5Hash("1.rar") != "9647997C556C5A37A63EFAFBCA4A40D0"
           || GetMD5Hash("2.rar") != "6626959A9099B4C6F5C755E0D2E57EF8"
           || GetMD5Hash("3.rar") != "4D6611A825F81024E0153E2753B8A27E")
        {
            System.Windows.Forms.MessageBox.Show(
            "Sorry come back and restor your orginal files.",
            "Error",
            System.Windows.Forms.MessageBoxButtons.OK,
            System.Windows.Forms.MessageBoxIcon.Error);
            return;
        }
    }

    #region Hash Calculator

    private static byte[] ConvertStringToByteArray(string data)
    {
        return (new System.Text.UnicodeEncoding()).GetBytes(data);
    }

    private static System.IO.FileStream GetFileStream(string pathName)
    {
        return (new System.IO.FileStream(pathName, System.IO.FileMode.Open,
             System.IO.FileAccess.Read, System.IO.FileShare.ReadWrite));
    }

    public static string GetSHA1Hash(string pathName)
    {
        string strResult = "";
        string strHashData = "";

        byte[] arrbytHashValue;
        System.IO.FileStream oFileStream = null;

        System.Security.Cryptography.SHA1CryptoServiceProvider oSHA1Hasher =
              new System.Security.Cryptography.SHA1CryptoServiceProvider();

        try
        {
            oFileStream = GetFileStream(pathName);
            arrbytHashValue = oSHA1Hasher.ComputeHash(oFileStream);
            oFileStream.Close();

            strHashData = System.BitConverter.ToString(arrbytHashValue);
            strHashData = strHashData.Replace("-", "");
            strResult = strHashData;
        }
        catch (System.Exception ex)
        {
            System.Windows.Forms.MessageBox.Show(ex.Message, "Error!",
              System.Windows.Forms.MessageBoxButtons.OK,
              System.Windows.Forms.MessageBoxIcon.Error,
              System.Windows.Forms.MessageBoxDefaultButton.Button1);
        }

        return (strResult);
    }

    public static string GetMD5Hash(string pathName)
    {
        string strResult = "";
        string strHashData = "";

        byte[] arrbytHashValue;
        System.IO.FileStream oFileStream = null;

        System.Security.Cryptography.MD5CryptoServiceProvider oMD5Hasher =
              new System.Security.Cryptography.MD5CryptoServiceProvider();

        try
        {
            oFileStream = GetFileStream(pathName);
            arrbytHashValue = oMD5Hasher.ComputeHash(oFileStream);
            oFileStream.Close();

            strHashData = System.BitConverter.ToString(arrbytHashValue);
            strHashData = strHashData.Replace("-", "");
            strResult = strHashData;
        }
        catch (System.Exception ex)
        {
            System.Windows.Forms.MessageBox.Show(ex.Message, "Error!",
           System.Windows.Forms.MessageBoxButtons.OK,
           System.Windows.Forms.MessageBoxIcon.Error,
           System.Windows.Forms.MessageBoxDefaultButton.Button1);
        }

        return (strResult);
    }
    #endregion
  }

}

然后我在这里尝试使用上述代码中的类 CheckTexture:

 private void BtnMpLanClick(object sender, RoutedEventArgs e)
    {
        if (!File.Exists("chrome.exe"))
        {
            MessageBox.Show("Cannot find chrome.exe");
            return;
        }

        else
        {
            //Process.Start("chrome.exe");
            this.StartTheProcess("chrome.exe", "");

            Thread.Sleep(10);

            try
            {
                // I have error on this line:

                CheckTexture Scan = new CheckTexture();

            }
            catch (Exception)

            { }

        }

    }

但我在这一行有这个错误:

CheckTexture Scan = new CheckTexture();

如果可能,请有人告诉我我的错误是什么。 谢谢你的帮助

【问题讨论】:

    标签: c# access-modifiers


    【解决方案1】:

    课程不公开。改成:

    public class CheckTexture
    {   
        Thread Search;
        public CheckTexture()
        {
            Search = new Thread(Scan);
            Search.Start();
        } 
    

    【讨论】:

    • 请注意,构造函数也受到保护。仅通过更改类上的访问器可能无法解决。
    • 对不起但也有同样的问题!!
    • 正如 carameiriel 所说,构造函数也需要公开。 (查看我的编辑)
    【解决方案2】:

    在 C# 中,类的默认访问修饰符是 internal

    所以你的CheckTexture 类是internal。改成publiclike;

    public class CheckTexture
    {
     ...   
    }
    

    来自Access Modifiers (C# Programming Guide)

    在命名空间中直接声明的类和结构(在 换句话说,没有嵌套在其他类或结构中)可以 要么是公开的,要么是内部的。 如果无法访问,则默认为内部 指定了修饰符。

    但这还不够。因为当你写的时候;

    CheckTexture Scan = new CheckTexture();
    

    这会调用CheckTexture 类的无参数构造函数,其访问修饰符是protected。也将其设为public

    public CheckTexture()
    {
        Search = new Thread(Scan);
        Search.Start();
    }
    

    【讨论】:

      【解决方案3】:

      默认情况下,C# 中的类是internal。标记它public。根据@Caramiriel 的注释,构造函数也需要标注public

      【讨论】:

        猜你喜欢
        • 2011-07-16
        • 2015-09-26
        • 2011-09-01
        • 1970-01-01
        • 2019-04-21
        • 1970-01-01
        • 2011-04-02
        相关资源
        最近更新 更多