WinForm的.Designer.cs代码内抛反射异常
Posted 千年海岩
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了WinForm的.Designer.cs代码内抛反射异常相关的知识,希望对你有一定的参考价值。
今天在项目内一个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.
以上是关于WinForm的.Designer.cs代码内抛反射异常的主要内容,如果未能解决你的问题,请参考以下文章