Beispiel #1
0
        /// <summary>
        /// Compiles the Visual Basic code in assembly.
        /// </summary>
        protected void CompileVB(string code, out Microsoft.VisualBasic.VBCodeProvider provider)
        {
            System.CodeDom.Compiler.CompilerParameters parameters = new System.CodeDom.Compiler.CompilerParameters();
            parameters.GenerateInMemory      = true; //  'Assembly is created in memory
            parameters.TreatWarningsAsErrors = false;
            parameters.WarningLevel          = 4;

            string[] refs = { "System.dll", System.Reflection.Assembly.GetExecutingAssembly().Location };

            parameters.ReferencedAssemblies.AddRange(refs);
            provider = new Microsoft.VisualBasic.VBCodeProvider();

            string tomoSource = @"
Imports System
Imports Fractrace.TomoGeometry


Public Class VBTomoFormula 
Inherits TomoFormula

" + code + @"

End Class
";

            try
            {
                results = provider.CompileAssemblyFromSource(parameters, tomoSource);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
                FormulaEditor.AddError(ex.ToString(), 0, 0);
                return;
            }

            if (results.Errors.Count != 0)
            {
                foreach (System.CodeDom.Compiler.CompilerError cerror in results.Errors)
                {
                    if (!cerror.IsWarning)
                    {
                        provider = null;
                        Console.WriteLine(cerror.ErrorText + "Line " + cerror.Line + " " + cerror.Column + " in \"" + tomoSource + "\"");
                        FormulaEditor.AddError(cerror.ErrorText + "Line " + cerror.Line + " " + cerror.Column + " in \"" + tomoSource + "\"", cerror.Line, cerror.Column);
                        return;
                    }
                }
            }
        }
Beispiel #2
0
        /// <summary>
        /// Compiles the code in C# assembly.
        /// </summary>
        protected void CompileCS(string code, out Microsoft.CSharp.CSharpCodeProvider provider)
        {
            System.CodeDom.Compiler.CompilerParameters parameters = new System.CodeDom.Compiler.CompilerParameters();
            parameters.GenerateInMemory      = true; //  'Assembly is created in memory
            parameters.TreatWarningsAsErrors = false;
            parameters.WarningLevel          = 4;

            string[] refs = { "System.dll", System.Reflection.Assembly.GetExecutingAssembly().Location };

            parameters.ReferencedAssemblies.AddRange(refs);
            provider = new Microsoft.CSharp.CSharpCodeProvider();

            List <Tuple <string, string, string> > interfaceVariableDefinition = ParseInterfaceVariables(code);

            StringBuilder initialisiationCode = new StringBuilder();

            foreach (Tuple <string, string, string> parameterInfo in interfaceVariableDefinition)
            {
                switch (parameterInfo.Item2)
                {
                case "double":
                    if (parameterInfo.Item3 != "")
                    {
                        initialisiationCode.AppendLine(parameterInfo.Item1 + "=GetOrSetDouble(\"" + parameterInfo.Item1 + "\"," + parameterInfo.Item3 + ");");
                    }
                    else
                    {
                        initialisiationCode.AppendLine(parameterInfo.Item1 + "=GetOrSetDouble(\"" + parameterInfo.Item1 + "\");");
                    }
                    break;

                case "bool":
                    if (parameterInfo.Item3 != "")
                    {
                        initialisiationCode.AppendLine(parameterInfo.Item1 + "=GetOrSetBool(\"" + parameterInfo.Item1 + "\"," + parameterInfo.Item3 + ");");
                    }
                    else
                    {
                        initialisiationCode.AppendLine(parameterInfo.Item1 + "=GetOrSetBool(\"" + parameterInfo.Item1 + "\");");
                    }
                    break;

                case "int":
                    if (parameterInfo.Item3 != "")
                    {
                        initialisiationCode.AppendLine(parameterInfo.Item1 + "=(int)GetOrSetDouble(\"" + parameterInfo.Item1 + "\"," + parameterInfo.Item3 + ");");
                    }
                    else
                    {
                        initialisiationCode.AppendLine(parameterInfo.Item1 + "=(int)GetOrSetDouble(\"" + parameterInfo.Item1 + "\");");
                    }
                    break;
                }
            }

            string tomoSource = @"
using System;
using Fractrace;
using Fractrace.TomoGeometry;
using Fractrace.Geometry;

public class CSTomoFormula : " + GuessFormulaClass(code) + @" {

    " + code + @"
public CSTomoFormula() {
" + initialisiationCode.ToString()
                                +
                                @"
    }

}
";

            try
            {
                results = provider.CompileAssemblyFromSource(parameters, tomoSource);
                RemoveUnusedParameters(tomoSource);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
                FormulaEditor.AddError(ex.ToString(), 0, 0);
                return;
            }
            if (results.Errors.Count != 0)
            {
                foreach (System.CodeDom.Compiler.CompilerError cerror in results.Errors)
                {
                    if (!cerror.IsWarning)
                    {
                        provider = null;
                        Console.WriteLine(cerror.ErrorText + "Line " + cerror.Line + " " + cerror.Column + " in \"" + tomoSource + "\"");
                        FormulaEditor.AddError(cerror.ErrorText + "Line " + cerror.Line + " " + cerror.Column + " in \"" + tomoSource + "\"", cerror.Line, cerror.Column);
                        return;
                    }
                }
            }
        }