Example #1
0
        internal Assembly GetOrLoad(ModuleData moduleData, bool reflectionOnly)
        {
            var cache = reflectionOnly ? _reflectionOnlyAssemblyCache : _assemblyCache;

            lock (s_guard)
            {
                if (cache.TryGetValue(moduleData.Mvid, out var assembly))
                {
                    return(assembly);
                }

                var loadedAssembly = DesktopRuntimeUtil.LoadAsAssembly(moduleData.SimpleName, moduleData.Image, reflectionOnly);

                // Validate the loaded assembly matches the value that we now have in the cache.
                if (!cache.TryGetValue(moduleData.Mvid, out assembly))
                {
                    throw new Exception($"Explicit assembly load didn't update the proper cache: '{moduleData.SimpleName}' ({moduleData.Mvid})");
                }

                if (loadedAssembly != assembly)
                {
                    throw new Exception("Cache entry doesn't match result of load");
                }

                return(assembly);
            }
        }
Example #2
0
        public int Execute(string moduleName, string[] mainArgs, int?expectedOutputLength, out string output)
        {
            ImmutableArray <byte> bytes = GetModuleBytesByName(moduleName);
            Assembly   assembly         = DesktopRuntimeUtil.LoadAsAssembly(moduleName, bytes);
            MethodInfo entryPoint       = assembly.EntryPoint;

            Debug.Assert(entryPoint != null, "Attempting to execute an assembly that has no entrypoint; is your test trying to execute a DLL?");

            object result = null;
            string stdOut, stdErr;

            DesktopRuntimeEnvironment.Capture(() =>
            {
                var count = entryPoint.GetParameters().Length;
                object[] args;
                if (count == 0)
                {
                    args = new object[0];
                }
                else if (count == 1)
                {
                    args = new object[] { mainArgs ?? new string[0] };
                }
                else
                {
                    throw new Exception("Unrecognized entry point");
                }

                result = entryPoint.Invoke(null, args);
            }, expectedOutputLength ?? 0, out stdOut, out stdErr);

            output = stdOut + stdErr;
            return(result is int?(int)result : 0);
        }