首页 > 其他 > 详细

特性,元数据

时间:2018-01-14 13:29:45      阅读:187      评论:0      收藏:0      [点我收藏+]
   public class Employee
    {
        // 只有SourceType== Salary 才转账
        public void TransferToEmployee([TransferSource(TransferType = TransferSourceType.Salary)]int number)
        {
            //汇钱到银行卡
        }
    }
    /// <summary>
    /// 转账类型
    /// </summary>
    public enum TransferSourceType
    {
        Salary,
        Reimburse,
        Loan
    }
  public class HR
    {
        public void ToSalary(Employee employee)
        {
// 使用元数据
var transferSource = typeof(Employee).GetMethod("TransferToEmployee").GetParameters()[0].GetCustomAttributes(false)[0] as TransferSource; if (transferSource.TransferType == TransferSourceType.Salary) { employee.TransferToEmployee(500); } else { employee.TransferToEmployee(0); } } }
        Employee employee = new Employee();
            HR hr = new HR();
            hr.ToSalary(employee);

 

特性,元数据

原文:https://www.cnblogs.com/xuyuchen/p/8283094.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!