WinForm的.Designer.cs代码内抛反射异常

时间:2022-08-02 22:45:38

今天在项目内一个Winform增加控件后,无法打开,抛如下异常。

System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.NullReferenceException: Object reference not set to an instance of an object.
at CM.CTOS.VesselDocument.CovCodControl.InitializeComponent() in C:\Test\TestForm.Designer.cs:line 2547
at CM.CTOS.VesselDocument.CovCodControl..ctor() in C:\Test\TestForm.cs:line 153
at CM.CTOS.VesselDocument.CovCodForm.InitUserControl() in C:\Test\TestForm.cs:line 58
at CM.CTOS.VesselDocument.CovCodForm..ctor() in C:\Test\TestForm.cs:line 43
--- End of inner exception stack trace ---
at System.RuntimeTypeHandle.CreateInstance(RuntimeType type, Boolean publicOnly, Boolean noCheck, Boolean& canBeCached, RuntimeMethodHandle& ctor, Boolean& bNeedSecurityCheck)
at System.RuntimeType.CreateInstanceSlow(Boolean publicOnly, Boolean fillCache)
at System.RuntimeType.CreateInstanceImpl(Boolean publicOnly, Boolean skipVisibilityChecks, Boolean fillCache)
at System.Activator.CreateInstance(Type type, Boolean nonPublic)
at System.Activator.CreateInstance(Type type)
at CM.CTOS.WinUIAdmin.MenuManager.SubMenuClick(Object sender, EventArgs e) in C:\Test\MenuManager.cs:line 267

经检查,出错代码在..Designer.cs:line 2547。此代码是VisualStudio自动生成的,没理由出错。

后来经同事提醒,打开TestForm.resx文件时,VisualStudio提示有重复,原来我新控件后,对控件重命名了(只改了名字的大小写),这样导致了TestForm.resx出现大小写名称不同的项,清除废弃项后,WinForm正常。

这应该是VisualStudio的Bug.