新手泛型赋值问题!求教

「已注销」 2013-03-28 03:04:25
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Xml.Serialization;

namespace MangerFT.FS
{
public partial class WebForm4 : System.Web.UI.Page
{
public static string UCFilePath = "D:\\UserConfig.xml";

protected void Page_Load(object sender, EventArgs e)
{

}

[Serializable]
/// <summary>
/// 用户信息类
/// </summary>
public class Userconfig
{
public List<UserDetail> ListUser;
}


[Serializable]
public class UserDetail
{
private string id;
/// <summary>
/// 用户Id
/// </summary>
public string Id
{
get
{
return id;
}
set
{
id = value;
}
}

private string name;
/// <summary>
/// 用户名
/// </summary>
public string Name
{
get
{
return name;
}
set
{
name = value;
}

}

private string role;
/// <summary>
/// 系统角色
/// </summary>
public string Role
{
get
{
return role;
}
set
{
role = value;
}
}

private bool permission;
/// <summary>
/// 扫描枪使用权限
/// </summary>
public bool Permission
{
get
{
return permission;
}
set
{
permission = value;
}
}

private bool unLock;
/// <summary>
/// 解锁权限
/// </summary>
public bool UnLock
{
get
{
return unLock;
}
set
{
unLock = value;
}
}
}

void Serialize()
{
Userconfig a = new Userconfig();
UserDetail b = new UserDetail();

a.ListUser.Add(b);//这里报错,未将对象引用到实例的对象。。。

a.ListUser[0].Id = "000000";
a.ListUser[0].Name = "admin";
a.ListUser[0].Permission = true;
a.ListUser[0].Role = "admin";
a.ListUser[0].UnLock = true;

a.ListUser.Add(b);

a.ListUser[1].Id = "000001";
a.ListUser[1].Name = "admin1";
a.ListUser[1].Permission = true;
a.ListUser[1].Role = "admin1";
a.ListUser[1].UnLock = true;

XmlSerializer xl = new XmlSerializer(typeof(Userconfig));
xl.Serialize(File.Create(UCFilePath),a);
}

protected void Button1_Click(object sender, EventArgs e)
{
Serialize();
}
}
}

求教
...全文
104 点赞 收藏 2
写回复
2 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
iceMung 2013-03-28
楼上正解 List<T>在被使用之前需要进行初始化操作。
public class Userconfig
        {
            public Userconfig()
            {
                ListUser = new List<UserDetail>();
            }
            public List<UserDetail> ListUser { get; set; }
        }
回复
jinning13 2013-03-28
public class Userconfig { public List<UserDetail> ListUser= new List<UserDetail>(); }
回复
相关推荐
发帖
C#
创建于2007-09-28

10.6w+

社区成员

.NET技术 C#
申请成为版主
帖子事件
创建了帖子
2013-03-28 03:04
社区公告

让您成为最强悍的C#开发者