Esempio n. 1
0
        private static CompilerResult DoCompilation(string code)
        {
            var csharpSyntaxTree = BuildSyntaxTree(code);

            var compilation = CSharpCompilation.Create(
                Path.GetRandomFileName(),
                new[] { csharpSyntaxTree },
                References,
                new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary));

            using (var ms = new MemoryStream())
            {
                var emitResult = compilation.Emit(ms);
                var output     = new CompilerResult
                {
                    Success  = emitResult.Success,
                    Messages = emitResult.Diagnostics.ToList()
                };


                if (emitResult.Success)
                {
                    var type = Assembly.Load(ms.GetBuffer())
                               .GetTypes()
                               .FirstOrDefault(t => t.Name == "ExpressionClass");

                    var delegateObject = type
                                         ?.GetMethod("CreateDelegate", BindingFlags.Static | BindingFlags.Public)
                                         ?.Invoke(null, new object[0]) as System.Delegate;

                    output.ReturnType = delegateObject?.GetType().GetGenericArguments()[0];
                    output.Execute    = delegateObject;
                }

                return(output);
            }
        }
        protected override void OnInitCalled()
        {
            var code = GetCodeFromLambda(this.ConfigObject.LambdaCode);

            this._result = Compiler.Compile(code);

            foreach (var resultMessage in this._result.Messages)
            {
                this.Engine.OutputMessage(this.NToolId, MessageStatus.STATUS_Info, resultMessage.GetMessage());
            }

            if (!this._result.Success)
            {
                this.Engine.OutputMessage(
                    this.NToolId,
                    MessageStatus.STATUS_Error,
                    "Compilation Failed.");
                return;
            }

            var sampleType = this._result.ReturnType;

            this._descriptions = FieldDescriptionsFromType(sampleType);
        }