将程序集加载到appdomain [英] Loading an assembly into an appdomain

查看:67
本文介绍了将程序集加载到appdomain的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

出现了一个新问题:

A new problem has arisen:

For Each key As String In assemblies.Keys

            Dim ad As AppDomain = AppDomainHelper.BuildChildAppDomain(AppDomain.CurrentDomain, key)

            Dim finder As AssemblyHelper.AssemblyResolveFinder = CType(ad.CreateInstanceAndUnwrap(GetType(AssemblyHelper.AssemblyResolveFinder).Assembly.FullName, GetType(AssemblyHelper.AssemblyResolveFinder).FullName), AssemblyHelper.AssemblyResolveFinder)
            finder.DeployPath = Assembly.GetExecutingAssembly.Location.Substring(0, Assembly.GetExecutingAssembly.Location.LastIndexOf("\"c) + 1)
            AddHandler ad.AssemblyResolve, AddressOf finder.AssemblyResolve

            For Each value As String In assemblies(key)

                Dim item As IScanner = CType(ad.CreateInstanceAndUnwrap(key, value), IScanner)

                ListBox1.Items.Add(item)
            Next
            _l.Add(ad)

        Next


该错误发生在Dim item As IScanner = CType(ad.CreateInstanceAndUnwrap(key, value), IScanner),因为它无法加载程序集引用.我已经创建了一个程序集解析类,以便可以从试图将程序集加载到的同一个appdomain中调用evenht:


the error occurs at Dim item As IScanner = CType(ad.CreateInstanceAndUnwrap(key, value), IScanner) as it is not able to load the assemblies references. I have created an assembly resolve class just so that I could call the evenht from within the same appdomain that I''m trying to load the assembly into:

Public Class AssemblyResolveFinder
    Inherits MarshalByRefObject
    Private _deployPath As String

    Public Function AssemblyResolve(sender As Object, args As ResolveEventArgs) As Assembly
        Dim path As String = _deployPath + args.Name + ".dll"
        If File.Exists(path) Then
            Return Assembly.LoadFrom(path)
        End If
        Return Nothing
    End Function

    Public Property DeployPath As String
        Get
            Return _deployPath
        End Get
        Set(value As String)
            _deployPath = value
        End Set
    End Property
End Class



我在_deployPath字符串中尝试了不同的路径.理想情况下,外部程序集可以全部放在与其他任何程序集都不同的单独文件夹中,但无论如何我都无法加载该程序集.

奇怪的是,我已经从另一个在不同appdomain中实例化的类中加载了程序集,以使我可以探查类类型(试图从IScanner中找到这些类型).这种方法的唯一问题是,我不确定在我的appdomain.currentdomain
中没有引用它们的情况下如何与实例化类型通信.
我希望我能解释我的困境.如果不是,那么让我知道,我会尽力澄清

谢谢
Andy



I have tried different paths in the _deployPath string. Ideally the external assemblies could all be in their own single folder away from any other assemblies but I can''t get the assembly to load eitherway.

Bizarly, I have already loaded the assembly from another class I had instansiated in a different appdomain, just so that I could probe the class types (trying to find ones that inheret from IScanner). My only problem with that method is that I am not sure how I would be able to communicate with the instansiated types without a reference to them in my appdomain.currentdomain

I hope I explained my predicament. If not then Let me know and I''ll try to clarify

Thanks
Andy

推荐答案

OIC现在!我以前没有得到它:我无法将程序集中类型的引用传递回我的主appdomain,因为它不知道该类型是什么!它必须保留在它自己的appdomain中,所以我创建了一个包装类,也可以将其加载到appdomain中,因为我知道它是什么类型",然后使用它来加载我的程序集.

我的加载程序类是跨域的,但是只要整个程序集都在域中就没有关系.耶.

现在,我只需要能够杀死程序集(和子线程)以防它们超时,但这是另一个问题

感谢SAKryukov发表我的另一个问题.两者都帮助了

OIC now! I was not getting it before: I cannot pass a reference of the types in the assembly back to my main appdomain as it has no idea what that type is! It must remain in it''s own appdomain so I have created a wrapper class that I can also load into the appdomain, as I know what ''type'' it is, and then use that to load my assemblies.

My loader class is sort of cross domain but is shouldn''t matter as long as the entire assembly is in the domain. yay.

Now I just need to be able to kill the assembly (and sub threads) in case they timeout, but that''s another question

thanks to SAKryukov for posting on my other question. Both have helped

报价:


创建使用可重载插件的WPF应用程序... [^],
AppDomain拒绝加载程序集[^],
使用CodeDom [^]生成代码.


Create WPF Application that uses Reloadable Plugins...[^],
AppDomain refuses to load an assembly[^],
code generating using CodeDom[^].


这篇关于将程序集加载到appdomain的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆