110,567
社区成员
发帖
与我相关
我的任务
分享
Type t = typeof(User);
PropertyInfo[] propInfos = t.GetProperties();
List<User> user = new List<User>();
foreach (User u1 in user)
{
foreach (var pi in propInfos)
{
string name = pi.Name;
object value = pi.GetValue(u1, null);
}
}
[/quote]
呵呵,那还需要
List<User> user = new List<User>();
foreach (User u1 in user)
么?[/quote]
这个多写了一步,通过反射取属性值
Type t = typeof(User);
PropertyInfo[] propInfos = t.GetProperties();
List<User> user = new List<User>();
foreach (User u1 in user)
{
foreach (var pi in propInfos)
{
string name = pi.Name;
object value = pi.GetValue(u1, null);
}
}
[/quote]
呵呵,那还需要
List<User> user = new List<User>();
foreach (User u1 in user)
么?
Type t = typeof(User);
PropertyInfo[] propInfos = t.GetProperties();
List<User> user = new List<User>();
foreach (User u1 in user)
{
foreach (var pi in propInfos)
{
string name = pi.Name;
object value = pi.GetValue(u1, null);
}
}
foreach (User u1 in user)
{
Type t = u1.GetType();
foreach (var prop in t.GetProperties())
{
string name = prop.Name;
}
}
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 . . .