0

我正在使用WPF/VB.NET开发一个可扩展的软件,它从扩展文件夹加载 dll 并创建它们的实例并从中添加用户控件。但是在某个扩展中,我必须添加另一个程序集的引用,当我加载它时它不起作用。我应该怎么办?

4

1 回答 1

0

我想到了

我的项目从本地扩展文件夹加载程序集(即扩展程序集),但扩展引用的程序集不是。此代码解决了加载扩展的引用程序集的问题(万岁!),有些已由 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\10\UnionMetadata\10.0.17763.0\Windows.winmd</HintPath>
</Reference>

这样我所有的问题都解决了!

于 2020-03-06T07:48:57.760 回答