Пример #1
0
        /// <summary>
        ///
        ///     This code compiles a block of code into an Assembly, embeds it within a safe Application Domain,
        ///     finds the first type in the Assembly with the IRunnable type, and then instantiates and executes that class.
        ///
        ///     UNDER CONSTRUCTION - DOES NOT CURRENTLY WORK
        ///
        /// </summary>
        /// <param name="UrlParameters">Parameters that might be needed in the future</param>
        /// <param name="ExecCode">The code to be compiled and assembled into an assembly</param>
        /// <param name="Body">The values needed by the code in order to successfully execute the code</param>
        /// <returns>The value(s) sought by the caller and returned by the compiled code block</returns>
        public static List <Dictionary <string, string> > CompileAndExecuteCodeSafe(string UrlParameters,
                                                                                    string ExecCode,
                                                                                    List <Dictionary <string, string> > Body)
        {
            AppDomain sandbox = null;

            HashSet <string> additionalAssemblyDirs = new HashSet <string>();

            try
            {
                string targetAssemblyPath = AppDomain.CurrentDomain.SetupInformation.ApplicationBase;

                var assemblyNames = (from a in AppDomain.CurrentDomain.GetAssemblies()
                                     where !a.IsDynamic
                                     select a.Location).ToArray();

                foreach (string tmpAssembly in assemblyNames)
                {
                    // additionalAssemblyDirs.Add(Path.GetDirectoryName(tmpAssembly));

                    if (tmpAssembly.Contains("MetadataApiCommon"))
                    {
                        targetAssemblyPath = tmpAssembly;
                        additionalAssemblyDirs.Add(Path.GetDirectoryName(targetAssemblyPath));
                    }
                }

                Assembly dynamicAssembly = RunnableExecutor.CompileCode(ExecCode, assemblyNames, false);

                additionalAssemblyDirs.Add(RunnableExecutor.GetAssemblyDirectory(dynamicAssembly));

                sandbox = ProduceSecureDomain(additionalAssemblyDirs.ToArray());

                var typesInAssembly = dynamicAssembly.GetTypes();

                var type = typesInAssembly.First();

                RunnableExecutor runnableExecutor =
                    (RunnableExecutor)sandbox.CreateInstanceFromAndUnwrap(targetAssemblyPath, "MetadataApiCommon.RunnableExecutor") as RunnableExecutor;

                IRunnable runnable = runnableExecutor.Create(GetAssemblyPath(dynamicAssembly), type.FullName, null);

                return(runnable.Run(Body));
            }
            finally
            {
                if (sandbox != null)
                {
                    AppDomain.Unload(sandbox);
                }
            }
        }
Пример #2
0
        /// <summary>
        ///
        ///     This code compiles a block of code into an Assembly, finds the first type in the Assembly with the IRunnable type,
        ///     and then instantiates and executes that class.
        ///
        /// </summary>
        /// <param name="UrlParameters">Parameters that might be needed in the future</param>
        /// <param name="ExecCode">The code to be compiled and assembled into an assembly</param>
        /// <param name="Body">The values needed by the code in order to successfully execute the code</param>
        /// <param name="Assemblies">Any other assemblies needed for successful compilation</param>
        /// <returns>The value(s) sought by the caller and returned by the compiled code block</returns>
        public List <Dictionary <string, string> > CompileAndExecuteCode(string UrlParameters,
                                                                         string ExecCode,
                                                                         List <Dictionary <string, string> > Body,
                                                                         string[]                         Assemblies)
        {
            Assembly dynamicAssembly = RunnableExecutor.CompileCode(ExecCode, Assemblies);

            var typesInAssembly = dynamicAssembly.GetTypes();

            var type = typesInAssembly.First();

            return(RunnableExecutor.InvokeRunnable(type, Body));
        }