我的应用程序依赖于一些DLL。我将它们全部放在资源中,而不是在应用程序启动时,我使用我在网上找到的方法加载它们:
public static void LoadDllsFromResources()
{
AppDomain.CurrentDomain.AssemblyResolve += (sender, a) =>
{
string dllName = a.Name.Contains(',')
? a.Name.Substring(0, a.Name.IndexOf(','))
: a.Name.Replace(".dll", "");
dllName = dllName.Replace(".", "_");
if (dllName.EndsWith("_resources")) return null;
System.Resources.ResourceManager rm =
new System.Resources.ResourceManager(
"DesktopDashboard" + ".Properties.Resources",
System.Reflection.Assembly.GetExecutingAssembly());
byte[] bytes = (byte[])rm.GetObject(dllName);
return System.Reflection.Assembly.Load(bytes);
};
}
在我尝试添加WPFToolkitExtended.dll之前,它对我来说很好。比我的应用程序抛出一个错误。是什么让这个DLL如此特别?
System.Windows.Markup.XamlParseException:'设置connectionId扔了一个 。例外'行号' 4'和线位置' 37' ---> System.InvalidCastException:[A] Xceed.Wpf.Toolkit.BusyIndicator不能 被投射到[B] Xceed.Wpf.Toolkit.BusyIndicator。 A型起源于 ' WPFToolkit.Extended,Version = 1.7.4644.13122,Culture = neutral, 公钥= 3e4669d2f30244f4'在上下文中'LoadNeither'在一个 字节数组。类型B源自' WPFToolkit.Extended, 版本= 1.7.4644.13122,文化=中立, 公钥= 3e4669d2f30244f4'在上下文中'LoadNeither'在一个 字节数组。在 DesktopDashboard.LogoutWindow.System.Windows.Markup.IComponentConnector.Connect(的Int32 connectionId,Object target)at MS.Internal.Xaml.Runtime.ClrObjectRuntime.SetConnectionId(Object root, Int32 connectionId,Object instance)---内部异常结束 堆栈跟踪--- at System.Windows.Markup.XamlReader.RewrapException(Exception e, IXamlLineInfo lineInfo,Uri baseUri)at System.Windows.Markup.WpfXamlLoader.Load(XamlReader xamlReader, IXamlObjectWriterFactory writerFactory,Boolean skipJournaledProperties,Object rootObject,XamlObjectWriterSettings 设置,Uri baseUri)at System.Windows.Markup.WpfXamlLoader.LoadBaml(XamlReader xamlReader, Boolean skipJournaledProperties,Object rootObject,XamlAccessLevel accessLevel,Uri baseUri)at System.Windows.Markup.XamlReader.LoadBaml(Stream stream,ParserContext parserContext,Object parent,Boolean closeStream)at System.Windows.Application.LoadComponent(Object component,Uri resourceLocator) DesktopDashboard.LogoutWindow.InitializeComponent()at DesktopDashboard.LogoutWindow..ctor()at DesktopDashboard.MainWindow.ContextMenuItemLogout_Click(Object sender, RoutedEventArgs e)at System.Windows.RoutedEventHandlerInfo.InvokeHandler(Object target, RoutedEventArgs routedEventArgs)at System.Windows.EventRoute.InvokeHandlersImpl(Object source, RoutedEventArgs args,Boolean reRaised)at System.Windows.UIElement.RaiseEventImpl(DependencyObject sender, RoutedEventArgs args)at System.Windows.UIElement.RaiseEvent(RoutedEventArgs e)at System.Windows.Controls.MenuItem.InvokeClickAfterRender(Object arg)
在System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate 回调,对象args,Int32 numArgs)at MS.Internal.Threading.ExceptionFilterHelper.TryCatchWhen(对象 source,Delegate方法,Object args,Int32 numArgs,Delegate catchHandler)at System.Windows.Threading.DispatcherOperation.InvokeImpl()at System.Windows.Threading.DispatcherOperation.InvokeInSecurityContext(对象 在System.Threading.ExecutionContext.runTryCode(Object。) userData)at System.Runtime.CompilerServices.RuntimeHelpers.ExecuteCodeWithGuaranteedCleanup(TryCode 代码,CleanupCode backoutCode,Object userData)at System.Threading.ExecutionContext.RunInternal(执行上下文 executionContext,ContextCallback回调,对象状态)at System.Threading.ExecutionContext.Run(执行上下文 executionContext,ContextCallback回调,对象状态,布尔值 ignoreSyncCtx)at System.Threading.ExecutionContext.Run(执行上下文 executionContext,ContextCallback回调,对象状态)at System.Windows.Threading.DispatcherOperation.Invoke()at System.Windows.Threading.Dispatcher.ProcessQueue()at System.Windows.Threading.Dispatcher.WndProcHook(IntPtr hwnd,Int32 msg,IntPtr wParam,IntPtr lParam,Boolean&处理) MS.Win32.HwndWrapper.WndProc(IntPtr hwnd,Int32 msg,IntPtr wParam, IntPtr lParam,Boolean&处理) MS.Win32.HwndSubclass.DispatcherCallbackOperation(Object o)at System.Windows.Threading.ExceptionWrapper.InternalRealCall(代表 回调,对象args,Int32 numArgs)at MS.Internal.Threading.ExceptionFilterHelper.TryCatchWhen(对象 source,Delegate方法,Object args,Int32 numArgs,Delegate catchHandler)at System.Windows.Threading.Dispatcher.InvokeImpl(的DispatcherPriority 优先级,TimeSpan超时,委托方法,对象args,Int32 numArgs)在MS.Win32.HwndSubclass.SubclassWndProc(IntPtr hwnd, Int32 msg,IntPtr wParam,IntPtr lParam)at MS.Win32.UnsafeNativeMethods.DispatchMessage(MSG& msg)at at System.Windows.Threading.Dispatcher.PushFrameImpl(DispatcherFrame 框架) System.Windows.Threading.Dispatcher.PushFrame(DispatcherFrame框架)
在System.Windows.Application.RunDispatcher(Object ignore)at System.Windows.Application.RunInternal(窗口窗口)at System.Windows.Application.Run(窗口窗口)at System.Windows.Application.Run()at DesktopDashboard.App.Main(String [] args)
答案 0 :(得分:3)
您的代码多次加载相同的程序集。使用Assembly.Load(byte [])时出现问题,CLR无法帮助您确定已加载程序集。技术术语是在没有“加载上下文”的情况下加载这样的组件。接下来出现的问题是同一类型不再兼容,类型标识不仅包括namspace名称和类型名称,还包括它来自的程序集。
确保在请求相同的程序集时返回完全相同的程序集引用是您的工作。最好的办法是保持跟踪此类装配的Dictionary<string, Assembly>
。
答案 1 :(得分:0)
假设您使用的是visual studio,可以直接从IDE中将它们添加到项目中。 Look here