I was experimenting with dynamic class creation at runtime but I get the following error when i run my program at the last line. Does anyone know how to solve this? I tried searching similar problems online but none of the solutions have helped me. Thanks in advance for any help
Could not load file or assembly ‘file:///C:\Users\xxxx\AppData\Local\Temp\jelsfwqz.dll’ or one of its dependencies. The system cannot find the file specified.
string code2 = "using System;" +
"using System.Collections.Generic;" +
"using System.Linq;" +
"using System.Text;" +
"" +
" public sealed class CustomClass" +
" {" +
" }"
;
// Compiler and CompilerParameters
CSharpCodeProvider codeProvider = new CSharpCodeProvider();
CompilerParameters compParameters = new CompilerParameters();
CodeDomProvider compiler = CSharpCodeProvider.CreateProvider("CSharp");
// Compile the code
CompilerResults res = codeProvider.CompileAssemblyFromSource(compParameters, code2);
// Create a new instance of the class 'CustomClass'
object myClass = res.CompiledAssembly.CreateInstance("CustomClass");
Fixed code: