如何判断对象包含List<T>中的类型

今天要做个主从表的查询,比如传入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; }
    }
}


  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值