短篇小说:资源在运行时使用项目的所有引用,即使它们不需要运行。
长篇故事: 我正在写一个在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."
因为我们不在autocad中,所以无法加载该dll;但是在项目的“exe”形式中并不需要它。事实上,如果我删除图像(显然与autocad无关)一切正常。它就像项目的“资源”部分,在运行时,引用项目中包含的所有dll ......即使它们没有被使用。
如何在不删除autocad dll的情况下将图像添加到项目中?我希望图像成为项目的一部分(也就是说,在dll中,而不是在磁盘上的某个地方)。
BTW: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()
(替换了一些路径/名称)