A)编译C#EXE和动态链接库相对容易。 B)执行EXE意味着运行一个新的应用程序。加载DLL意味着可以在应用程序或项目之间共享的情况下使用方法和函数。
现在,编译EXE最快和最简单的方法(或轻度修改,DLL)可以从MSDN或为了您的方便找到:
如何快速编译C#DLL,加载和使用
private bool CompileCSharpCode(string script)
{
lvErrors.Items.Clear();
try
{
CSharpCodeProvider provider = new CSharpCodeProvider();
// Build the parameters for source compilation.
CompilerParameters cp = new CompilerParameters
{
GenerateInMemory = false,
GenerateExecutable = false, // True = EXE, False = DLL
IncludeDebugInformation = true,
OutputAssembly = "eventHandler.dll", // Compilation name
};
// Add in our included libs.
cp.ReferencedAssemblies.Add("System.dll");
cp.ReferencedAssemblies.Add("System.Windows.Forms.dll");
cp.ReferencedAssemblies.Add("Microsoft.VisualBasic.dll");
// Invoke compilation. This works from a string, but you can also load from a file using FromFile()
CompilerResults cr = provider.CompileAssemblyFromSource(cp, script);
if (cr.Errors.Count > 0)
{
// Display compilation errors.
foreach (CompilerError ce in cr.Errors)
{
//I have a listview to display errors.
lvErrors.Items.Add(ce.ToString());
}
return false;
}
else
{
lvErrors.Items.Add("Compiled Successfully.");
}
provider.Dispose();
}
catch (Exception e)
{
// never really reached, but better safe than sorry?
lvErrors.Items.Add("SEVERE! "+e.Message + e.StackTrace.ToString());
return false;
}
return true;
}
现在,你可以在飞行中编译,如何加载DLL之间有一些差异。通常来说,您可以将其作为Visual Studio中的参考添加到项目中。这很容易,你可能已经完成了很多次,但是我们想在当前的项目中使用它,并且我们不能很好地要求用户在每次他们想测试他们的新DLL时重新编译整个项目。因此,我将简单讨论一下如何加载一个图书馆。这里的另一个术语是“编程式”。要做到这一点,一个编译成功后,我们加载了一个大会如下:
Assembly assembly = Assembly.LoadFrom("yourfilenamehere.dll");
如果你有一个AppDomain,你可以试试这个:
Assembly assembly = domain.Load(AssemblyName.GetAssemblyName("yourfilenamehere.dll"));
现在的lib是“引用”的,我们可以打开它并使用它。有两种方法可以做到这一点。一个要求你知道该方法是否有参数,另一个会检查你。我会做的更晚,你可以检查另一个MSDN。
// replace with your namespace.class
Type type = assembly.GetType("company.project");
if (type != null)
{
// replace with your function's name
MethodInfo method = type.GetMethod("method");
if (method != null)
{
object result = null;
ParameterInfo[] parameters = method.GetParameters();
object classInstance = Activator.CreateInstance(type, null);
if (parameters.Length == 0) // takes no parameters
{
// method A:
result = method.Invoke(classInstance, null);
// method B:
//result = type.InvokeMember("method", BindingFlags.InvokeMethod, null, classInstance, null);
}
else // takes 1+ parameters
{
object[] parametersArray = new object[] { }; // add parameters here
// method A:
result = method.Invoke(classInstance, parametersArray);
// method B:
//result = type.InvokeMember("method", BindingFlags.InvokeMethod, null, classInstance, parametersArray);
}
}
}
问题: 首先编译工作正常。第一次执行正常。但是,重新编译尝试会出错,并说您的* .PDP(调试器数据库)正在使用中。我听说过关于编组和AppDomains的一些提示,但我还没有完全解决这个问题。重新编译只会在DLL加载后失败。
在编组当前尝试& &的AppDomain:上_instance = _domain.CreateInstanceAndUnwrap(_assembly.FullName,的typeName)
class ProxyDomain : MarshalByRefObject
{
private object _instance;
public object Instance
{
get { return _instance; }
}
private AppDomain _domain;
public AppDomain Domain
{
get
{
return _domain;
}
}
public void CreateDomain(string friendlyName, System.Security.Policy.Evidence securityinfo)
{
_domain = AppDomain.CreateDomain(friendlyName, securityinfo);
}
public void UnloadDomain()
{
try
{
AppDomain.Unload(_domain);
}
catch (ArgumentNullException dne)
{
// ignore null exceptions
return;
}
}
private Assembly _assembly;
public Assembly Assembly
{
get
{
return _assembly;
}
}
private byte[] loadFile(string filename)
{
FileStream fs = new FileStream(filename, FileMode.Open);
byte[] buffer = new byte[(int)fs.Length];
fs.Read(buffer, 0, buffer.Length);
fs.Close();
return buffer;
}
public void LoadAssembly(string path, string typeName)
{
try
{
if (_domain == null)
throw new ArgumentNullException("_domain does not exist.");
byte[] Assembly_data = loadFile(path);
byte[] Symbol_data = loadFile(path.Replace(".dll", ".pdb"));
_assembly = _domain.Load(Assembly_data, Symbol_data);
//_assembly = _domain.Load(AssemblyName.GetAssemblyName(path));
_type = _assembly.GetType(typeName);
}
catch (Exception ex)
{
throw new InvalidOperationException(ex.ToString());
}
}
private Type _type;
public Type Type
{
get
{
return _type;
}
}
public void CreateInstanceAndUnwrap(string typeName)
{
_instance = _domain.CreateInstanceAndUnwrap(_assembly.FullName, typeName);
}
}
错误;说我的大会不是可序列化的。尝试添加[Serializable]标签到我的班级,没有运气。仍在研究修复。
当你看不到它们是如何被使用的时候,似乎事情会变得有点混乱,所以让它变得容易?
private void pictureBox1_Click(object sender, EventArgs e)
{
pd.UnloadDomain();
if (CompileCSharpCode(header + tScript.Text + footer))
{
try
{
pd.CreateDomain("DLLDomain", null);
pd.LoadAssembly("eventHandler.dll", "Events.eventHandler");
pd.CreateInstanceAndUnwrap("Events.eventHandler"); // Assembly not Serializable error!
/*if (pd.type != null)
{
MethodInfo onConnect = pd.type.GetMethod("onConnect");
if (onConnect != null)
{
object result = null;
ParameterInfo[] parameters = onConnect.GetParameters();
object classInstance = Activator.CreateInstance(pd.type, null);
if (parameters.Length == 0)
{
result = pd.type.InvokeMember("onConnect", BindingFlags.InvokeMethod, null, classInstance, null);
//result = onConnect.Invoke(classInstance, null);
}
else
{
object[] parametersArray = new object[] { };
//result = onConnect.Invoke(classInstance, parametersArray);
//result = type.InvokeMember("onConnect", BindingFlags.InvokeMethod, null, classInstance, parametersArray);
}
}
}*/
//assembly = Assembly.LoadFrom(null);
}
catch (Exception er)
{
MessageBox.Show("There was an error executing the script.\n>" + er.Message + "\n - " + er.StackTrace.ToString());
}
finally
{
}
}
}
你的进程已经加载的DLL,你不能overwr迭代它。 – leppie 2013-04-04 15:29:23
您想创建一个新的应用程序域并将该DLL加载到该域中。然后,如果您想重新编译,请将该应用程序域取消。因此,您的主程序运行在一个应用程序域中,并且它创建并管理用于加载正在处理的程序集的其他应用程序域。 – 2013-04-04 15:33:28
你需要PDB文件吗?人们用VS进行调试吗?如果不包含,则设置IncludeDebugInformation = false。 – MrMoDoJoJr 2013-04-04 15:33:40