예제 #1
0
        /// <exclude />
        public static MethodInfo Create(IInlineFunction function, string code = null, InlineFunctionCreateMethodErrorHandler createMethodErrorHandler = null, List <string> selectedAssemblies = null)
        {
            if (string.IsNullOrWhiteSpace(code))
            {
                try
                {
                    code = GetFunctionCode(function);
                }
                catch (ThreadAbortException)
                {
                }
                catch (Exception ex)
                {
                    if (createMethodErrorHandler != null)
                    {
                        createMethodErrorHandler.OnLoadSourceError(ex);
                    }
                    else
                    {
                        LogMessageIfNotShuttingDown(function, ex.Message);
                    }
                    return(null);
                }
            }

            CompilerParameters compilerParameters = new CompilerParameters();

            compilerParameters.GenerateExecutable = false;
            compilerParameters.GenerateInMemory   = true;

            if (selectedAssemblies == null)
            {
                IEnumerable <IInlineFunctionAssemblyReference> assemblyReferences =
                    DataFacade.GetData <IInlineFunctionAssemblyReference>(f => f.Function == function.Id).Evaluate();

                foreach (IInlineFunctionAssemblyReference assemblyReference in assemblyReferences)
                {
                    string assemblyPath = GetAssemblyFullPath(assemblyReference.Name, assemblyReference.Location);
                    compilerParameters.ReferencedAssemblies.Add(assemblyPath);
                }
            }
            else
            {
                foreach (string reference in selectedAssemblies)
                {
                    compilerParameters.ReferencedAssemblies.Add(reference);
                }
            }


            Assembly appCodeAssembly = AssemblyFacade.GetAppCodeAssembly();

            if (appCodeAssembly != null)
            {
                compilerParameters.ReferencedAssemblies.Add(appCodeAssembly.Location);
            }

            CSharpCodeProvider compiler = new CSharpCodeProvider();
            CompilerResults    results  = compiler.CompileAssemblyFromSource(compilerParameters, code);

            if (results.Errors.HasErrors)
            {
                foreach (CompilerError error in results.Errors)
                {
                    if (createMethodErrorHandler != null)
                    {
                        createMethodErrorHandler.OnCompileError(error.Line, error.ErrorNumber, error.ErrorText);
                    }
                    else
                    {
                        LogMessageIfNotShuttingDown(function, error.ErrorText);
                    }
                }

                return(null);
            }


            Type type = results.CompiledAssembly.GetTypes().SingleOrDefault(f => f.Name == MethodClassContainerName);

            if (type == null)
            {
                string message = Texts.CSharpInlineFunction_OnMissingContainerType(MethodClassContainerName);

                if (createMethodErrorHandler != null)
                {
                    createMethodErrorHandler.OnMissingContainerType(message);
                }
                else
                {
                    LogMessageIfNotShuttingDown(function, message);
                }

                return(null);
            }

            if (type.Namespace != function.Namespace)
            {
                string message = Texts.CSharpInlineFunction_OnNamespaceMismatch(type.Namespace, function.Namespace);

                if (createMethodErrorHandler != null)
                {
                    createMethodErrorHandler.OnNamespaceMismatch(message);
                }
                else
                {
                    LogMessageIfNotShuttingDown(function, message);
                }

                return(null);
            }

            MethodInfo methodInfo = type.GetMethods(BindingFlags.Public | BindingFlags.Static).SingleOrDefault(f => f.Name == function.Name);

            if (methodInfo == null)
            {
                string message = Texts.CSharpInlineFunction_OnMissionMethod(function.Name, MethodClassContainerName);

                if (createMethodErrorHandler != null)
                {
                    createMethodErrorHandler.OnMissionMethod(message);
                }
                else
                {
                    LogMessageIfNotShuttingDown(function, message);
                }

                return(null);
            }

            return(methodInfo);
        }
예제 #2
0
        /// <exclude />
        public static MethodInfo Create(IInlineFunction function, string code = null, InlineFunctionCreateMethodErrorHandler createMethodErrorHandler = null, List<string> selectedAssemblies = null)
        {
            if (string.IsNullOrWhiteSpace(code))
            {
                try
                {
                    code = GetFunctionCode(function);
                }
                catch (ThreadAbortException)
                {
                }
                catch (Exception ex)
                {
                    if (createMethodErrorHandler != null)
                    {
                        createMethodErrorHandler.OnLoadSourceError(ex);
                    }
                    else
                    {
                        LogMessageIfNotShuttingDown(function, ex.Message);
                    }
                    return null;
                }
            }

            CompilerParameters compilerParameters = new CompilerParameters();
            compilerParameters.GenerateExecutable = false;
            compilerParameters.GenerateInMemory = true;

            if (selectedAssemblies == null)
            {
                IEnumerable<IInlineFunctionAssemblyReference> assemblyReferences =
                    DataFacade.GetData<IInlineFunctionAssemblyReference>(f => f.Function == function.Id).Evaluate();

                foreach (IInlineFunctionAssemblyReference assemblyReference in assemblyReferences)
                {
                    string assemblyPath = GetAssemblyFullPath(assemblyReference.Name, assemblyReference.Location);
                    compilerParameters.ReferencedAssemblies.Add(assemblyPath);
                }
            }
            else
            {
                foreach (string reference in selectedAssemblies)
                {
                    compilerParameters.ReferencedAssemblies.Add(reference);
                }
            }

            Assembly appCodeAssembly = AssemblyFacade.GetAppCodeAssembly();
            if (appCodeAssembly != null)
            {
                compilerParameters.ReferencedAssemblies.Add(appCodeAssembly.Location);
            }

            CSharpCodeProvider compiler = new CSharpCodeProvider();
            CompilerResults results = compiler.CompileAssemblyFromSource(compilerParameters, code);

            if (results.Errors.HasErrors)
            {
                foreach (CompilerError error in results.Errors)
                {
                    if (createMethodErrorHandler != null)
                    {
                        createMethodErrorHandler.OnCompileError(error.Line, error.ErrorNumber, error.ErrorText);
                    }
                    else
                    {
                        LogMessageIfNotShuttingDown(function, error.ErrorText);
                    }
                }

                return null;
            }

            Type type = results.CompiledAssembly.GetTypes().SingleOrDefault(f => f.Name == MethodClassContainerName);
            if (type == null)
            {
                string message = Texts.CSharpInlineFunction_OnMissingContainerType(MethodClassContainerName);

                if (createMethodErrorHandler != null)
                {
                    createMethodErrorHandler.OnMissingContainerType(message);
                }
                else
                {
                    LogMessageIfNotShuttingDown(function, message);
                }

                return null;
            }

            if (type.Namespace != function.Namespace)
            {
                string message = Texts.CSharpInlineFunction_OnNamespaceMismatch(type.Namespace, function.Namespace);

                if (createMethodErrorHandler != null)
                {
                    createMethodErrorHandler.OnNamespaceMismatch(message);
                }
                else
                {
                    LogMessageIfNotShuttingDown(function, message);
                }

                return null;
            }

            MethodInfo methodInfo = type.GetMethods(BindingFlags.Public | BindingFlags.Static).SingleOrDefault(f => f.Name == function.Name);
            if (methodInfo == null)
            {
                string message = Texts.CSharpInlineFunction_OnMissionMethod(function.Name, MethodClassContainerName);

                if (createMethodErrorHandler != null)
                {
                    createMethodErrorHandler.OnMissionMethod(message);
                }
                else
                {
                    LogMessageIfNotShuttingDown(function, message);
                }

                return null;
            }

            return methodInfo;
        }