2015-09-07 86 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 ... 
    } 
}