. Net Dynamic compilation solves the attendance Calculation Problem

Source: Internet
Author: User


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.

 


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.