由於公司實施SAP HR項目,但是SAP HR對考勤功能真的太弱化了,
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.CodeDom.Compiler; using Microsoft.CSharp; using System.Reflection; using System.Data; namespace EDICLibrary { public class DynCompiler:IDisposable { CodeDomProvider _codeprovide; CompilerParameters _parameters; CompilerResults _result = null; MethodInfo _method = null; Object _instance = null; public DynCompiler() { _codeprovide = new CSharpCodeProvider(); _parameters = new CompilerParameters(); _parameters.GenerateExecutable = false; _parameters.GenerateInMemory = true; _parameters.ReferencedAssemblies.Add("System.dll"); _parameters.ReferencedAssemblies.Add("System.Data.dll"); _parameters.ReferencedAssemblies.Add("System.Xml.dll"); } public bool SourceCompiler(string source, out string outputMsg) { outputMsg = ""; StringBuilder sbout = new StringBuilder(); StringBuilder sb = new StringBuilder(); if (string.IsNullOrEmpty(source)) { outputMsg = "源代碼不能為空!"; return false; } else { sb.AppendLine("using System;"); sb.AppendLine("using System.Collections.Generic;"); sb.AppendLine("using System.Text;"); sb.AppendLine("using System.Data;"); sb.AppendLine ("using System.Xml;"); sb.AppendLine("namespace JMCompiler"); sb.AppendLine("{"); sb.AppendLine("public class DynCompilerHelper"); sb.AppendLine("{"); sb.AppendLine("public DataTable Calculate(DataTable dtShiftInfo,DataTable dtTimes)"); sb.AppendLine("{"); sb.Append(source); sb.Append("}"); sb.AppendLine("}"); sb.AppendLine("}"); } _result = _codeprovide.CompileAssemblyFromSource(_parameters, sb.ToString()); if (_result.Errors.HasErrors) { foreach (string str in _result.Output) { sbout.AppendLine(str); } outputMsg = sbout.ToString(); return false; } else { Type _type = _result.CompiledAssembly.GetType("JMCompiler.DynCompilerHelper"); _instance = Activator.CreateInstance(_type); _method = _type.GetMethod("Calculate",new Type[]{typeof(DataTable),typeof (DataTable)}); outputMsg = "編譯成功"; return true; } } public object GetReturnResult(List<DataTable> parameters) { if (_method == null) { throw new Exception ("未進行代碼編譯"); } return _method.Invoke(_instance, parameters.ToArray()); } #region IDisposable 成員 public void Dispose() { _codeprovide = null; _parameters = null; _result = null; _method = null; _instance = null; } #endregion } }
DataTable Calculate( source, DataTable dtShiftInfo, DataTable dtTimes, (= = result = complier.SourceCompiler(source, <DataTable> lstData = List<DataTable>= =
Source = Source = <獲取計算工式的方法> = MIS.Util.AttdCalculate.Calculate(Source, dtShiftInfo, dtTimes, msg);
在寫這篇文章的時候,開發的功能還沒有投入到正式運行,但是經過最近一段時候的測試來看,沒有任何問題。