C# 从资源加载dll失败
我的应用程序依赖于几个DLL。我将它们全部放在资源中,然后在应用程序启动时使用在web上找到的方法加载它们:C# 从资源加载dll失败,c#,wpf,C#,Wpf,我的应用程序依赖于几个DLL。我将它们全部放在资源中,然后在应用程序启动时使用在web上找到的方法加载它们: public static void LoadDllsFromResources() { AppDomain.CurrentDomain.AssemblyResolve += (sender, a) => { string dllName = a.Name.Contains(',')
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:“Set-connectionId引发了
异常。“行号“4”和行位置“37”。-->
System.InvalidCastException:[A]Xceed.Wpf.Toolkit.BusyIndicator无法
被强制转换为[B]Xceed.Wpf.Toolkit.BusyIndicator。A型起源于
'WPFToolkit.Extended,版本=1.7.4644.13122,区域性=中性,
PublicKeyToken=3e4669d2f30244f4'在
字节数组。类型B源于“WPFToolkit.Extended”,
版本=1.7.4644.13122,区域性=中性,
PublicKeyToken=3e4669d2f30244f4'在
字节数组。在
DesktopDashboard.LogoutWindow.System.Windows.Markup.IComponentConnector.Connect(Int32
连接ID,对象目标)位于
MS.Internal.Xaml.Runtime.ClrObjectRuntime.SetConnectionId(对象根、,
Int32 connectionId,对象实例)--内部异常结束
堆栈跟踪--at
System.Windows.Markup.XamlReader.RewrapException(异常e,
IXamlLineInfo lineInfo,Uri baseUri)位于
System.Windows.Markup.WpfXamlLoader.Load(XamlReader-XamlReader,
IXamlObjectWriterFactory writerFactory,布尔型
skipJournaledProperties、Object rootObject、XamlObjectWriterSettings
设置,Uri baseUri)位于
System.Windows.Markup.wpfxamloader.LoadBaml(XamlReader-XamlReader,
布尔skipJournaledProperties,对象rootObject,XamlAccessLevel
访问级别,Uri baseUri)位于
System.Windows.Markup.XamlReader.LoadBaml(Stream,ParserContext
parserContext、对象父对象、布尔closeStream)位于
System.Windows.Application.LoadComponent(对象组件,Uri
资源定位器)
DesktopDashboard.LogoutWindow.InitializeComponent()位于
DesktopDashboard.LogoutWindow..ctor()位于
DesktopDashboard.MainWindow.ContextMenuItemLogout\单击(对象发送者,
路线(东)在
System.Windows.RoutedEventHandlerInfo.InvokeHandler(对象目标,
RoutedEventArgs RoutedEventArgs)位于
System.Windows.EventRoute.InvokeHandlerSimp(对象源、,
RoutedEventArgs参数,布尔值重新分级)位于
System.Windows.UIElement.RaiseEventImpl(DependencyObject发送方,
RoutedEventArgs args)位于
System.Windows.UIElement.RaiseEvent(RoutedEventArgs e)位于
System.Windows.Controls.MenuItem.InvokeClickAfterRender(对象参数)位于System.Windows.Threading.ExceptionWrapper.InternalRealCall(委托 回调,对象args,Int32 numArgs)位于 MS.Internal.Threading.ExceptionFilterHelper.TryCatchWhen(对象 源,委托方法,对象参数,Int32 numArgs,委托 捕手)在 System.Windows.Threading.DispatcherOperation.InvokeImpl()位于 System.Windows.Threading.DispatcherOperation.InvokeInSecurityContext(对象 在System.Threading.ExecutionContext.runTryCode(对象 用户数据)在 System.Runtime.CompilerServices.RuntimeHelpers.ExecuteCodeWithGuaranteedCleanup(TryCode 代码,清除代码backoutCode,对象userData)位于 System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext、ContextCallback回调、对象状态)位于 System.Threading.ExecutionContext.Run(ExecutionContext executionContext、ContextCallback回调、对象状态、布尔值 ignoreSyncCtx)在 System.Threading.ExecutionContext.Run(ExecutionContext executionContext、ContextCallback回调、对象状态)位于 System.Windows.Threading.DispatcherOperation.Invoke()位于 位于的System.Windows.Threading.Dispatcher.ProcessQueue() System.Windows.Threading.Dispatcher.WndProcHook(IntPtr-hwnd,Int32 msg、IntPtr wParam、IntPtr lParam、Boolean和handled)位于 MS.Win32.hwndwapper.WndProc(IntPtr-hwnd、Int32-msg、IntPtr-wParam、, 在 位于的MS.Win32.HwndSubclass.DispatcherCallbackOperation(对象o) System.Windows.Threading.ExceptionWrapper.InternalRealCall(委托 回调,对象args,Int32 numArgs)位于 MS.Internal.Threading.ExceptionFilterHelper.TryCatchWhen(对象 源,委托方法,对象参数,Int32 numArgs,委托 捕手)在 System.Windows.Threading.Dispatcher.InvokeImpl(DispatcherPriority 优先级、TimeSpan超时、委托方法、对象参数、Int32 在MS.Win32.HwndSubclass.SubclassWndProc(IntPtr hwnd, Int32 msg、IntPtr wParam、IntPtr lParam)位于 MS.Win32.unsafentivemethods.DispatchMessage(MSG&MSG)位于 System.Windows.Threading.Dispatcher.PushFrameImpl(DispatcherFrame 帧)在 System.Windows.Threading.Dispatcher.PushFrame(DispatcherFrame框架)
位于System.Windows.Application.RunDispatcher(对象忽略)处 System.Windows.Application.RunInternal(窗口窗口)位于 System.Windows.Application.Run(窗口)在 System.Windows.Application.Run()位于 DesktopDashboard.App.Main(字符串[]args)
假设您在visual studio上,您可以直接从IDE将它们添加到项目中 假设您在visual studio上,您可以直接从IDE将它们添加到项目中 您的代码多次加载同一程序集。当您使用Assembly.Load(字节[])时,这是一个问题,CLR无法帮助您确定已加载程序集。技术术语是在没有“加载上下文”的情况下加载这样的程序集。接下来出现的问题是,同一类型不再兼容,类型标识不仅包括namspace名称和类型名称,还包括它来自的程序集
您的工作是确保在请求相同的程序集时返回完全相同的程序集引用。最好的做法是保留一个跟踪此类程序集的
字典。您的代码多次加载同一个程序集。