0

私のアプリケーションはいくつかのDLLに依存しています。私はそれらをすべてリソースに入れ、アプリケーションの起動時よりも、Webで見つけた方法を使用してそれらをロードします。

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:'SetconnectionIdが例外をスローしました。' 行番号「4」および行位置「37」。---> System.InvalidCastException:[A]Xceed.Wpf.Toolkit.BusyIndi​​catorを[B]Xceed.Wpf.Toolkit.BusyIndi​​catorにキャストできません。タイプAは、バイト配列のコンテキスト「LoadNether」の「WPFToolkit.Extended、Version = 1.7.4644.13122、Culture = neutral、PublicKeyToken=3e4669d2f30244f4」に由来します。タイプBは、バイト配列のコンテキスト「LoadNether」の「WPFToolkit.Extended、Version = 1.7.4644.13122、Culture = neutral、PublicKeyToken=3e4669d2f30244f4」に由来します。DesktopDashboard.LogoutWindow.System.Windows.Markup.IComponentConnector.Connect(Int32 connectionId、Object target)at MS.Internal.Xaml.Runtime.ClrObjectRuntime.SetConnectionId(Object root、
System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback、Object args、Int32 numArgs)at MS.Internal.Threading.ExceptionFilterHelper.TryCatchWhen(Object source、Delegate method、Object args、Int32 numArgs、Delegate catchHandler)at System.Windows .Threading.DispatcherOperation.InvokeImpl()at System.Windows.Threading.DispatcherOperation.InvokeInSecurityContext(Object state)at System.Threading.ExecutionContext.runTryCode(Object userData)at System.Runtime.CompilerServices.RuntimeHelpers.ExecuteCodeWithGuaranteedCleanup(TryCode code、CleanupCode backoutCode 、Object userData)at System.Threading.ExecutionContext.RunInternal(ExecutionContextexecutionContext、ContextCallback callback、Object state)at System.Threading.ExecutionContext.Run(ExecutionContextexecutionContext、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&handled)at MS.Win32.HwndWrapper.WndProc(IntPtr hwnd、Int32 msg、IntPtr wParam、IntPtr )at MS.Win32.HwndSubclass.DispatcherCallbackOperation(Object o)at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback、Object args、Int32 numArgs)at MS.Internal.Threading.ExceptionFilterHelper.TryCatchWhen(Object source、Delegate method、オブジェクト引数、Int32 numArgs、Delegate catchHandler)at System.Windows.Threading.Dispatcher.InvokeImpl(DispatcherPriority priority、TimeSpan timeout、Delegate method、Object args、Int32 numArgs)at MS.Win32.HwndSubclass.SubclassWndProc(IntPtr hwnd、Int32 msg、IntPtr IntPtr lParam)at MS.Win32.UnsafeNativeMethods.DispatchMessage(MSG&msg)at System.Windows.Threading.Dispatcher.PushFrameImpl(DispatcherFrame frame)at System.Windows.Threading.Dispatcher.PushFrame(DispatcherFrame frame)DispatchMessage(MSG&msg)at System.Windows.Threading.Dispatcher.PushFrameImpl(DispatcherFrame frame)at System.Windows.Threading.Dispatcher.PushFrame(DispatcherFrame frame)DispatchMessage(MSG&msg)at System.Windows.Threading.Dispatcher.PushFrameImpl(DispatcherFrame frame)at System.Windows.Threading.Dispatcher.PushFrame(DispatcherFrame frame)
System.Windows.Application.RunDispatcher(オブジェクト無視)at System.Windows.Application.RunInternal(ウィンドウウィンドウ)at System.Windows.Application.Run(ウィンドウウィンドウ)at System.Windows.Application.Run()at DesktopDashboard.App .Main(String [] args)

4

2 に答える 2

3

コードが同じアセンブリを複数回読み込んでいます。これは、Assembly.Load(byte[]) を使用する場合の問題です。CLR は、アセンブリが既に読み込まれていることを把握するのに役立ちません。専門用語は、そのようなアセンブリが「読み込みコンテキスト」なしで読み込まれたということです。次に問題になるのは、同じ型に互換性がなくなったことです。型 ID には、名前空間名と型名だけでなく、元のアセンブリも含まれます。

同じアセンブリが要求されたときに、まったく同じアセンブリ参照を返すようにするのは、あなたの仕事です。最善の方法は、Dictionary<string, Assembly>そのようなアセンブリを追跡する を保持することです。

于 2012-09-21T13:38:54.917 に答える
0

Visual Studio を使用していると仮定すると、IDE から直接プロジェクトに追加できます。ここを見てください

于 2012-09-21T13:05:02.337 に答える