我怎样才能对用户进行访问
本文关键字:用户 访问 | 更新日期: 2023-09-27 18:11:24
我们分为两类:
第一类是:
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;
}
告诉我如何保存用户。我想发送每个用户添加()保存。谢谢。
Replace
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 ...
}
}