如果我使用
Assembly assembly = Assembly.LoadFrom(file);
我需要将它加载到一个新的appdomain.
我似乎找到的是如何在Assembly中创建实例的示例,
有没有办法加载整个装配.
我需要的是:
(1) load the assembly into a new AppDomain from a file . (2) extract an embedded resource (xml file) from the Dll . (3) extract a type of class which implements an interface (which i kNow the interface type) . (4) unload the entire appdomain in order to free the file .
2-4不是问题
我似乎无法找到如何将程序集加载到一个新的AppDomin,只有例子
创建实例,它给了我一个来自Dll的类的实例.
我需要整个事情.
喜欢这个问题:Create实例的另一个例子.
解决方法
最基本的多域方案是
static void Main() { AppDomain newDomain = AppDomain.CreateDomain("New Domain"); newDomain.ExecuteAssembly("file.exe"); AppDomain.Unload(newDomain); }
在单独的域上调用ExecuteAssembly是方便的,但不提供与域本身交互的能力.它还要求目标程序集是可执行文件,并强制调用者进入单个入口点.要结合一些灵活性,您还可以将字符串或args传递给.exe.
我希望这有帮助.
扩展:尝试类似下面的内容
AppDomainSetup setup = new AppDomainSetup(); setup.AppDomainInitializer = new AppDomainInitializer(ConfigureAppDomain); setup.AppDomainInitializerArguments = new string[] { unkNownAppPath }; AppDomain testDomain = AppDomain.CreateDomain("test",AppDomain.CurrentDomain.Evidence,setup); AppDomain.Unload(testDomain); File.Delete(unkNownAppPath);
AppDomain可以在哪里初始化,如下所示
public static void ConfigureAppDomain(string[] args) { string unkNownAppPath = args[0]; AppDomain.CurrentDomain.DoCallBack(delegate() { //check that the new assembly is signed with the same public key Assembly unkNownAsm = AppDomain.CurrentDomain.Load(AssemblyName.GetAssemblyName(unkNownAppPath)); //get the new assembly public key byte[] unkNownKeyBytes = unkNownAsm.GetName().GetPublicKey(); string unkNownKeyStr = BitConverter.ToString(unkNownKeyBytes); //get the current public key Assembly asm = Assembly.GetExecutingAssembly(); AssemblyName aname = asm.GetName(); byte[] pubKey = aname.GetPublicKey(); string hexKeyStr = BitConverter.ToString(pubKey); if (hexKeyStr == unkNownKeyStr) { //keys match so execute a method Type classtype = unkNownAsm.GetType("namespace.classname"); classtype.InvokeMember("MethodNametoInvoke",BindingFlags.InvokeMethod,null,null); } }); }
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。