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); }
public void NoParameterNames() { // Create simple interface where method parameters have no names. // interface I // { // void M(object, object); // } var reference = DesktopRuntimeUtil.CreateReflectionEmitAssembly(moduleBuilder => { var typeBuilder = moduleBuilder.DefineType( "I", TypeAttributes.Interface | TypeAttributes.Public | TypeAttributes.Abstract); var methodBuilder = typeBuilder.DefineMethod( "M", MethodAttributes.Public | MethodAttributes.Abstract | MethodAttributes.Virtual, typeof(void), new Type[] { typeof(object), typeof(object) }); methodBuilder.DefineParameter(1, ParameterAttributes.None, null); methodBuilder.DefineParameter(2, ParameterAttributes.None, null); typeBuilder.CreateType(); }); var source = @"class C { static void M(I o) { o.M(0, value: 2); } }"; var compilation = CreateCompilation(source, new[] { reference }); compilation.VerifyDiagnostics( // (5,16): error CS1744: Named argument 'value' specifies a parameter for which a positional argument has already been given Diagnostic(ErrorCode.ERR_NamedArgumentUsedInPositional, "value").WithArguments("value").WithLocation(5, 16)); }
internal Assembly GetOrLoad(ModuleData moduleData, bool reflectionOnly) { var cache = reflectionOnly ? _reflectionOnlyAssemblyCache : _assemblyCache; lock (s_guard) { Assembly assembly; if (cache.TryGetValue(moduleData.Mvid, out 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"); } if (loadedAssembly != assembly) { throw new Exception("Cache entry doesn't match result of load"); } return(assembly); } }