InvalidCastException将对象转换为自己的类型

时间:2010-10-09 00:24:41

标签: c# propertygrid .net typedescriptor

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

这一切都很好。我得到了下降,我可以编辑值没问题。然而,由于某种原因,当我选择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的问题。有谁知道发生了什么事?

2 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:0)

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

检查此问题的最简单方法是将断点放在抛出异常的位置。然后你可以看一下手表或即时窗口中的两种类型并查看o1.GetType().Assembly. FullName(对于另一个对象也是如此)。

相关问题