可访问性错误不一致:参数比方法更难访问

Mas*_*idy 0 .net c# access-modifiers

我有一个不一致的问题,我正在创建一个Csharp更新程序,我有一个非常奇怪的时间与一行代码生成此错误:

错误代码1 - 不一致的可访问性:参数类型'ModBoxUpdate.ModBoxUpdateXml'不如方法ModBoxUpdate.ModBoxUpdateInfoForm.ModBoxUpdateInfoForm(ModBoxUpdate.IModBoxUpdatable,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;


        }

    }
}
Run Code Online (Sandbox Code Playgroud)

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());   
        }

    }
}
Run Code Online (Sandbox Code Playgroud)

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;}
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

Phi*_*tle 6

你的ModBoxUpdateInfoForm构造函数是公共和需要类型的参数,ModBoxUpdateXml这是内部的.您得到此异常是因为程序集外部的调用者无法调用公共ModBoxUpdateInfoForm构造函数,因为调用者不允许知道它是什么ModBoxUpdateXml.

要么ModBoxUpdateXml 公开,要么将ModBoxUpdateInfoForm构造函数设为内部.

这是一个简单的MCVE示例:

导致编译器错误:

internal class A{}

public class B
{
    public B(A a){}
}
Run Code Online (Sandbox Code Playgroud)

固定:

//Make this public
public class A{}

public class B
{
    public B(A a){}
}
Run Code Online (Sandbox Code Playgroud)

要么:

internal class A{}

//Make this internal
internal class B
{
    public B(A a){}
}
Run Code Online (Sandbox Code Playgroud)

要么:

internal class A{}

public class B
{
    //Make only the constructor internal
    internal B(A a){}
}
Run Code Online (Sandbox Code Playgroud)

  • 在最后一个修复程序中,"make it`internal`",你可以将整个`class B`设为内部(就像你所做的那样),或者你可以保持`class B`为public并使构造函数成为内部.所以你可以给最后一个修复两个. (2认同)