簡體   English   中英

不一致的可訪問性錯誤:參數比方法更不可訪問

[英]Inconsistent accessibility error: parameter is less accessible than method

我遇到了一個不一致的問題,我正在創建一個Csharp更新器,並且用一行代碼生成此錯誤的時間很奇怪:

錯誤代碼1-不一致的可訪問性:與方法ModBoxUpdate.ModBoxUpdateInfoForm.ModBoxUpdateInfoForm(ModBoxUpdate.IModBoxUpdatable,ModBoxUpdate.ModBoxUpdateXml)相比,參數類型'ModBoxUpdate.ModBoxUpdateXml'的訪問性較低。

這是我遇到問題的代碼-ModBoxUpdateInfo.cs

using System;
using System.Windows.Forms;

namespace ModBoxUpdate
{
    public partial class ModBoxUpdateInfoForm : Form
    {
                  //This one here//
        public  ModBoxUpdateInfoForm(IModBoxUpdatable applicationInfo, 
        ModBoxUpdateXml updateInfo)
        {
            InitializeComponent();
            if (applicationInfo.ApplicationIcon != null)
                this.Icon = applicationInfo.ApplicationIcon;


        }

    }
}

ModBoxAccept.cs

using System;
using System.Windows.Forms;

namespace ModBoxUpdate
{
    internal partial class ModBoxAcceptForm : Form
    {
        private IModBoxUpdatable applicationInfo;

        private ModBoxUpdateXml updateInfo;

        private ModBoxUpdateInfoForm ModBoxUpdateInfo;
        public ModBoxAcceptForm(IModBoxUpdatable  
        applicationInfo, ModBoxUpdateXml updateInfo)
        {
            InitializeComponent();

            this.applicationInfo = applicationInfo;
            this.updateInfo = updateInfo;

            this.Text = this.applicationInfo.ApplicationName + 
            " - Update Available";


            if (this.applicationInfo.ApplicationIcon != null)
                this.Icon = this.applicationInfo.ApplicationIcon;
            this.NewVersionLabel.Text = string.Format("New Version: {0}", 
            this.updateInfo.Version.ToString());   
        }

    }
}

ModBoxUpdateXml.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;
using System.Xml;


namespace ModBoxUpdate
{
    internal class ModBoxUpdateXml
    {
        private Version version;
        private Uri uri;
        private String fileName;
        private String md5;
        private String desc;
        private String launchArgs;
        internal Version Version
        {
            get { return this.version; }
        }
        internal Uri Uri
        {
            get { return this.uri; }
        }
        internal String FileName
        {
            get { return this.fileName; }
        }
        internal String MD5
        {
            get { return this.md5; }
        }
        internal String Description
        {
            get { return this.desc; }
        }
        internal String LaunchArgs
        {

            get { return this.launchArgs; }

        }
        internal ModBoxUpdateXml(Version version, Uri uri, 
        string fileName, string md5, string desc, string launchArgs)
        {
            this.version = version;
            this.uri = uri;
            this.fileName = fileName;
            this.md5 = md5;
            this.desc = desc;
            this.launchArgs = launchArgs;
        }
        internal bool VersionCheck(Version version)
        { 
            return this.version > version;
        }

        internal static bool EOnServer (Uri location)
        {
            try{
            HttpWebRequest Rq = (HttpWebRequest)WebRequest.Create
            (location.AbsoluteUri);

                HttpWebResponse Rp = (HttpWebResponse)Rq.GetResponse();
                Rp.Close();

                return Rp.StatusCode == HttpStatusCode.OK;
            }
            catch   { return false; }
        }

        internal static ModBoxUpdateXml Parse(Uri location, String appID)
        {
            Version version = null;
            string url = "", fileName = "", md5 = "" , 
desc = "" , launchArgs = "";

            try
            {

                XmlDocument doc = new XmlDocument();
                doc.Load(location.AbsoluteUri);

                XmlNode node =doc.DocumentElement.SelectSingleNode
                ("//update[@appId='" + appID + "']");
                if (node == null)
                    return null;
                version = Version.Parse(node["version"].InnerText);
                url = node["url"].InnerText;
                fileName = node["fileName"].InnerText;
                md5 = node["md5"].InnerText;
                desc = node["desc"].InnerText;
                return new ModBoxUpdateXml(version, new Uri(url), 
                fileName, md5, desc, launchArgs);
            }
            catch{return null;}
        }
    }
}

您的ModBoxUpdateInfoForm構造函數是公共的,並且需要一個內部類型為ModBoxUpdateXml的參數。 之所以會出現此異常,是因為程序集外部的調用者無法調用公共ModBoxUpdateInfoForm構造函數,因為不允許調用者了解ModBoxUpdateXml是什么。

使ModBoxUpdateXml 公開 ,或使ModBoxUpdateInfoForm構造函數成為內部構造。

這是一個簡單的MCVE示例:

導致編譯器錯誤:

internal class A{}

public class B
{
    public B(A a){}
}

固定:

//Make this public
public class A{}

public class B
{
    public B(A a){}
}

要么:

internal class A{}

//Make this internal
internal class B
{
    public B(A a){}
}

要么:

internal class A{}

public class B
{
    //Make only the constructor internal
    internal B(A a){}
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM