2015-09-07 84 views
1

我们有两大类:我怎样才能访问用户?

第一类是:

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想要发送每个用户添加()保存。 谢谢。

+0

为什么你会采取'Team'类参数为'object',而不是'Team'类型? :( –

+0

我想向DALTableIO发送不同的类。 – shahroz

+0

当你将Team添加到数据库时,你需要ID团队来添加每个用户,这个方法在数据库中搜索并给你列表。 – shahroz

回答

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 ... 
    } 
}