C# 公共类 - “由于其保护级别而无法访问。只能处理公共类型。”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11277605/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 17:14:24  来源:igfitidea点击:

Public Class - "is inaccessible due to its protection level. Only public types can be processed."

c#winformsclass-visibility

提问by Thad Blankenship

I am doing a test project to learn about XML serialization of an object, and I am getting an odd runtime error:

我正在做一个测试项目来了解对象的 XML 序列化,但我收到了一个奇怪的运行时错误:

namespace SerializeTest
{

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {

    }



    private void serializeConnection(Conn connection)
    {
        XmlSerializer serializer = new XmlSerializer(typeof(Conn));
        TextWriter textWriter = new StreamWriter(@"serialized.xml");
        serializer.Serialize(textWriter, connection);
        textWriter.Close();
    }

    static List<Conn> deserializeConnection()
    {
        XmlSerializer deserializer = new XmlSerializer(typeof(List<Conn>));
        TextReader textReader = new StreamReader(@"serialized.xml");
        List<Conn> connectionList;
        connectionList = (List<Conn>)deserializer.Deserialize(textReader);
        textReader.Close();

        return connectionList;
    }

    private void btnSerialize_Click(object sender, EventArgs e)
    {
        Conn conn = getConnection();
        serializeConnection(conn);

    }

    private Conn getConnection()
    {
        Conn connection = new Conn();
        connection.connectionName = txtName.Text;
        connection.address = txtAddress.Text;
        connection.height = 2542;
        connection.width = 4254;
        connection.password = txtPassword.Text;
        connection.smartSizing = false;
        connection.username = txtUsername.Text;
        connection.port = 474;
        return connection;
    }

    private void btnDeserialize_Click(object sender, EventArgs e)
    {
        int count = deserializeConnection().Count;
        lblStatus.Text = "Count: " + count;
    }
}

class Conn
{
    public Conn()
    {
    }
    public string connectionName { get; set; }
    public int height { get; set; }
    public int width { get; set; }
    public string address { get; set; }
    public string username { get; set; }
    public string password { get; set; }
    public int port { get; set; }
    public bool smartSizing { get; set; }
}

}

The Class is public - I don't understand what could be causing this error. Any help would be appreciated.

课程是公开的 - 我不明白是什么导致了这个错误。任何帮助,将不胜感激。

采纳答案by Jon Skeet

The Class is public

班级是公开的

No it's not. Here's the declaration:

不,这不对。这是声明:

class Conn
{
    ...
}

You haven't specified any access modifiers, so it's defaulting to internal(assuming it's non-nested). Just because it's got a public constructor doesn't make it public. You need to make it public explicitly:

您尚未指定任何访问修饰符,因此默认为internal(假设它是非嵌套的)。仅仅因为它有一个公共构造函数并没有使它公开。您需要明确公开:

public class Conn
{
    ...
}