c# - 公开课 - "is inaccessible due to its protection level. Only public types can be processed."

标签 c# winforms class-visibility

我正在做一个测试项目来了解对象的 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

不,不是。这是声明:

class Conn
{
    ...
}

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

public class Conn
{
    ...
}

关于c# - 公开课 - "is inaccessible due to its protection level. Only public types can be processed.",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11277605/

相关文章:

c# - 如何使用Windows IOT获取Raspberry PI 2的处理器序列号

java - 如何实现可见性受限的Java图数据结构和类?

C# Environment.GetDrives() 在管理员模式下无法正常工作

c# - Nethereum 中的 functioncall.CallAsync<string>() 返回 null

c# - 简单但令人沮丧的列表框和文本框再次搜索

WinForms多线程数据绑定(bind)场景,最佳实践?

c# - C#中如何显示当前时间和日期

f# - FS0074 : The type referenced through 'C.CRecord' is defined in an assembly that is not referenced. 您必须添加对程序集 'C' 的引用

c# - 允许 Newtonsoft 的 JsonConvert 访问内部 getter/setter

C# : Read/Write DateTime from/into XML