VS2022中引用DLL的Specific Version属性不符合预期问题咨询
VS2022中DLL引用Specific Version属性行为异常问题
问题详情
使用VS2022开发时,发现DLL引用的Specific Version属性行为不符合预期。从属性名称的字面意思判断,程序编译完成后,即使替换版本与原引用DLL不同的新版本DLL,程序也应该能正常加载运行——这个逻辑在过去是生效的,不确定是否有变更。
之前参考过Stack Overflow的旧帖,最新评论建议开新帖咨询。相关属性说明截图如下:
我的exe部署在900多台设备上,长期采用Xcopy方式管理应用,所有应用集中在少数目录中。为了避免GAC、缓存等因素的影响,我实现了AppDomain.CurrentDomain.AssemblyResolve事件处理函数,通过递归遍历指定目录查找DLL。该逻辑在大部分场景下正常,但仍会出现「找不到DLL」的错误,且错误发生在Sub Main()启动前。推测是应用与DLL同目录但版本不符导致识别异常。重新移除并添加DLL引用可以解决问题,但我希望通过设置Specific Version属性来避免重新编译所有引用该DLL的应用。
相关代码
事件注册代码
AddHandler AppDomain.CurrentDomain.AssemblyResolve, AddressOf ResolveAssembly
解析函数代码
Public Function ResolveAssembly(ByVal sender As Object, ByVal args As ResolveEventArgs, Optional ByVal pPath As String = Nothing) As Reflection.Assembly 'BuildLogOutput("ResolveAssembly - " & args.Name) Dim resourceName As String = New Reflection.AssemblyName(args.Name).Name & ".dll" If resourceName.Contains("&") Then Return Nothing If resourceName.ToLower.Contains(".resources.") Then Return Nothing Dim AssemblyPath As String = "" pPath = DLLSearchPath(pPath) Dim returnedassembly As Reflection.Assembly = Nothing If pPath.Length > 0 Then AssemblyPath = Path.Combine(pPath, resourceName) If IO.File.Exists(AssemblyPath) Then returnedassembly = Reflection.Assembly.LoadFrom(AssemblyPath) Else returnedassembly = ResolveAssembly(sender, args, pPath) End If Else Throw New IO.FileNotFoundException("ResolveAssembly - Could not load Assembly: " & resourceName, AssemblyPath) End If If returnedassembly Is Nothing Then Throw New Exception("ResolveAssembly - Unexpected failure to load: " & AssemblyPath) End If Return returnedassembly End Function Public Function DLLSearchPath(Optional ByVal LastPath As String = "") As String Dim PathList() As String = Environment.GetEnvironmentVariable("Path").Split(";"c) If LastPath = "" Then Return AppPath ElseIf LastPath = AppPath Then Return SystemPath ElseIf LastPath = SystemPath Then Return "c:\user\braums\com" ElseIf LastPath = "c:\user\braums\com" Then If PathList.Length > 0 Then Return PathList(0) Else Return "" End If Else For xLoop As Integer = 0 To PathList.Count - 1 If LastPath = PathList(xLoop) Then If xLoop + 1 <= PathList.Count - 1 Then If PathList(xLoop + 1).ToLower = SystemPath.ToLower OrElse PathList(xLoop + 1).ToLower = "c:\user\braums\com" OrElse PathList(xLoop + 1).ToLower = AppPath.ToLower Then LastPath = PathList(xLoop + 1) Continue For End If Return PathList(xLoop + 1) End If End If Next End If Return "" End Function
内容的提问来源于stack exchange,提问作者Paul
相关产品推荐
相关产品推荐

