今天要做个主从表的查询,比如传入Order对象,得到订单和订单明细的对象来,而要实现这个第一个要解决的问题,如何判断集合List<T>的类型。为此测试如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using System.Collections;
namespace Test
{
class Program
{
static void Main(string[] args)
{
JudePropertyType<Order>();//此处只需要传入主体的对象
Console.Read();
}
private static void JudePropertyType<T>()
{
T obj = Activator.CreateInstance<T>();
PropertyInfo[] pinfo = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance);
foreach (PropertyInfo pf in pinfo)
{
if (pf.PropertyType.IsClass && pf.PropertyType.IsGenericType)
{
Type type = pf.PropertyType;
Type[] genericArgTypes = type.GetGenericArguments();
Type objType = Type.GetType(genericArgTypes[0].FullName, true);
var objs = Activator.CreateInstance(objType);
PropertyInfo[] properties = objType.GetProperties(BindingFlags.Instance | BindingFlags.Public);
foreach (PropertyInfo pi in properties)
{
Console.WriteLine(pi.Name);
}
}
}
}
}
class Order
{
public int OderID { get; set; }
public decimal Price { get; set; }
public List<OrderItem> OrderItems { get; set; }
}
class OrderItem
{
public int OderItemID { get; set; }
public string Name { get; set; }
public DateTime CreateTime { get; set; }
}
}