using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
namespace PropertyInfo的使用
{
class Program
{
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();
}
public static User ConvertObject(User user, Person person)
{
PropertyInfo[] userPro = user.GetType().GetProperties();//获取user的属性
PropertyInfo[] personPro = person.GetType().GetProperties();//获取person的属性
if (userPro.Length > 0 && personPro.Length > 0)
{
for (int i = 0; i < userPro.Length; i++)
{
for (int j = 0; j < personPro.Length; j++)
{
//判断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;
}
}
public class Person
{
public Person(int id, string name, string address)
{
this.Id = id;
this.Name = name;
this.Address = address;
}
public int Id { get; set; }
public string Name { get; set; }
public string Address { get; set; }
}
public class User
{
public int Id { get; set; }
public string Name { get; set; }
public string Group { get; set; }
}
}
c# 中属性PropertyInfo的使用案例(完整代码)
最新推荐文章于 2024-03-05 21:55:53 发布