我在同一个解决方案中将几个对话框表单从一个类库移动到另一个(拖放)(两者都是 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 的数量。
我相信这个问题对于我来说仍然是独一无二的,但有人可能会觉得这很有用。
谢谢。