Exemple #1
0
        private static Tuple <WeakReference, JObject> LoadAndExecute(string classNameWithNameSpace, byte[] compiledAssembly, JObject json)
        {
            using (var asm = new MemoryStream(compiledAssembly))
            {
                var assemblyLoadContext = new SimpleUnloadableAssemblyLoadContext();
                var assembly            = assemblyLoadContext.LoadFromStream(asm);

                IRunnable p          = (IRunnable)assembly.CreateInstance(classNameWithNameSpace);
                JObject   jsonResult = null;

                // ---------------------------------------------------------------------------------------

                /*JArray dataJArray = (JArray)json["data"];
                 * IList<InputDataStructure> inputList = dataJArray.ToObject<IList<InputDataStructure>>();
                 *
                 * List<OutputDataStructure> outputList = new List<OutputDataStructure>();
                 *
                 * foreach (var currInput in inputList)
                 * {
                 *  outputList.Add(new OutputDataStructure
                 *  {
                 *      out_1 = currInput.col_1 + currInput.col_2,
                 *      out_2 = currInput.col_6
                 *  });
                 * }
                 *
                 * // List => Json
                 * // https://www.newtonsoft.com/json/help/html/SerializingCollections.htm
                 *
                 * string outputJson = JsonConvert.SerializeObject(outputList, Formatting.Indented);
                 * JArray jsonArray = JArray.Parse(outputJson);
                 *
                 * var outputJObject = new JObject();
                 * outputJObject.Add("elaboratedData", jsonArray);*/

                // ---------------------------------------------------------------------------------------

                if (!(p == null))
                {
                    jsonResult = p.Elaborate(json);
                }
                else
                {
                    Console.WriteLine("Unable to instantiate the desired DynamicClass.");
                }

                assemblyLoadContext.Unload();

                return(new Tuple <WeakReference, JObject>(new WeakReference(assemblyLoadContext), jsonResult));
            }
        }
Exemple #2
0
        private static WeakReference LoadAndExecute(byte[] compiledAssembly, string[] args)
        {
            using (var asm = new MemoryStream(compiledAssembly))
            {
                var assemblyLoadContext = new SimpleUnloadableAssemblyLoadContext();

                var assembly = assemblyLoadContext.LoadFromStream(asm);

                var entry = assembly.EntryPoint;

                _ = entry != null && entry.GetParameters().Length > 0
                    ? entry.Invoke(null, new object[] { args })
                    : entry.Invoke(null, null);

                assemblyLoadContext.Unload();

                return(new WeakReference(assemblyLoadContext));
            }
        }