class a
{
   private String name;   [MyAttribute]
   public String Name{get{}set{}}   private int age;   [AnotherAttribute]
   public int Age{get{}set{}}
}
Type t = a.GetType();forea(PropertyInfo p in t.GetProperties)
{
      p.////如何获取p的attribute,并且判断是MyAttribute还是AnotherAttribute?
}

解决方案 »

  1.   

    PropertyInfo类型对象不是有Attributes这个集合吗,进行foreach来访问。
      

  2.   

    class Mypropertyinfo
    {
        public static int Main(string[] args)
        {
            Console.WriteLine("\nReflection.PropertyInfo");
      
            // Define a property.
            Myproperty Myproperty = new Myproperty();
            Console.Write("\nMyproperty.Caption = " + Myproperty.Caption);
      
            // Get the type and PropertyInfo.
            Type MyType = Type.GetType("Myproperty");
            PropertyInfo Mypropertyinfo = MyType.GetProperty("Caption");
      
            // Get and display the attributes property.
            PropertyAttributes Myattributes = Mypropertyinfo.Attributes;
          
            Console.Write("\nPropertyAttributes - " + Myattributes.ToString());
      
            return 0;
        }
    }
      

  3.   

    顺便问一下,反射怎么判断IList<>类型?PropertyType.Is...?
      

  4.   

    Attributes问题解决,搞错了命名空间