c# – 公共课 – “由于其保护等级而无法访问.只能处理公共类型.“

前端之家收集整理的这篇文章主要介绍了c# – 公共课 – “由于其保护等级而无法访问.只能处理公共类型.“前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在做一个测试项目,以了解一个对象的 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
{
    ...
}

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

public class Conn
{
    ...
}
原文链接:https://www.f2er.com/csharp/93380.html

猜你在找的C#相关文章