我有一个动态加载以下三个解决方案的应用程序:
- SolutionA which references
X.dll version 3
Z.dll version 1 which references
X.dll version 1
- SolutionB which references
X.dll version 2
- SolutionC which references
X.dll version 1
.
X.dll 包含一些 XAML(取决于我更改文本、背景和前景的 DLL 版本):
<UserControl x:Class="X.MainWindow"
Background="DodgerBlue">
<UserControl.Content>
<Label Foreground="Black" Content="version 1" />
</UserControl.Content>
</UserControl>
.
Z.dll 还包含一些使用 X.dll 的 XAML:
<UserControl x:Class="Z.ZMainWindow">
<StackPanel>
<X:MainWindow />
</StackPanel>
</UserControl>
.
解决方案A 显示 X 版本 3 和 Z 版本 1(显示 X 版本 1)
解决方案 B 显示 X 版本 2
SolutionC 显示 X 版本 1
.
我想要的是始终使用更高版本的 Assembly,这意味着 SolutionA、SolutionB 和 SolutionC 必须始终使用版本 3中的X.dll。
这是我用于 AssemblyResolve 的代码:
private static Assembly OnAssemblyResolveWithDllPaths(object sender, ResolveEventArgs args)
{
try
{
AssemblyName parsedName = new AssemblyName(args.Name);
Version currentVersion = parsedName.Version;
string currentName = parsedName.Name;
string currentCulture = parsedName.CultureName;
byte[] currentPublicKeyToken = parsedName.GetPublicKeyToken();
string assemblyPath = string.Empty;
if (loadedAssemblies.FirstOrDefault(a => a == args.Name) != null)
{
// Assembly has already been loaded
return null;
}
List<string> possibleAssembliesPaths = m_dllPaths.Where(d => d.Contains(parsedName.Name)).ToList();
if (possibleAssembliesPaths != null && possibleAssembliesPaths.Count > 0)
{
if (possibleAssembliesPaths.Count == 1)
{
assemblyPath = possibleAssembliesPaths[0];
}
else
{
foreach (string possibleAssembly in possibleAssembliesPaths)
{
if (File.Exists(possibleAssembly))
{
AssemblyName possibleAssemblyName = AssemblyName.GetAssemblyName(possibleAssembly);
Version possibleVersion = possibleAssemblyName.Version;
string possibleName = possibleAssemblyName.Name;
string possibleCulture = possibleAssemblyName.CultureName;
byte[] possiblePublicKeyToken = possibleAssemblyName.GetPublicKeyToken();
if (currentName == possibleName
&& currentCulture == possibleCulture
&& currentPublicKeyToken.SequenceEqual(possiblePublicKeyToken)
&& currentVersion < possibleVersion)
{
assemblyPath = possibleAssembly;
currentVersion = possibleAssemblyName.Version;
currentName = possibleAssemblyName.Name;
currentCulture = possibleAssemblyName.CultureName;
currentPublicKeyToken = possibleAssemblyName.GetPublicKeyToken();
}
}
}
}
if (!string.IsNullOrEmpty(assemblyPath))
{
loadedAssemblies.Add(args.Name);
return Assembly.LoadFrom(assemblyPath);
}
}
}
catch
{
return null;
}
return null;
}
.
一切正常,直到我的控件名副其实
即(在 Z.dll 中):<Label Foreground="Black" Content="version 1" />
变为<Label Foreground="Black" Content="version 1" x:Name="LabelV1" />
和(在 X.dll 中):<X:MainWindow x:Name="XMainWindow" />
变成<X:MainWindow x:Name="XMainWindow" />
此时,SolutionA 出现异常,在 SolutionA 之前加载 SolutionA 时不显示(SolutionB 从来不是问题,只要在 SolutionA 之前加载 SolutionC,一切正常)。
我看到的是,当我附加到AppDomain.CurrentDomain.AssemblyLoad += CurrentDomain_AssemblyLoad
XAML 时,它们会自动加载他们需要的程序集:
SolutionC's XAML loads X in version 1
SolutionA's XAML loads X in version 3
SolutionA's XAML loads Z in version 1
然后我在 xaml.cs 中看到以下异常:
An exception of type 'System.Exception' occurred in PresentationFramework.dll but was not handled in user code
Additional information: The component 'X.MainWindow' does not have a resource identified by the URI '/X;component/mainwindow.xaml'.
以及 xaml 中的以下内容:
Exception thrown: 'System.Windows.Markup.XamlParseException' in PresentationFramework.dll
Additional information: 'The invocation of the constructor on type 'X.MainWindow' that matches the specified binding constraints threw an exception.' Line number '10' and line position '10'.
.
你有避免这个问题的线索吗?