我在同一解决方案中将几个对话框表单从一个类库移动到另一个类库(拖放)(都是 C# 类库)。然后在运行时,我开始在该目标 dll 中移动的和先前存在的表单的 myform.Designer.cs
的 InitializeComponent
方法内出现错误,行类似于
this.pictureBox1.Image = global::mydll.Properties.Resources.Webster;
异常(exception)情况是:
String cannot have zero length.
有时表单第一次会正确加载,但之后就无法正确加载。
将表单从一个项目转移到另一个项目时是否遇到过问题?
我确实更新了所有命名空间以使用目标 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 Resolve 代码中发现了问题(显然,此尝试加载程序集但未能执行此操作):
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”代码,因为我将表单从一个类库移动到另一个类库的目标是减少解决方案 dll 的数量。
我相信这个问题对于我的情况来说仍然是独特的,但有人可能会发现这很有用。
谢谢。
最佳答案
我记得有同样的问题,而且它也与继承自 Image
的类有关。
我不记得问题的根源是什么,但我记得这是由于未处理的内部异常造成的。
原始异常与字符串长度不能为零
无关,因此此消息可能会产生误导。
尝试以下操作:
- 启用捕获所有第一次机会异常(您可以在我的回答 here 中找到说明)
- 调试您的应用程序。
- 确保不存在第一次机会异常(如果不可能,请确保捕获并处理它们)
- 在没有未处理的异常之后,再次调试您的应用程序。
- 您仍然收到错误吗?
我会尝试环顾四周,以记住此问题的原因是什么。 如果上述内容对您有帮助,请分享您对原因的见解。
关于c# - 对话框 InitializeComponent 内出现异常“字符串长度不能为零”,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18809048/