2

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

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 的指示,我启用了“第一次机会异常”并在下面的 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 的数量。

我相信这个问题对于我来说仍然是独一无二的,但有人可能会觉得这很有用。

谢谢。

4

2 回答 2

1

我记得有同样的问题,它也与继承自的类有关Image

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

尝试以下操作:

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

我将尝试环顾四周,以记住导致此问题的原因。如果以上内容对您有所帮助,请分享您对原因的见解。

于 2013-09-15T05:59:22.773 回答
1

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

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

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

MyAssembly = Assembly.LoadFrom(strTempAssmbPath);

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

因此例外

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

于 2016-04-27T17:20:46.570 回答