with C# Code run on Dynamicx AX 2009

 

1)I use Visual Studio 2008 to create a Class Library (name space)project named:JimmyXieyf.CSharpScript,

and then added to the project in this class:ScriptRunner.

JimmyXieyf.CSharpScript.ScriptRunner
using System;
using System.Reflection;
using System.ComponentModel;
using System.CodeDom;//Code Document Object Model
using System.CodeDom.Compiler;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.CSharp;
using System.Collections;
using System.Diagnostics;
using System.Collections.Specialized;

namespace JimmyXieYF.CSharpScript
{
public class ScriptRunner
{
private Assembly assembly;
private StringCollection compileErrors = new StringCollection();

public bool Compile(string script)
{
try
{
CSharpCodeProvider codeProvider
= new CSharpCodeProvider();
CompilerParameters compilerParameters
= new CompilerParameters();
compilerParameters.GenerateExecutable
= false;
compilerParameters.GenerateInMemory
= true;
compilerParameters.IncludeDebugInformation
= false;
compilerParameters.ReferencedAssemblies.Add(
"System.dll");
compilerParameters.ReferencedAssemblies.Add(
"System.Windows.Forms.dll");
compilerParameters.ReferencedAssemblies.Add(
"System.XML.dll");
CompilerResults results
=
codeProvider.CompileAssemblyFromSource(compilerParameters, script);

if (!results.Errors.HasErrors)
{
this.assembly = results.CompiledAssembly;
return true;
}
else
{
this.compileErrors.Clear();
foreach (CompilerError ce in results.Errors)
{
compileErrors.Add(ce.ErrorText);
}
return false;
}
}
catch (Exception e)
{
EventLog.WriteEntry(
"CSharpScript",e.Message, EventLogEntryType.Error);
throw;
}
}

public StringCollection CompileErros
{
get
{
return compileErrors;
}
}

public ArrayList Run(ArrayList args)
{
ArrayList ret
= new ArrayList();
try
{
Type entryType
= null;
MethodInfo entryPoint
= null;
foreach (var type in assembly.GetExportedTypes())
{
if (type.Name.Equals("Script"))
{
entryType
= type;
break;
}
}
foreach (var methodInfo in entryType.GetMethods(BindingFlags.Public |
BindingFlags.Static))
{
if (methodInfo.Name.Equals("EntryMethod"))
{
entryPoint
= methodInfo;
break;
}
}
Type returnType
= entryPoint.ReturnType;
ParameterInfo[] parameters
= entryPoint.GetParameters();
if (returnType.Equals(typeof(ArrayList)) &&
parameters
!= null &&
parameters.Count()
== 1 &&
parameters[
0].ParameterType.Equals(typeof(ArrayList)))
{
ret
= (ArrayList)entryPoint.Invoke(null, new object[] { args });
}

}
catch (Exception e)
{
StringBuilder sb
= new StringBuilder();

sb.Append(e.Message);
sb.Append(
"-->");
sb.Append(e.StackTrace);
sb.Append(Environment.NewLine);
Exception inner
= e.InnerException;
while (inner != null)
{
sb.Append(inner.Message);
sb.Append(
"-->");
sb.Append(inner.StackTrace);
sb.Append(Environment.NewLine);
inner
= inner.InnerException;
}
EventLog.WriteEntry(
"CSharpScript", sb.ToString(), EventLogEntryType.Error);
throw;
}

return ret;
}
}
}

2) setup the C# project sign the assembly (key.snk.),then Compiled the C# Code and generate dll file(JimmyXieYF.CSharpScript.dll)

 

3)drag-and-drop the dll file(JimmyXieYF.CSharpScript.dll) to the "C:\WINDOWS\assembly" and "..\Client\Bin"

 

4)add the dll "..\Client\Bin\JimmyXieYF.CSharpScript.dll" to the AOT reference

 

5)wrote a job runningC# code

X++ Job

static void Jimmy_AX2009RunCSharpCode(Args _args)
{
    System.Collections.ArrayList                        scriptArgs;
    System.Collections.ArrayList                        returns;
    System.Collections.IEnumerator                      enumerator;
    System.Collections.Specialized.StringCollection     errors;
    JimmyXieYF.CSharpScript.ScriptRunner                runner;
    int                                                 result;

    #localmacro.SourceScript
        "using System;" +
        "using System.Collections;" +
        "public class Script" +
        "{" +
        "    public static ArrayList EntryMethod(ArrayList args)" +
        "    {" +
        "        int a = (int)args[0];" +
        "        int b = (int)args[1];" +
        "        int c = a + b;\r\n" +
        "        ArrayList returns = new ArrayList();" +
        "        returns.Add(c);" +
        "        return returns;" +
        "    }" +
        "}"
    #endmacro

    ;
    runner = new JimmyXieYF.CSharpScript.ScriptRunner();
    // Prepares the parameters
    scriptArgs = new System.Collections.ArrayList();
    scriptArgs.Add(70);
    scriptArgs.Add(20);

    // Runs the script
    if (runner.Compile(#SourceScript))
    {
        // Gets the return values and output
        returns = runner.Run(scriptArgs);
        result  = returns.get_Item(0);
        info(int2str(result));
    }
    else
    {
        errors = runner.get_CompileErros();
        enumerator = errors.GetEnumerator();
        while (enumerator.MoveNext())
        {
            error(enumerator.get_Current());
        }
    }

}
原文地址:https://www.cnblogs.com/Fandyx/p/1890458.html