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;
}
}
}
}