我建議你需要加載一個單獨的程序集與它自己的.config文件,不是嗎? 我這樣做的一種方法是將程序集加載到新的AppDomain中。您可以將該程序集部署到具有所有必需參考的單獨文件夾中。
先設定的AppDomain,在這裏你有一個方法:
AppDomain getAppDomainForAssembly(string assemblypath, string appdomainname)
{
//this._assembly_file = AssemblyFile;
string _assembly_file_name = System.IO.Path.GetFileName(assemblypath);
string _rootpath = System.IO.Path.GetDirectoryName(assemblypath);
//this._assembly_class_name = AssemblyClassNameToInstance;
AppDomainSetup _app_domain_info = new AppDomainSetup();
_app_domain_info.ApplicationBase = _rootpath;
_app_domain_info.PrivateBinPath = _rootpath;
_app_domain_info.PrivateBinPathProbe = _rootpath;
_app_domain_info.ConfigurationFile = _rootpath + @"\app.config"; //Here put the path to the correct .assembly .config file
AppDomain _app_domain = AppDomain.CreateDomain(
appdomainname, null, _app_domain_info);
return _app_domain;
}
然後得到執行方法上裝配的對象的實例:
protected System.Reflection.Assembly _asm_resolve(string assemblyFile)
{
return System.Reflection.Assembly.LoadFrom(assemblyFile);
}
object getInstanceFromAppDomain(ref AppDomain appDomain,
string assemblyPath, string className = null)
{
if (string.IsNullOrEmpty(className))
{
System.Reflection.Assembly assembly = _asm_resolve(assemblyPath);
System.Reflection.MethodInfo method = assembly.EntryPoint;
return appDomain.CreateInstanceFromAndUnwrap(assemblyPath, method.Name);
}
else
{
return appDomain.CreateInstanceFromAndUnwrap(assemblyPath, className);
}
}
即使我們知道對象類型,我們可以創建一個通用類型的方法:
T getInstanceFromAppDomain<T>(ref AppDomain appDomain,
string assemblyPath, string className = null)
{
if (string.IsNullOrEmpty(className))
{
System.Reflection.Assembly assembly = _asm_resolve(assemblyPath);
System.Reflection.MethodInfo method = assembly.EntryPoint;
return (T)appDomain.CreateInstanceFromAndUnwrap(assemblyPath, method.Name);
}
else
{
return (T)appDomain.CreateInstanceFromAndUnwrap(assemblyPath, className);
}
}
然後,調用方法在創建的實例,至極在新的AppDomain執行:
void executeMethod(Type objecttype, string methodname, ref object instancedObject, object[] methodparams)
{
objecttype.InvokeMember(
methodname, System.Reflection.BindingFlags.InvokeMethod, null, instancedObject, methodparams);
}
您可以使用這樣的:
AppDomain newappdomain = getAppDomainForAssembly(filePath, "Loaded.exe.domain");
object loadedexe_object = getInstanceFromAppDomain(ref newappdomain,
filePath);
//If you know the method name to call...
executeMethod(loadedexe_object.GetType(), "methodname", ref loadedexe_object, null);
//or get entry point...
executeMethod(loadedexe_object.GetType(),
_asm_resolve(filePath).EntryPoint.Name, ref loadedexe_object, null);
對於第二個問題,你可以使用了NamedPipes,遠程處理,WCF ... 您需要在同一臺機器上實現進程間通信。 看看MSDN文件建立,覆蓋此方案與WCF的代碼示例 http://msdn.microsoft.com/en-us/library/system.servicemodel.netnamedpipebinding.aspx
見在CodeProject這個樣本,使用Remoting的 Inter-process communication via Remoting
或一些樣品http://tech.pro/tutorial/633/interprocess - 通訊 - 使用 - 命名管道功能於CSHARP –