2010-10-09 53 views
3

我目前完全被我遇到的问题困惑不解。我正在为另一个提供公共.NET API的应用程序编写一个插件。我创建了一个名为Room的类,我正在使用PropertyGrid来允许用户查看和编辑Room实例的属性。一些属性仅限于一组标准值。因此,我使用GetStandardValues()覆盖的自定义TypeDescriptors来获取属性网格,以显示这些属性的下拉列表。InvalidCastException将对象转换为它自己的类型

这一切都工作得很好。我得到的下降,我可以编辑值没有问题。然而,现在由于某种原因,当我选择Room时,PropertyGrid将类型描述符的属性显示为黑盒子。

alt text

如果我点击框会变成白色,我得到一个闪烁的光标,但我无法输入任何内容。如果我再选择另一间我的程序有以下例外崩溃:

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的问题。有谁知道发生了什么事?

+0

我最近被困在一个类似的方式,事实证明,这与设计师生成的代码相关,当我命名一个属性时,这些代码碰到了 – 2010-10-09 00:33:01

+0

Humm有趣的东西。然而即使我回滚(包括设计器代码)到一个正在工作的版本,它仍然不起作用。尽管如此,我确实有一些与我的课程名称相同的属性。 – 2010-10-09 00:47:17

回答

1

如果堆栈跟踪始终显示相同的行,即使您更改了代码后,该代码也会指示ProperyGrid未运行相同版本的程序集。当你说你放置了一个断点但断点从未被击中时,这一点将得到进一步证实。如果您在Visual Studio的调试器内部运行,我建议您查看输出窗口(Ctrl + W,O),它将列出在运行中加载的所有程序集(及其路径)。我已经看到程序集版本混乱,特别是当程序集在GAC中时,它坚持要加载旧版本的程序集。

+1

是的,我不知道为什么,但在根程序文件夹中有一个单独的DLL文件副本。奇怪的是我的代码似乎是参考了两者。例如,我可以在SelectionChanged事件处理程序中捕获断点,但在设置PropertyGrid.SelectedObject属性后,它不会在GetStandardValues()方法中捕获断点。我删除了额外的DLL副本,它再次运行得很完美。谢谢! – 2010-10-11 18:30:39

+0

+1你刚刚救了我一些重要的时间我的朋友! – Achilles 2011-01-06 15:58:32

0

它可能是这两种类型实际上是不同的 - 例如,如果其中一个是从另一个版本的某个程序集加载而不是另一个类型。我不太确定这是否会发生在您的案件中,但这可能是一个问题。

检查这个最简单的方法是将断点放在抛出异常的地方。然后,您可以查看手表或立即窗口中的两种类型,并查看o1.GetType().Assembly. FullName(对于其他对象同样如此)。

相关问题