本文摘自PHP中文网,作者黄舟,侵删。
在实现C#导出List数据到xml文件[附源码]时遇到错误:因其保护级别而不可访问。只能处理公共类型。产生错误的代码行:
1 | XmlSerializer xmlSerializer = new XmlSerializer( typeof (List<UserModel>));
|
错误原因:以下代码中类定义为:
没有添加public访问修饰符
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 | /// <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; }
}
}
|
以上就是c#因其保护级别而不可访问,只能处理公共类型的详细代码介绍的详细内容!
相关阅读 >>
C#调用默认浏览器打开网页的几种方法具体介绍(图)
C#的四种基本数据类型详解
C#与.net框架之间的关系是什么?C#程序的开发工具
C#与vb.net混合开发测试的详细介绍(图文)
详解C#中抽象类与接口的区别
简单介绍C#类的继承的示例代码
C#中关于的for循环与foreach循环的对比
详解winform C#中子窗体关闭刷新父窗体的示例代码
C#如何使用 oledbconnection 连接读取excel?(代码实例)
详细介绍用C#描述数据结构0:导图
更多相关阅读请进入《C#》频道 >>
清华大学出版社
作者:[美]克里斯琴·内格尔(Christian Nagel)著。出版时间:2019年3月。
转载请注明出处:木庄网络博客 » c#因其保护级别而不可访问,只能处理公共类型的详细代码介绍