Gui*_*shy 5 c# assemblies appdomain
我主要通过谷歌阅读了很多东西(不是在这里),但没有找到可以回答我的问题的东西,所以我在这里问。
我确实想添加到“某些东西”(AppDomain我认为),以便我的代码可以在运行时解析如何Assembly.CreateInstance访问我的应用程序编译文件夹之外的特定 DLL。
我真的觉得AppDomain是要使用的类,AppendPrivatePath听起来像要使用的方法,但它现在“已过时”... msdn 建议使用 PrivateBinPath 但据我所知,我必须创建一个新的 AppDomain 并通过我的测试,我感觉Assembly.CreateInstance不在我的新 AppDomain 中寻找引用
一些代码如:
AppDomainSetup domaininfo = new AppDomainSetup();
domaininfo.ApplicationBase = AppDomain.CurrentDomain.BaseDirectory;
domaininfo.PrivateBinPath = "D:\\.....\\bin\\Debug\\";
Evidence adevidence = AppDomain.CurrentDomain.Evidence;
AppDomain newDomain = AppDomain.CreateDomain("FrameworkDomain", adevidence, domaininfo);
Run Code Online (Sandbox Code Playgroud)
有效,但是当我尝试时CreateInstance,我得到了一个 TargetInitation 异常
我也尝试过:
Thread.GetDomain().SetupInformation.PrivateBinPath = "D:\\.....\\bin\\Debug\\";
Run Code Online (Sandbox Code Playgroud)
这听起来“特别”,但对我来说很好,但它不起作用......
我真的觉得我必须给出D:\\.....\\bin\\Debug\\当前 AppDomain 的路径,但它不再可能,因为AppendPrivatePath已经过时了......
有什么帮助吗?
您可以在程序集触发事件时自行解析其他 DLL,以让您知道它找不到类型。
AppDomain.CurrentDomain.ReflectionOnlyAssemblyResolve += MyResolve;
AppDomain.CurrentDomain.AssemblyResolve += MyResolve;
private Assembly MyResolve(Object sender, ResolveEventArgs e) {
Console.Error.WriteLine("Resolving assembly {0}", e.Name);
// Load the assembly from your private path, and return the result
}
Run Code Online (Sandbox Code Playgroud)