T LoadCode <T>(string scriptText, string scriptFile, params object[] args) where T : class { //Debug.Assert(false); if (!DisableReferencingFromCode) { ReferenceAssembliesFromCode(scriptText); } if (this.IsDebug) { //compile script and proxy as two separate actions //fin the exact class name of the script by test compiling it and browsing the assembly Assembly testAsm = CompileCode(scriptText, scriptFile); Type scriptType = AsmBrowser.FindFirstScriptUserType(testAsm); this.ReferenceAssemblyOf <T>(); string proxyTypeName = ""; string proxyClass = scriptType.BuildAlignToInterfaceCode <T>(out proxyTypeName, false); string parentClass = scriptType.FullName.Split('+').First(); // the full name is "Submission#0+Script" proxyClass = proxyClass.Replace(parentClass + ".", ""); //Compiler cannot compile the full name Submission#0.Script so convert it into short name Script string separator = Environment.NewLine + "// start of user code" + Environment.NewLine; Assembly scriptAsm = CompileCode(proxyClass + separator + scriptText, scriptFile); scriptType = AsmBrowser.FindFirstScriptUserType(scriptAsm, scriptType.Name); var proxyType = AsmBrowser.FindFirstScriptUserType(scriptAsm, proxyTypeName); var scriptObject = Activator.CreateInstance(scriptType, args); var proxyObject = Activator.CreateInstance(proxyType, new object[] { scriptObject }); return((T)proxyObject); } else { //compile script and proxy as two separate actions var scriptComp = CSharpScript.Create(scriptText, CompilerSettings).RunAsync().Result; var scriptAsmName = scriptComp.Script.GetCompilation().AssemblyName; Assembly scriptAsm = AppDomain.CurrentDomain.GetAssemblies().First(a => a.FullName.StartsWith(scriptAsmName, StringComparison.OrdinalIgnoreCase)); var scriptObject = scriptAsm.CreateObject("*", args); Type scriptType = scriptObject.GetType(); this.ReferenceAssemblyOf <T>(); string type = ""; string proxyClass = scriptObject.BuildAlignToInterfaceCode <T>(out type, false); string parentClass = scriptType.FullName.Split('+').First(); //Submission#0+Script proxyClass = proxyClass.Replace(parentClass + ".", ""); //Compiler cannot compile Submission#0.Script so convert it into Script var proxyComp = scriptComp.ContinueWithAsync(proxyClass).Result; var proxyAsmName = proxyComp.Script.GetCompilation().AssemblyName; Assembly proxyAsm = AppDomain.CurrentDomain.GetAssemblies().First(a => a.FullName.StartsWith(proxyAsmName, StringComparison.OrdinalIgnoreCase)); var proxyObject = proxyAsm.CreateObject("*", new object[] { scriptObject }); return((T)proxyObject); } }