在对话框InitializeComponent中的例外“string不能有零长度”

我将同一个解决scheme中的几个对话框窗体从一个类库移到另一个(拖放)(都是c#类库)。 然后在运行时,我开始在myform.Designer.csInitializeComponent方法中移动和以前存在的forms在该目标dll中类似

 this.pictureBox1.Image = global::mydll.Properties.Resources.Webster; 

例外是:

string不能有零长度。

有时表单会在第一次正确加载,但不会在此之后加载。

你有没有把表格从一个项目移到另一个项目的问题?

我没有更新所有命名空间使用目标dll命名空间。

– 从事件查看器

 Message: String cannot have zero length. Source: mscorlib TraceStack: at System.Reflection.RuntimeAssembly._nLoad(AssemblyName fileName, String codeBase, Evidence assemblySecurity, RuntimeAssembly locationHint, StackCrawlMark& stackMark, IntPtr pPrivHostBinder, Boolean throwOnFileNotFound, Boolean forIntrospection, Boolean suppressSecurityChecks) at System.Reflection.RuntimeAssembly.InternalGetSatelliteAssembly(String name, CultureInfo culture, Version version, Boolean throwOnFileNotFound, StackCrawlMark& stackMark) at System.Resources.ManifestBasedResourceGroveler.GetSatelliteAssembly(CultureInfo lookForCulture, StackCrawlMark& stackMark) at System.Resources.ManifestBasedResourceGroveler.GrovelForResourceSet(CultureInfo culture, Dictionary`2 localResourceSets, Boolean tryParents, Boolean createIfNotExists, StackCrawlMark& stackMark) at System.Resources.ResourceManager.InternalGetResourceSet(CultureInfo requestedCulture, Boolean createIfNotExists, Boolean tryParents, StackCrawlMark& stackMark) 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 Common.Properties.Resources.get_License() in E:\WORK\ProjectOne\Common\Properties\Resources.Designer.cs:line 146 at Project.ONE.Common.ProgressDialog.InitializeComponent() in E:\WORK\ProjectOne\Common\ProgressDialog.Designer.cs:line 100 at Project.ONE.Common.ProgressDialog..ctor(String caption) in E:\WORK\ProjectOne\Common\ProgressDialog.cs:line 60 at Start.CSCom.start() in E:\WORK\ProjectOne\Addin\CSCom.cs:line 326 at Start.Connect.ButtonStartClicked(IRibbonControl control) in E:\WORK\ProjectOne\Addin\Connect.cs:line 464. 

– 解决了

按照Avi的指示,我启用了“第一次机会例外”,并在下面的Assembly Assembly代码中find了问题(显然这是试图加载程序集并且没有这样做):

 currentDomain.AssemblyResolve += new ResolveEventHandler(currentDomain_AssemblyResolve); 

.. ..

  Assembly currentDomain_AssemblyResolve(object sender, ResolveEventArgs args){ //This handler is called only when the common language runtime tries to bind to the assembly and fails. //Retrieve the list of referenced assemblies in an array of AssemblyName. Assembly MyAssembly, objExecutingAssemblies; string strTempAssmbPath = ""; objExecutingAssemblies = Assembly.GetExecutingAssembly(); AssemblyName[] arrReferencedAssmbNames = objExecutingAssemblies.GetReferencedAssemblies(); //Loop through the array of referenced assembly names. foreach (AssemblyName strAssmbName in arrReferencedAssmbNames) { //Check for the assembly names that have raised the "AssemblyResolve" event. if (strAssmbName.FullName.Substring(0, strAssmbName.FullName.IndexOf(",")) == args.Name.Substring(0, args.Name.IndexOf(","))) { //Build the path of the assembly from where it has to be loaded. //The following line is probably the only line of code in this method you may need to modify: RegistryKey regkey = Registry.LocalMachine.OpenSubKey(@"Software\ProjectONE\addin"); strTempAssmbPath = regkey.GetValue("DllLocation").ToString(); if (strTempAssmbPath.EndsWith("\\")) strTempAssmbPath += "\\"; strTempAssmbPath += args.Name.Substring(0, args.Name.IndexOf(",")) + ".dll"; break; } } //Load the assembly from the specified path. MyAssembly = Assembly.LoadFrom(strTempAssmbPath); //Return the loaded assembly. return MyAssembly; } 

我完全删除了“Assembly Resolve”代码,因为我将目标从一个类库移到另一个类库是为了减less我的解决schemedll的数量。

我相信这个问题在我的情况下仍然是独一无二的,但有人可能会觉得这很有用

谢谢。

我记得有同样的问题,而且它也与从Image继承的类有关。

我不记得这个问题的起源是什么,但是我记得这是由于一个内部的例外,而没有得到处理。
原来的异常是不相关的, String cannot have zero length. 所以这个消息可能是误导性的。

尝试以下操作:

  1. 使捕捉所有第一次机会的例外(你可以在我的答案找到说明在这里 )
  2. 调试你的应用程序
  3. 确保没有第一次机会例外(如果这不可能,请确保抓住并处理它们)
  4. 没有未处理的异常之后再次调试您的应用程序。
  5. 你仍然得到错误?

我会尽量四处看看,以便记住这个问题的原因。 如果上面的帮助你,请分享你的洞察力的原因。

由于我遇到了同样的问题,

我发现这个问题(如果有人想知道这个):

您解决事件的最后一行是:

 MyAssembly = Assembly.LoadFrom(strTempAssmbPath); 

但如果没有找到正确的程序集strTempAssmbPath="" < – 空字符串。

因此是例外

这也会发生,如果你添加引用,但从来没有使用类型(它不会被加载到objExecutingAssemblies.GetReferencedAssemblies();