zoukankan      html  css  js  c++  java
  • 特性与元数据

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    namespace chap2_1_7
    {
        class Program
        {
            static void Main(string[] args)
            {
                HR hr = new HR();
                Employee employee = new Employee();
                hr.ToSalary(employee);
                Console.ReadKey();
            }
        }
        public enum TransferSourceType//转账类型
        { 
            Salary,
            Reimburse,
            Loan
        }
        [AttributeUsage(AttributeTargets.Parameter)]
        public class TransferSource : Attribute//转账元数据
        {
            public TransferSourceType TransferType { get; set; }
        }
        public partial class Employee//员工实体
        {
            public void PaySalary([TransferSource(TransferType = TransferSourceType.Salary)] int toNumber)
            { 
                //直接汇入员工银行卡
                Console.WriteLine("收到工资:"+toNumber);
            }
            public void PayReimburse([TransferSource(TransferType = TransferSourceType.Salary)] int toNumber)
            {
                //直接汇入员工银行卡
                Console.WriteLine("收到工资:" + toNumber);
            }
            public void PayLoan([TransferSource(TransferType = TransferSourceType.Salary)] int toNumber)
            {
                //直接汇入员工银行卡
                Console.WriteLine("收到工资:" + toNumber);
            }
        }
        public class HR
        {
            public void ToSalary(Employee employee)
            {
                var transferSource = typeof(Employee).GetMethod("PaySalary").GetParameters()[0].GetCustomAttributes(false)[0] as TransferSource;
                switch (transferSource.TransferType)
                {
                    case TransferSourceType.Salary:
                        {
                            employee.PaySalary(6000);//发工资
                        }break;
                    case TransferSourceType.Reimburse:
                        {
                            employee.PayReimburse(500);//报销                        
                        }break;
                    case TransferSourceType.Loan:
                        {
                            employee.PayLoan(20000);//借款
                        }break;
                }
            }
        }
    }
  • 相关阅读:
    ChsDet is a Charset Detector (检测字符编码)
    Microsoft Fakes进行单元测试
    区域及分离、Js压缩、css、jquery扩展
    服务端软件的服务品质
    警告: [SetPropertiesRule]{Server/Service/Engine/Host/Context}
    开发之技能
    分布式
    工具集
    Visual Studio Code
    Autofac 入门
  • 原文地址:https://www.cnblogs.com/sulong/p/4917100.html
Copyright © 2011-2022 走看看