简体   繁体   English

序列化时的保护级别问题

[英]Issue with protection level when serializing

I am doing a little program just to further learn xml serialization in wich i save id,name,age that belong to an object named Person. 我正在做一个小程序,只是为了进一步学习xml序列化,直到我保存属于名为Person的对象的id,name,age。 But somehow it throws an exception ( xmlTeste.Person is inaccessible due to its protection level. Only public types can be processed.). 但是以某种方式它会引发一个异常(由于xmlTeste.Person的保护级别,它是不可访问的。只能处理公共类型。)。 How can i improve my code? 如何改善我的代码? The expected result is an xml file being created with the object Person. 预期结果是使用对象Person创建一个xml文件。

The object Person: 对象Person:

    class Person
{
        #region Variables

    private int id = 0;
    private string name = string.Empty;
    private int idade = 0; //it's age in portuguese

    #endregion

    #region Properties

    public int Id
    {
        get { return id; }
        set { id = value; }
    }

    public string Name
    {
        get { return name; }
        set { name = value; }
    }

    public int Idade //again... means age
    {
        get { return idade; }
        set { idade = value; }
    }

    #endregion
 }

My class to manage xml serialization 我的班来管理xml序列化

    class XMLController
{
    private static void SerializeAndSaveObject(XmlSerializer writer, Person item)
    {
        var path = "C://Folder//teste.xml";
        FileStream file = File.Create(path);

        writer.Serialize(file, item);
        file.Close();
    }

    public static void SaveFile(Person person)
    {
        SerializeAndSaveObject(new XmlSerializer(typeof(Person)), pessoa);//here is where i am having the error
       //An unhandled exception of type 'System.InvalidOperationException' occurred in System.Xml.dll
      //Additional information: xmlTeste.Pessoa is inaccessible due to its protection level. Only public types can be processed.
    }

}

Usage: 用法:

        private void btnGo_Click(object sender, EventArgs e)
    {

        Person p = new Person
        {
            Id = 2,
            Name = "DEFEF",
            Idade = 2 //means age
        };
        xmlTeste.XMLController.SaveFile(p);


    }

Person is an internal class. Person是内部阶级。 That's the "protection level" the exception is talking about. 这就是例外所指的“保护级别”。 In C#, internal is the default, if you don't specify a protection level explicitly. 如果您未明确指定保护级别,则在C#中, internal是默认设置。

Only public types can be processed 只能处理公共类型

If it can only process public types, and you want it to process your type, try making your type public. 如果它只能处理公共类型,并且您希望它处理您的类型,请尝试将您的类型公开。 The serialization code can't do anything with your class because the serialization code doesn't have access to your class -- internal means nobody outside of its own assembly has access to it. 序列化代码无法对您的类进行任何操作,因为序列化代码无权访问您的类-内部意味着您自己的程序集之外的任何人都无法访问它。

Define your class like so: 像这样定义您的班级:

public class Person {
...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM