Home > Backend Development > C#.Net Tutorial > c# is not accessible due to its protection level and can only handle public types Detailed code introduction

c# is not accessible due to its protection level and can only handle public types Detailed code introduction

黄舟
Release: 2017-03-11 13:33:54
Original
4124 people have browsed it

An error was encountered when implementing C# to export List data to an xml file [source code attached]: inaccessible due to its protection level. Can only handle public types.

The line of code that generated the error:

XmlSerializer xmlSerializer = new XmlSerializer(typeof(List<UserModel>));
Copy after login

Cause of error: The class in the following code is defined as:

class UserModel
Copy after login

No public access modifier is added

    /// <summary>
    /// 用户实体类
    /// 
    /// 注意:类的访问修饰符必须是:public,否则会出现错误:
    /// 因其保护级别而不可访问。只能处理公共类型。
    /// </summary>
    [Serializable]
    public class UserModel
    {
        private long id = 1;

        private string name = "name";

        private int gender = 0;

        private int age = 20;

        private string userName = "userName";

        private string password = "password";

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

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

        public int Gender
        {
            get { return gender; }
            set { gender = value; }
        }

        public int Age
        {
            get { return age; }
            set { age = value; }
        }

        public string UserName
        {
            get { return userName; }
            set { userName = value; }
        }

        public string Password
        {
            get { return password; }
            set { password = value; }
        }
    }
Copy after login

The above is the detailed content of c# is not accessible due to its protection level and can only handle public types Detailed code introduction. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template