The sap hr project is implemented by the company, but the attendance function of sap hr is too weak,
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 = "The source code cannot be empty!";
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 = "Compile successfully";
return true;
}
}
public object GetReturnResult (List <DataTable> parameters)
{
if (_method == null)
{
throw new Exception ("code not compiled");
}
return _method.Invoke (_instance, parameters.ToArray ());
}
#region IDisposable members
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 = <method of obtaining calculation methods>
= MIS.Util.AttdCalculate.Calculate (Source, dtShiftInfo, dtTimes, msg);
At the time of writing this article, the developed functions have not been put into operation yet, but after a recent test, there is no problem.