Attribute和TypeOf 外加Linq应用

来源:互联网 发布:个人域名邮箱登录 编辑:程序博客网 时间:2024/05/01 07:24

直接进入代码主题

一、写个Teacher类和一个校长Attribute

    public class Teacher    {        public long PKID { get; set; }        public string Name { get; set; }        [Principal]        public decimal Salary { get; set; }        public int Age { get; set; }    }    [AttributeUsage(AttributeTargets.Property)]    public class PrincipalAttribute:Attribute    {        /// <summary>        /// 工资大于14000的就是boss        /// </summary>        /// <param name="salary">工资</param>        /// <returns></returns>        public bool IsBoss(decimal salary)        {            return salary > 14000;        }    }
二、写个打印方法好让我们观察数据具体信息

        /// <summary>        /// 打印出来字段的信息并且判断是否是校长        /// </summary>        /// <typeparam name="T">泛类型,根据带有PrincipalAttribute属性的值来判断是否是校长</typeparam>        /// <param name="list"></param>        public static void Print<T>(IEnumerable<T> list)        {            var type = typeof(T);            System.Console.WriteLine("元素类型{0}",type.ToString());            int count = 1;            var properties = type.GetProperties();            foreach (var item in list)            {                System.Console.WriteLine("第{0}元素",count);                foreach (var property in properties)                {                    var currentValue = property.GetValue(item);                    System.Console.WriteLine("      元素{0},属性{1}:{2}",count,property.ToString(),currentValue);                    if (property.IsDefined(typeof(PrincipalAttribute)))                    {                        var attribute = Attribute.GetCustomAttribute(property, typeof(PrincipalAttribute)) as PrincipalAttribute;                        System.Console.WriteLine("      您是{0}", attribute.IsBoss(Convert.ToDecimal(currentValue)) ? "校长" : "老师");                    }                }                 count++;            }        }


三、写个最后测试方法

        public static void linqGroupBy()        {            var listTeachers = new List<Teacher>();            listTeachers.AddRange(new List<Teacher>()            {                new Teacher(){                    PKID=1,                    Name="小峰",                    Salary=19800,                    Age=34                },                 new Teacher(){                    PKID=2,                    Name="小帅",                    Salary=13000,                    Age=27                },                new Teacher(){                    PKID=3,                    Name="小娟",                    Salary=11000,                    Age=23                },                 new Teacher(){                    PKID=4,                    Name="小君",                    Salary=12000,                    Age=26                },                 new Teacher(){                    PKID=5,                    Name="小娟",                    Salary=90000,                    Age=22                }            });            //case1: from e in customers select f            var case1 = from teacher in listTeachers select teacher;            Print<Teacher>(case1);            System.Console.ReadLine();        }

四、给出运行结果




0 0
原创粉丝点击