例外“字符串长度不能为零”。内部对话框InitializeComponent

时间:2013-09-15 04:54:28

标签: c# windows winforms

我在同一个解决方案中将几个对话框表单从一个类库移动到另一个类(拖放)(两个都是c#类库)。然后在运行时,我开始在InitializeComponent的{​​{1}}方法中的myform.Designer.cs方法中获取错误,该方法在该目标dll中移动和以前存在的表单类似于

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的指示,我启用了“First Chance Exception”并在下面的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的数量。

我认为这个问题对我来说仍然是独一无二的,但有人可能会发现这个问题很有用。

感谢。

2 个答案:

答案 0 :(得分:1)

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

我不记得问题的根源是什么,但我确实记得它是由于未处理的内部异常造成的。
原始异常与String cannot have zero length.无关,因此此消息可能会产生误导。

尝试以下方法:

  1. 启用捕获所有第一次机会异常(您可以在我的回答中找到说明here
  2. 调试您的应用程序。
  3. 确保没有第一次机会异常(如果不可能,请务必抓住并处理它们)
  4. 在没有未处理的异常后再次调试应用程序。
  5. 你还得错误吗?
  6. 我会试着四处看看,以便记住这个问题的原因。 如果以上内容对您有所帮助,请分享您对原因的见解。

答案 1 :(得分:1)

因为我遇到了同样的问题,

我发现了问题(如果有人对此感到奇怪):

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

MyAssembly = Assembly.LoadFrom(strTempAssmbPath);

但是如果找不到正确的程序集strTempAssmbPath=""< - 空字符串。

因此异常

如果添加了引用,也会发生这种情况,但从未使用过类型(它不会加载到objExecutingAssemblies.GetReferencedAssemblies();中)