短篇小说:资源在运行时使用项目的所有引用,即使它们不需要运行。
长话短说:我正在编写一个在 Autocad 中使用的 .net dll。它是 .net 表单的集合。该项目当然引用了一些 AutoCAD dll。为了帮助测试,我将表单代码与 AutoCAD 代码分离。因此,如果我将 proj 编译为 dll,我可以将它与 autocad 一起使用;如果我将它编译为 exe 表单加载(使用模拟对象作为数据)。一切都很好,除非我使用以下资源将图像添加到表单中:
Image img = global::myproj.Properties.Resources.myimage;
这会在运行时导致此错误:
System.IO.FileNotFoundException was unhandled
Message="Could not load file or assembly 'acmgd, Version=17.1.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies. The system cannot find the file specified."
无法加载该dll,因为我们不在AutoCAD中;但在项目的“exe”形式中不需要。事实上,如果我删除图像(这显然与 AutoCAD 无关)一切正常。这就像项目的“资源”部分,在运行时引用项目中包含的所有 dll ......即使它们没有被使用。
如何在不删除 autocad dll 的情况下将图像添加到我的项目中?我希望图像成为项目的一部分(即在 dll 中,而不是在磁盘上的某个位置)。
顺便说一句:autocad dll 只是一个例子。我想对于需要在某些应用程序中“托管”项目的任何其他 dll,我都会遇到同样的问题。
违规线路:
pictureBox9.Image = global::MyProj.Properties.Resources.tdb2;
完整的堆栈跟踪:
System.IO.FileNotFoundException was unhandled
Message="Could not load file or assembly 'acmgd, Version=17.1.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies. The system cannot find the file specified."
Source="mscorlib"
FileName="acmgd, Version=17.1.0.0, Culture=neutral, PublicKeyToken=null"
FusionLog="=== Pre-bind state information ===\r\nLOG: User = user\r\nLOG: DisplayName = acmgd, Version=17.1.0.0, Culture=neutral, PublicKeyToken=null\n (Fully-specified)\r\nLOG: Appbase = file:<full path>/\r\nLOG: Initial PrivatePath = NULL\r\nCalling assembly : MyProj, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null.\r\n===\r\nLOG: This bind starts in default load context.\r\nLOG: No application configuration file found.\r\nLOG: Using machine configuration file from C:\\Windows\\Microsoft.NET\\Framework\\v2.0.50727\\config\\machine.config.\r\nLOG: Policy not being applied to reference at this time (private, custom, partial, or location-based assembly bind).\r\nLOG: Attempting download of new URL file:<full path>/acmgd.DLL.\r\nLOG: Attempting download of new URL file:<full path>/acmgd/acmgd.DLL.\r\nLOG: Attempting download of new URL file:<full path>/acmgd.EXE.\r\nLOG: Attempting download of new URL file:<full path>/acmgd/acmgd.EXE.\r\n"
StackTrace:
at System.ModuleHandle.ResolveMethod(Int32 methodToken, RuntimeTypeHandle* typeInstArgs, Int32 typeInstCount, RuntimeTypeHandle* methodInstArgs, Int32 methodInstCount)
at System.ModuleHandle.ResolveMethodHandle(Int32 methodToken, RuntimeTypeHandle[] typeInstantiationContext, RuntimeTypeHandle[] methodInstantiationContext)
at System.Reflection.CustomAttributeData..ctor(Module scope, CustomAttributeRecord caRecord)
at System.Reflection.CustomAttributeData.GetCustomAttributes(Module module, Int32 tkTarget)
at System.Reflection.CustomAttributeData.GetCustomAttributes(Assembly target)
at System.Resources.ResourceManager.GetNeutralResourcesLanguage(Assembly a, UltimateResourceFallbackLocation& fallbackLocation)
at System.Resources.ResourceManager.InternalGetResourceSet(CultureInfo culture, Boolean createIfNotExists, Boolean tryParents)
at System.Resources.ResourceManager.GetObject(String name, CultureInfo culture, Boolean wrapUnmanagedMemStream)
at System.Resources.ResourceManager.GetObject(String name, CultureInfo culture)
at MyProj.Properties.Resources.get_tdb2() in <full path>Resources.Designer.cs:line 93
at MyProj.MyForm.InitializeComponent() in <full path>\MyForm.Designer.cs:line 524
at MyProj.MyForm..ctor() in <full path>\MyForm.cs:line 28
at MyProj.MyForm.tabPage_Enter(Object sender, EventArgs e) in <full path>\MyForm.cs:line 121
at System.Windows.Forms.Control.OnEnter(EventArgs e)
at System.Windows.Forms.TabPage.OnEnter(EventArgs e)
at System.Windows.Forms.TabPage.FireEnter(EventArgs e)
at System.Windows.Forms.TabControl.OnSelected(TabControlEventArgs e)
at System.Windows.Forms.TabControl.WmSelChange()
at System.Windows.Forms.TabControl.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.SendMessage(HandleRef hWnd, Int32 msg, IntPtr wParam, IntPtr lParam)
at System.Windows.Forms.Control.SendMessage(Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.Control.ReflectMessageInternal(IntPtr hWnd, Message& m)
at System.Windows.Forms.Control.WmNotify(Message& m)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ScrollableControl.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.CallWindowProc(IntPtr wndProc, IntPtr hWnd, Int32 msg, IntPtr wParam, IntPtr lParam)
at System.Windows.Forms.NativeWindow.DefWndProc(Message& m)
at System.Windows.Forms.Control.DefWndProc(Message& m)
at System.Windows.Forms.Control.WmMouseDown(Message& m, MouseButtons button, Int32 clicks)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.TabControl.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.Run(Form mainForm)
at PidXp.MyForm.Main() in <full path>\MyForm.cs:line 23
at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
(替换了一些路径/名称)