我目前完全被我遇到的问题困惑不解。我正在为另一个提供公共.NET API的应用程序编写一个插件。我创建了一个名为Room
的类,我正在使用PropertyGrid来允许用户查看和编辑Room
实例的属性。一些属性仅限于一组标准值。因此,我使用GetStandardValues()
覆盖的自定义TypeDescriptors来获取属性网格,以显示这些属性的下拉列表。InvalidCastException将对象转换为它自己的类型
这一切都工作得很好。我得到的下降,我可以编辑值没有问题。然而,现在由于某种原因,当我选择Room
时,PropertyGrid将类型描述符的属性显示为黑盒子。
如果我点击框会变成白色,我得到一个闪烁的光标,但我无法输入任何内容。如果我再选择另一间我的程序有以下例外崩溃:
System.InvalidCastException was caught
Message=Unable to cast object of type 'DVAMC.Room' to type 'DVAMC.Room'.
Source=DVAMC
StackTrace:
at DVAMC.BuildingTypeConverter.GetStandardValuesSupported(ITypeDescriptorContext context) in C:\Documents and Settings\eric.anastas\My Documents\_SVN WC\DVAMC Working\BuildingTypeConverter.cs:line 14
at System.Windows.Forms.PropertyGridInternal.GridEntry.get_Flags()
at System.Windows.Forms.PropertyGridInternal.GridEntry.get_NeedsDropDownButton()
at System.Windows.Forms.PropertyGridInternal.PropertyDescriptorGridEntry.get_NeedsDropDownButton()
at System.Windows.Forms.PropertyGridInternal.PropertyGridView.SelectRow(Int32 row)
at System.Windows.Forms.PropertyGridInternal.PropertyGridView.SelectGridEntry(GridEntry gridEntry, Boolean fPageIn)
at System.Windows.Forms.PropertyGridInternal.PropertyGridView.GridPositionData.Restore(PropertyGridView gridView)
at System.Windows.Forms.PropertyGridInternal.PropertyGridView.Refresh(Boolean fullRefresh, Int32 rowStart, Int32 rowEnd)
at System.Windows.Forms.PropertyGridInternal.PropertyGridView.Refresh()
at System.Windows.Forms.PropertyGrid.Refresh(Boolean clearCached)
at System.Windows.Forms.PropertyGrid.set_SelectedObjects(Object[] value)
at System.Windows.Forms.PropertyGrid.set_SelectedObject(Object value)
at DVAMC.RoomDetailsForm.set_RoomDetailsSelectedRoom(Room value) in C:\Documents and Settings\eric.anastas\My Documents\_SVN WC\DVAMC Working\RoomDetailsForm.cs:line 115
at DVAMC.RoomDetailsForm.roomListTreeView_SelectionChanged(Object sender, EventArgs e) in C:\Documents and Settings\eric.anastas\My Documents\_SVN WC\DVAMC Working\RoomDetailsForm.cs:line 159
at BrightIdeasSoftware.ObjectListView.OnSelectionChanged(EventArgs e)
at BrightIdeasSoftware.ObjectListView.HandleApplicationIdle(Object sender, EventArgs e)
at System.Windows.Forms.Application.ThreadContext.System.Windows.Forms.UnsafeNativeMethods.IMsoComponent.FDoIdle(Int32 grfidlef)
at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.RunDialog(Form form)
at System.Windows.Forms.Form.ShowDialog(IWin32Window owner)
at System.Windows.Forms.Form.ShowDialog()
at DVAMC.RoomDetailsCmd.Execute(ExternalCommandData commandData, String& message, ElementSet elements) in C:\Documents and Settings\eric.anastas\My Documents\_SVN WC\DVAMC Working\RoomDetailsCmd.cs:line 44
InnerException:
在堆栈跟踪点我BuildingTypeConverter.GetStandardValuesSupported()方法(如下所示)中的最后一项。
GetStandardValuesSupported(System.ComponentModel.ITypeDescriptorContext context)
{
Room r = (Room)context.Instance; //this is line 14 referenced by the InvalidCastException's stack trace
if (r.IsLinked)
{
return true;
}
else
{
return false;
}
}
现在,如果我在设置上面一行14断点并尝试调试调试器在断点处不破。另外,如果在强制转换之前添加任意代码,则InvalidCastException中的堆栈跟踪总是似乎引用GetStandardValues()的第一行,而不管它是什么。例如,我尝试了以下。
public override bool GetStandardValuesSupported(System.ComponentModel.ITypeDescriptorContext context)
{
string s = "hello world"; //FIRST LINE
int i = 0;
Room r = (Room)context.Instance;
.....
我还是得到了InvalidCastException。然而,它是堆栈跟踪参考上面的第一行,我初始化string s
。另外,如果我试图在第一行设置断点,它也不会被触发。
就像我之前说过的,这个是工作在一天前很好。我甚至尝试回滚到SVN存储库中的以前版本。我已经回到第一次修订版了,我创建了自定义类型描述符类,但仍遇到InvalidCastExceptions
的问题。有谁知道发生了什么事?
我最近被困在一个类似的方式,事实证明,这与设计师生成的代码相关,当我命名一个属性时,这些代码碰到了 – 2010-10-09 00:33:01
Humm有趣的东西。然而即使我回滚(包括设计器代码)到一个正在工作的版本,它仍然不起作用。尽管如此,我确实有一些与我的课程名称相同的属性。 – 2010-10-09 00:47:17