我曾经为一个HR系统写过一个工资计算的辅助类的草稿,
现在发出来给大家看看!
这个代码只是我的初稿,最终在HR中怎么使用我就不知道了,因为HR不是我写的!
但就下面简单的Test里实现的功能已经可以使用!
现在发出来给大家看看!
这个代码只是我的初稿,最终在HR中怎么使用我就不知道了,因为HR不是我写的!
但就下面简单的Test里实现的功能已经可以使用!
1
using System;
2
using System.Collections.Generic;
3
using System.Text;
4
using System.Text.RegularExpressions;
5
using System.CodeDom;
6
using System.CodeDom.Compiler;
7
using System.Reflection;
8
using Microsoft.CSharp;
9![]()
10
namespace WindowsApplication3
11
{
12
/// <summary>
13
/// 表达式辅助类
14
/// </summary>
15
public class ExpressionHelper : IDisposable
16
{
17
#if DEBUG
18
public static object Test()
19
{
20
using (ExpressionHelper helper = new ExpressionHelper("[基本工资]+([职级工资]/[出勤天数]+100)*[出勤天数]/[行事历天数]+年龄([职级工资]+[岗位工资]+年龄(10))"))
21
{
22
helper.AddMethod("年龄", @"if ( [部门]==[部门.JAVA部门])
23
{
24
return 100;
25
}
26
else if( [部门]==[部门.部门测试] && [年假] >= 8)
27
{
28
return [年假]*比率;
29
}
30
else
31
{
32
return 200;
33
}", typeof(int), new ParameterInfo("比率", typeof(int)));
34
return helper.Execute().ToString();
35
}
36
}
37
#endif
38
private AppDomain m_Domain;
39
private ExpressionExecute m_Execute;
40![]()
41
private Dictionary<string, object> m_Properties;
42
private Dictionary<string, Dictionary<string, object>> m_Consts;
43
private string m_Expression;
44
private const string Pattern = @"\[(@)?(.+?)(\.(.+?))?\]";
45![]()
46
/// <summary>
47
/// 表达式中所使用的属性如'[基本工资]'
48
/// </summary>
49
public Dictionary<string, object> Properties
50
{
51
get { return m_Properties; }
52
}
53![]()
54
/// <summary>
55
/// 表达式中所使用的常量Id如'[部门.DotNet部门]'
56
/// </summary>
57
public Dictionary<string, Dictionary<string, object>> Consts
58
{
59
get { return m_Consts; }
60
}
61![]()
62
public ExpressionHelper(string expression)
63
{
64
m_Properties = new Dictionary<string, object>();
65
m_Consts = new Dictionary<string, Dictionary<string, object>>();
66
m_Expression = expression;
67
InitVariable();
68
m_Domain = AppDomain.CreateDomain("ExpressionExecute" + Guid.NewGuid().ToString());
69![]()
70
foreach (Assembly ass in AppDomain.CurrentDomain.GetAssemblies())
71
try
72
{
73
m_Domain.Load(ass.FullName);
74
}
75
catch { }
76
m_Execute = m_Domain.CreateInstanceFromAndUnwrap(typeof(ExpressionExecute).Assembly.Location, typeof(ExpressionExecute).FullName) as ExpressionExecute;
77
}
78![]()
79
/// <summary>
80
/// 初始化常量和变量列表
81
/// </summary>
82
private void InitVariable()
83
{
84
// TODO: 需要修改此方法,进行初始化数据,如加载个人信息.常量(如部门,学历等)
85
Guid id = Guid.NewGuid();
86
Properties.Add("部门", id);
87
Properties.Add("年假", 10);
88
Properties.Add("基本工资", 1000);
89
Properties.Add("职级工资", 1000);
90
Properties.Add("出勤天数", 23);
91
Properties.Add("行事历天数", 10);
92
Properties.Add("岗位工资", 2000);
93
Consts.Add("部门", new Dictionary<string, object>());
94<span style="
using System;2
using System.Collections.Generic;3
using System.Text;4
using System.Text.RegularExpressions;5
using System.CodeDom;6
using System.CodeDom.Compiler;7
using System.Reflection;8
using Microsoft.CSharp;9

10
namespace WindowsApplication311
{12
/// <summary>13
/// 表达式辅助类14
/// </summary>15
public class ExpressionHelper : IDisposable16
{17
#if DEBUG18
public static object Test()19
{20
using (ExpressionHelper helper = new ExpressionHelper("[基本工资]+([职级工资]/[出勤天数]+100)*[出勤天数]/[行事历天数]+年龄([职级工资]+[岗位工资]+年龄(10))"))21
{22
helper.AddMethod("年龄", @"if ( [部门]==[部门.JAVA部门])23
{ 24
return 100;25
} 26
else if( [部门]==[部门.部门测试] && [年假] >= 8) 27
{28
return [年假]*比率; 29
} 30
else31
{32
return 200; 33
}", typeof(int), new ParameterInfo("比率", typeof(int)));34
return helper.Execute().ToString();35
}36
}37
#endif38
private AppDomain m_Domain;39
private ExpressionExecute m_Execute;40

41
private Dictionary<string, object> m_Properties;42
private Dictionary<string, Dictionary<string, object>> m_Consts;43
private string m_Expression;44
private const string Pattern = @"\[(@)?(.+?)(\.(.+?))?\]";45

46
/// <summary>47
/// 表达式中所使用的属性如'[基本工资]'48
/// </summary>49
public Dictionary<string, object> Properties50
{51
get { return m_Properties; }52
}53

54
/// <summary>55
/// 表达式中所使用的常量Id如'[部门.DotNet部门]'56
/// </summary>57
public Dictionary<string, Dictionary<string, object>> Consts58
{59
get { return m_Consts; }60
}61

62
public ExpressionHelper(string expression)63
{64
m_Properties = new Dictionary<string, object>();65
m_Consts = new Dictionary<string, Dictionary<string, object>>();66
m_Expression = expression;67
InitVariable();68
m_Domain = AppDomain.CreateDomain("ExpressionExecute" + Guid.NewGuid().ToString());69

70
foreach (Assembly ass in AppDomain.CurrentDomain.GetAssemblies())71
try72
{73
m_Domain.Load(ass.FullName);74
}75
catch { }76
m_Execute = m_Domain.CreateInstanceFromAndUnwrap(typeof(ExpressionExecute).Assembly.Location, typeof(ExpressionExecute).FullName) as ExpressionExecute;77
}78

79
/// <summary>80
/// 初始化常量和变量列表81
/// </summary>82
private void InitVariable()83
{84
// TODO: 需要修改此方法,进行初始化数据,如加载个人信息.常量(如部门,学历等)85
Guid id = Guid.NewGuid();86
Properties.Add("部门", id);87
Properties.Add("年假", 10);88
Properties.Add("基本工资", 1000);89
Properties.Add("职级工资", 1000);90
Properties.Add("出勤天数", 23);91
Properties.Add("行事历天数", 10);92
Properties.Add("岗位工资", 2000);93
Consts.Add("部门", new Dictionary<string, object>());94<span style="


浙公网安备 33010602011771号