我该如何访问用户?

时间:2015-09-07 04:56:25

标签: c# class object save

我们是两个班级:

头等舱是:

 public class Team
{
    public Team()
    {
        UsersMyTeam = new List<User>();
    }
    public int ID { set; get; }
    public string NameTeam { set; get; }

    public List<User> UsersMyTeam { set; get; }
    public override string ToString()
    {
        return "Team";
    }
}

第二课是:

public class User
{       
    public int ID { get; set; }
    public string Name { get; set; }
    public string IsActive { get; set; }
    public int teamID { get; set; }
    public override string ToString()
    {
        return "User";
    }

}

我按代码使用类:

protected void btnTest2_Click(object sender, EventArgs e)
    {
        Team myTeam = new Team();
        for (int i = 0; i < 4; i++)
        {
            User myUser = new User();
            myUser.Name = i.ToString();
            myTeam.UsersMyTeam.Add(myUser);
        }
        myTeam.NameTeam = "asdsa";
        DALTableIO DTO = new DALTableIO();
        DTO.Save(myTeam);

    }

我有一个名为DALTableIO的类,用于在数据库中保存类入口:

public class DALTableIO
{
 public int Add(object MyClass)
    {
        bool IsHaveSubClass = false;
        SqlParameter[] parametrs;
        List<SqlParameter> listParametrs = new List<SqlParameter>();
        Type t=MyClass.GetType();
        PropertyInfo[] proppertis = t.GetProperties(); 
        foreach (PropertyInfo property in proppertis)
        {
            if (property.Name == "ID")
                continue;
            if (property.PropertyType.Name.ToLower() == "list`1")
            {                    
                IsHaveSubClass = true;
                continue;
            }
            listParametrs.Add( new SqlParameter(property.Name, property.GetValue(MyClass, null)));                
        }
        parametrs = new SqlParameter[listParametrs.Count];
        for (int i = 0; i < listParametrs.Count; i++)
        {
            parametrs[i] = listParametrs[i];
        }
        ExecuteNonQuery(CommandType.StoredProcedure,string.Concat("Add",MyClass.ToString()),parametrs);
        if (IsHaveSubClass)
        {
            List<object> _listTeam = GetByOption(MyClass);
            Type _T1=_listTeam[0].GetType();
            int _IDTeam = int.Parse(_T1.GetProperty("ID").GetValue(_listTeam[0], null).ToString());            
            foreach (PropertyInfo property in proppertis)
            {
                if (property.PropertyType.Name.ToLower() == "list`1")
                {
                    //*****How Can Access To Users to save ****
                    //users are List<object>
                    //How do I access users.
                    //How do I get access to any users
                }
            }
        }
        return 1;
    }

告诉我如何保存user.i想要将每个用户发送到Add()进行保存。 感谢。

1 个答案:

答案 0 :(得分:1)

替换

if (property.PropertyType.Name.ToLower() == "list`1")
{
    //*****How Can Access To Users to save ****
    //users are List<object>
    //How do I access users.
    //How do I get access to any users
}

if (property.PropertyType.Name.ToLower() == "list`1")
{
    IList users = property.GetValue(MyClass, null) as IList;
    // users is the required list of users of Team, now loop for each user to get their Id and save to database.
    foreach (var user in users)
    {
        //do work with user here ...
    }
}