?????????????
public static User ConvertObject(User user??Person person)
{
PropertyInfo[] userPro = user.GetType().GetProperties();
PropertyInfo[] personPro = person.GetType().GetProperties();
if (userPro.Length>0&&personPro.Length>0)
{
for (int i = 0; i < userPro.Length; i++)
{
for (int j = 0; j < personPro.Length; j++)
{<br>????????????????????????????//?ж?User????????????Person??
if (userPro[i].Name == personPro[j].Name && userPro[i].PropertyType == personPro[j].PropertyType)
{
Object value=personPro[j].GetValue(person?? null);
//??Person?????????????User<br>??????????????????????????????   userPro[i].SetValue(user??value ?? null);
}
}
}
}
return user;
}
?????????????:
static void Main(string[] args)
{
Person person = new Person(1??"FlyElephant"??"????");
User user = new User();
user.Id = 20;
user = ConvertObject(user?? person);
Console.WriteLine("Id:" + user.Id + "Name:" + user.Name + "???:" + user.Group);
System.Console.Read();
}
????2.??????Winform?????????????SqlHelper????????к??????????????????????о????????????????????????????????????????δ???????ú????:
List<Person> list = new List<Person>();
SqlDataReader sdr = new SqlDataReader();
while (sdr.Read())
{
Person person = new Person();
person.Name = sdr.GetString(0);
//....????????
list.Add(person);
}
???????д????????????????д????????????????????????????????????????????
public static List<T> ConvertData<T>(SqlDataReader sdr)
{
List<T> list = new List<T>();
Type type = typeof(T);
PropertyInfo[] properties = type.GetProperties();
while (sdr.Read())
{
T model = Activator.CreateInstance<T>();
for (int i = 0; i < properties.Length; i++)
{
for (int j = 0; j < sdr.FieldCount; j++)
{
//?ж?????????????ε???????????
if (properties[i].Name == sdr.GetName(j))
{
Object value =sdr[j];
//????ε???????User?е?????
properties[i].SetValue(model?? value?? null);
}
}
}
list.Add(model);
}
return list;
}
????List<User> list = new List<User>();
????SqlDataReader sdr = cmd.ExecuteReader();
????list = ConvertData<User>(sdr);