我怎样才能收到用户?

时间:2015-09-08 04:56:11

标签: c# class object get

我们是两个班级:

头等舱是:

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

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

第二课是:

public class User
{       
    public string ID { get; set; }
    public string Name { get; set; }
    public string LName { get; set; }
    public string IsActive { get; set; }
    public string Date { get; set; }
    public string TeamID { get; set; }
    public override string ToString()
    {
        return "User";
    }       
}

我按代码使用类:

protected void btnTest_Click(object sender, EventArgs e)
{       
     DALTableIO DTIO = new DALTableIO();
     List<Team> listUser = new List<Team>();
     Team myTeam = new Team();
     myTeam.ID = "426f63a7-7f42-485f-8407-67c680f9e358";
     foreach (object item in DTIO.GetAll(myTeam))
     {
         listUser.Add((Team)item);
     }
}

我有一个名为DALTableIO类,它从数据库中获取值并将它们放入类的对象中:

public class DALTableIO:DALBase
{
    public List<object> GetAll(object MyClass)
    {
        SqlDataReader re = ExecuteReader(CommandType.StoredProcedure, string.Concat("GetAll", MyClass.ToString()), new SqlParameter[]{
            });
        List<object> list = new List<object>();
        try
        {
            while (re.Read())
            {
                Type t=MyClass.GetType();
                // creat new Class
                object item = Activator.CreateInstance(t);
                // start Fill Class
                foreach (PropertyInfo property in MyClass.GetType().GetProperties())
                {
                    //when we have property of list<object>
                    if (property.PropertyType.Name.ToLower() == "list`1")
                    {
                        //how can i create list<users> 
                        //how can i create user
                        //how can i do list<users> =getAll(user);
                        //how can i do property=list<users>;
                        continue;
                    }
                    if (property.PropertyType.Name.Substring(0, 3) == "Int")
                        item.GetType().GetProperty(property.Name).SetValue(item, int.Parse(re[property.Name].ToString()));
                    else
                        switch (property.PropertyType.Name)
                        {
                            case "String":
                                item.GetType().GetProperty(property.Name).SetValue(item, re[property.Name].ToString());
                                break;
                            case "Decimal":
                                item.GetType().GetProperty(property.Name).SetValue(item, decimal.Parse(re[property.Name].ToString()));
                                break;
                        }
                }
                list.Add(item);
            }
            if (!re.IsClosed)
                re.Close();
            re.Dispose();
            SqlConnection.ClearAllPools();
            return list;
        }
        catch (Exception err)
        {
            throw new Exception( err.Message);
        }

    }

告诉我如何填写用户list<>。我希望将一个类用户发送到getAll()以便为所有用户提供服务。感谢。

1 个答案:

答案 0 :(得分:0)

请尝试以下代码,它可能会对您有所帮助。

if (property.PropertyType.Name.ToLower() == "list`1")
{
     if (property.PropertyType.Name.ToLower() == "list`1")
     {
          var type = property.PropertyType;
          var it = type.GetGenericArguments()[0];

          var users = Activator.CreateInstance(type); // list of user
          var user = Activator.CreateInstance(it);    //user

          user.GetType().GetProperty("ID").SetValue(user, 1, null);
          user.GetType().GetProperty("Name").SetValue(user, "Name1", null);

          var add = type.GetMethod("Add");
          add.Invoke(users, new[] { user });
     }
}