C# 例外情况;字符串的长度不能为零。”;内部对话框初始化组件
我在同一个解决方案中将几个对话框表单从一个类库移动到另一个类库(拖放)(两者都是c#类库)。然后在运行时,我开始在目标dll中移动的和以前存在的表单的C# 例外情况;字符串的长度不能为零。”;内部对话框初始化组件,c#,windows,winforms,C#,Windows,Winforms,我在同一个解决方案中将几个对话框表单从一个类库移动到另一个类库(拖放)(两者都是c#类库)。然后在运行时,我开始在目标dll中移动的和以前存在的表单的myform.Designer.cs的InitializeComponent方法中出错,该行类似于 this.pictureBox1.Image = global::mydll.Properties.Resources.Webster; 例外情况是: 字符串的长度不能为零 有时表单在第一次加载时会正确加载,但之后不会正确加载 在将表单从一个项目移
myform.Designer.cs
的InitializeComponent
方法中出错,该行类似于
this.pictureBox1.Image = global::mydll.Properties.Resources.Webster;
例外情况是:
字符串的长度不能为零
有时表单在第一次加载时会正确加载,但之后不会正确加载
在将表单从一个项目移动到另一个项目时,您是否遇到过问题
我确实更新了所有名称空间以使用目标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的指示,我启用了“第一次机会异常”,并在下面的程序集解决代码中发现了问题(显然这是试图加载程序集但未能加载该程序集):
。。
..
我刚刚完全删除了“汇编解析”代码,因为我将表单从一个类库移动到另一个类库的目标是减少解决方案DLL的数量
我相信这个问题在我的案例中仍然是独一无二的,但有人可能会发现这很有用
谢谢。我记得有同样的问题,它也与继承自
Image
的类有关
我不记得问题的根源是什么,但我记得这是由于一个未处理的内部异常造成的。原始异常与
字符串的长度不能为零无关。
因此此消息可能有误导性
请尝试以下操作:
如果以上内容对您有所帮助,请分享您对原因的见解。因为我遇到了同样的问题 我发现了问题(如果有人对此感到好奇): 解析事件中的最后一行是:
MyAssembly = Assembly.LoadFrom(strTempAssmbPath);
但是如果找不到正确的程序集
strTempAssmbPath=“
是否检查了资源是否随程序集一起移动?是的,问题是资源已损坏或类似。只有在加载图像时,才会对已经存在的表单以及移动的表单抛出异常(更让我困惑的是,有些表单只会第一次加载)。删除图像并将其再次添加到表单中并不能解决问题。是否将图像设置为正确的编译器选项content
,以便生成过程将其正确移动到项目的bin或release目录中?我已编辑了您的标题。请看“”,其中的共识是“不,他们不应该”。谢谢你为我指出了正确的方向,我补充了上述问题的原因。
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;
}
MyAssembly = Assembly.LoadFrom(strTempAssmbPath);