Exemplo n.º 1
0
        public void Test1()
        {
            var assembly = nativeCompiler.Compile(@"using System;

namespace Testing
{
	public static class TestCase
	{
		public static int TestFunction()
		{
			return Math.Abs(-2)*2*2;
		}
	}
}");

            Assert.AreEqual(8,
                            assembly.GetType("Testing.TestCase")
                            .GetMethod("TestFunction", BindingFlags.Public | BindingFlags.Static)
                            .Invoke(null, null));
        }
        protected Delegate Compile()
        {
            SetVariablesAndSignatures();
            _customFunctionsCSharpCode = _tslCompiler.TransformToCSharp(CustomFunctionsTslCode);
            MainCSharpCode             = _tslCompiler.TransformToCSharp(MainTslCode);
            TransformImplicitToExplicit();

            var fullCode = BuildCode();

            try
            {
                var assembly = NativeCompiler.Compile(fullCode);
                var cls      = assembly.GetType("FunctionsCreatorNS.FunctionsCreator");
                var method   = cls.GetMethod("CustomFunction", BindingFlags.Static | BindingFlags.Public);
                return(Delegate.CreateDelegate(_delegateType, method));
            }
            catch (Exception ex)
            {
                if (ex is CompilationException)
                {
                    throw;
                }

                var message =
                    Strings
                    .ErrorInExpressionSyntaxOneOfUsedFunctionsDoesNotExistIsIncompatibleWithGivenArgumentsOrYouJustMadeAMistakeWritingExpression;
                message += Environment.NewLine + Strings.Details;
                message += Environment.NewLine + ex.Message;

                Logger.Parameters["MainTslCode"]         = MainTslCode;
                Logger.Parameters["MainCSharpCode"]      = MainCSharpCode;
                Logger.Parameters["customFunctionsCode"] = _customFunctionsCSharpCode;
                Logger.MethodName = MethodBase.GetCurrentMethod().Name;
                Logger.Log(message, ErrorType.Evaluation, ex);

                throw new EvaluationException(message, ex);
            }
        }