如何在运行时加载引用另一个程序集的程序集
How to load assembly at runtime which has reference to another assembly
我正在使用 WPF/VB.NET 开发可扩展软件,它从扩展文件夹加载 dll 并创建它们的实例并从中添加用户控件。但是在某个扩展中,我必须添加另一个程序集的引用,当我加载它时它不起作用。我该怎么办?
我想通了
我的项目从本地 Extensions 文件夹加载程序集(即扩展程序集),但扩展引用的程序集不是。此代码解决了加载 扩展的参考程序集 的问题(欢呼!)并且一些已由 GAC
解决
包含在Application.vb
中
Private WithEvents Domain As AppDomain = AppDomain.CurrentDomain
Private Function DomainCheck(ByVal sender As Object, ByVal e As ResolveEventArgs) As Assembly Handles Domain.AssemblyResolve
If e.Name.Contains(".resources") Then
Return Nothing
End If
If Not IO.Directory.Exists(".\ExtReferences") Then
IO.Directory.CreateDirectory(".\ExtReferences")
End If
Dim _Assembly = AppDomain.CurrentDomain.GetAssemblies().FirstOrDefault(Function(a) a.FullName = e.Name)
If _Assembly IsNot Nothing Then Return _Assembly
Dim filename = e.Name.Split(",")(0) + ".dll".ToLower()
Dim asmFile = IO.Path.Combine(".\", "ExtReferences", filename)
Try
Return Assembly.LoadFrom(asmFile)
Catch ex As Exception
End Try
Return Nothing
End Function
我还想出了一个扩展需要访问 WinRT 的特殊情况:(
包含在 .(vb/cs)proj 文件中
<Reference Include="System.Runtime.WindowsRuntime, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>$(MSBuildProgramFiles32)\Reference Assemblies\Microsoft\Framework\.NETCore\v4.5\System.Runtime.WindowsRuntime.dll</HintPath>
</Reference>
<Reference Include="Windows">
<HintPath>$(MSBuildProgramFiles32)\Windows Kits\UnionMetadata.0.17763.0\Windows.winmd</HintPath>
</Reference>
这样我所有的问题都解决了!
我正在使用 WPF/VB.NET 开发可扩展软件,它从扩展文件夹加载 dll 并创建它们的实例并从中添加用户控件。但是在某个扩展中,我必须添加另一个程序集的引用,当我加载它时它不起作用。我该怎么办?
我想通了
我的项目从本地 Extensions 文件夹加载程序集(即扩展程序集),但扩展引用的程序集不是。此代码解决了加载 扩展的参考程序集 的问题(欢呼!)并且一些已由 GAC
解决包含在Application.vb
中Private WithEvents Domain As AppDomain = AppDomain.CurrentDomain
Private Function DomainCheck(ByVal sender As Object, ByVal e As ResolveEventArgs) As Assembly Handles Domain.AssemblyResolve
If e.Name.Contains(".resources") Then
Return Nothing
End If
If Not IO.Directory.Exists(".\ExtReferences") Then
IO.Directory.CreateDirectory(".\ExtReferences")
End If
Dim _Assembly = AppDomain.CurrentDomain.GetAssemblies().FirstOrDefault(Function(a) a.FullName = e.Name)
If _Assembly IsNot Nothing Then Return _Assembly
Dim filename = e.Name.Split(",")(0) + ".dll".ToLower()
Dim asmFile = IO.Path.Combine(".\", "ExtReferences", filename)
Try
Return Assembly.LoadFrom(asmFile)
Catch ex As Exception
End Try
Return Nothing
End Function
我还想出了一个扩展需要访问 WinRT 的特殊情况:(
包含在 .(vb/cs)proj 文件中
<Reference Include="System.Runtime.WindowsRuntime, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>$(MSBuildProgramFiles32)\Reference Assemblies\Microsoft\Framework\.NETCore\v4.5\System.Runtime.WindowsRuntime.dll</HintPath>
</Reference>
<Reference Include="Windows">
<HintPath>$(MSBuildProgramFiles32)\Windows Kits\UnionMetadata.0.17763.0\Windows.winmd</HintPath>
</Reference>
这样我所有的问题都解决了!