bitbonk's answer, plus some example code, including both dynamically compiling and loading.
but here i used guids because when i tried to compile the existing dll, after unloding the related appdomain of course, i had an error saying that the assembly was in use. i could not figure it out, and had to compile a new dll each time. but that is ok with my application. if you achive to recompile the exsting dll please contact me.
string tempLib = System.Guid.NewGuid().ToString() ;
// Let's say txtIndicator is the textbox that executable code is written;
if (txtIndicator.Text.Trim() == "") return;
StreamWriter sw = new StreamWriter(LibPath + '\\' + txtIndicator.Text + ".cs", false);
sw.Write(txtSource.Text);
sw.Flush();
sw.Close();
CSharpCodeProvider csCompiler = new CSharpCodeProvider();
CompilerParameters compilerParams = new CompilerParameters();
compilerParams.OutputAssembly = AppDomain.CurrentDomain.BaseDirectory+"\\"+tempLib+".dll";
compilerParams.ReferencedAssemblies.Add("system.dll");
compilerParams.ReferencedAssemblies.Add("other assemblies");
compilerParams.GenerateExecutable = false;
compilerParams.IncludeDebugInformation = false;
// this is necessary if you are working with native dll.
compilerParams.CompilerOptions = "/platform:x86 /optimize";
CompilerResults cr = csCompiler.CompileAssemblyFromFile(compilerParams,Directory.GetFiles(LibPath));
if (cr.Errors.Count > 0)
{
listError.Items.Clear();
foreach (string s in cr.Output)
listError.Items.Add(s);
return;
}
string CurrentLibrary = compilerParams.OutputAssembly;
csCompiler.Dispose();
csCompiler = null;
AppDomainSetup ads = new AppDomainSetup();
ads.DisallowBindingRedirects = false;
ads.DisallowCodeDownload = true;
ads.ConfigurationFile = AppDomain.CurrentDomain.SetupInformation.ConfigurationFile;
AppDomain newdomain = AppDomain.CreateDomain("NEWDOMAIN",null,ads);
StreamReader sr = new StreamReader(CurrentLibrary);
byte[] byt = new byte[sr.BaseStream.Length];
sr.BaseStream.Read(byt, 0, byt.Length);
sr.Close();
sr.Dispose();
Assembly ass = newdomain.Load(byt);
AClassInAssembly ind = (AClassInAssembly)ass.CreateInstance("NameSpaceInAssembly.ClassName", true, BindingFlags.Default, null, new object[] { /*params*/ }, null, null);
/*Do some work*/
//
//
ass = null;
AppDomain.Unload(newdomain);
newdomain = null;