如何使用反射确定一个属性是否实现了IList接口,如何确定元素量为空的集合的元素类型。如有个属性public List<int> Test{get{return new List<int>;}}
在使用反射的GetType().GetPropety()时如何确定Test实现了IList接口如果Test.Count==0,如何确定其元素类型(即结果应为typeof(int)

解决方案 »

  1.   

    尝试下用:Type typeInterface = fileType.GetInterface("System.Collections.Generic.IList", true);
    If (typeInterface==null)
      return false;
    else
      return true;用类似的代码看看
      

  2.   


    namespace ConsoleApplication1
    {
        class Program
        {
            static void Main(string[] args)
            {
                foreach (PropertyInfo pi in typeof(Program).GetProperties(BindingFlags.Instance | BindingFlags.Public))
                {
                    if (Array.IndexOf(pi.PropertyType.GetInterfaces(), typeof(IList)) > -1)//查找实现了Ilist接口的属性
                        Console.WriteLine(pi.Name);
                    if (pi.PropertyType.IsGenericType)//如果该属性是泛型
                    {
                        foreach (Type t in pi.PropertyType.GetGenericArguments())//遍历输出属性的每个泛型参数类型
                            Console.WriteLine(t.FullName);
                    }
                }
            }        public List<int> Test { get { return new List<int>(); } }
        }
    }