C# 反照泛型对象得到属性和值
C# 反射泛型对象得到属性和值
有一个泛型对象List<User> user=new List<User>();
我想通过反射得到user对象的值和属性名称。请高人指点一二,给出具体代码。
------解决方案--------------------
System.String Name
Int32 Age
System.String EMail
Press any key to continue . . .
有一个泛型对象List<User> user=new List<User>();
我想通过反射得到user对象的值和属性名称。请高人指点一二,给出具体代码。
------解决方案--------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class User
{
public string Name { get; set; }
public int Age { get; set; }
public string EMail { get; set; }
}
class Program
{
static void Main(string[] args)
{
object list = new List<User>();
var properties = list.GetType().GetGenericArguments()[0].GetProperties();
foreach (var item in properties)
Console.WriteLine(item);
}
}
}
System.String Name
Int32 Age
System.String EMail
Press any key to continue . . .