InvalidCastException的铸造对象到其自己的类型 [英] InvalidCastException casting object to its own type

查看:417
本文介绍了InvalidCastException的铸造对象到其自己的类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前完全由我在这个问题百思不得其解。我正在写一个插件,提供了一个公共.NET API其他应用程序。我创建了一个类名为客房和我使用的是PropertyGrid中允许用户查看和编辑客房实例。一些属性被限制为一组标准值。因此,我使用自定义的TypeDescriptors与 GetStandardValues​​()覆盖来获得属性网格显示一个下拉为这些属性。

I'm currently completely baffled by the problem I'm having. I'm writing a plug-in for another application that provides a public .NET API. I've created a class named Room and I am using a PropertyGrid to allow users to see and edit the properties of the Room instances. A few properties are restricted to a set of standard values. Thus I am using custom TypeDescriptors with GetStandardValues() overrides to get the property grid to show a drop down for those properties.

这是所有工作就好了。我得到下拉菜单,我可以编辑值没有问题。然而,现在由于某种原因,当我选择了客房 PropertyGrid中显示属性的类型描述符是一个黑盒子。

This was all working just fine. I was getting drop downs, and I could edit values no problem. Yet now for some reason when I select an Room the PropertyGrid shows the properties with the type descriptors as a black box.

如果我点击盒子它变成白色,我得到一个闪烁的光标,但我不能键入任何东西。如果我再选择另一间以下异常我的程序崩溃了:

If I click on the box it turns white and I get a blinking cursor, yet I can't type anything. If I then select another Room my program crashes with the following exception:

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.GetStandardValues​​Supported()方法中的最后一个项目。

The last item in the stack trace points to my BuildingTypeConverter.GetStandardValuesSupported() method which is shown below.

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和尝试调试调试器在断点处不破。此外,如果我投前添加任意code堆栈跟踪从InvalidCastException的似乎总是参考GetStandardValues​​的第一行(),无论它是什么。比如我尝试以下。

Now if I set a breakpoint at line 14 above and try to debug the debugger does not break at the breakpoint. In addition, if I add arbitrary code before the cast the stack trace from the InvalidCastException always seems to reference to first line of GetStandardValues() regardless of what it is. For example I tried the following.

public override bool GetStandardValuesSupported(System.ComponentModel.ITypeDescriptorContext context)
    {
        string s = "hello world";   //FIRST LINE
        int i = 0;


        Room r = (Room)context.Instance; 

        .....

我还是得到了同样的InvalidCastException的。然而,它的堆栈跟踪参考上面,我初始化字符串s 的第一行。在addtion,如果我想在这第一行设置一个断点,它也不会被触发。

I still got the same InvalidCastException. Yet it's stack trace reference the first line above where I initialize string s. In addtion, if I tried to set a breakpoint on this first line it was also not triggered.

就像我之前说这个的的工作就好了一天左右前。我甚至尝试回滚到previous修订在我的SVN仓库。我已经早在第一次修订是我创建的自定义类型描述符类,但仍运行与 InvalidCastExceptions 的问题。没有人有任何想法是怎么回事?

Like I said before this was working just fine a day or so ago. I've even tried rolling back to previous revisions in my SVN repository. I've gone as far back as the first revision were I created the custom Type Descriptor class but still run into the problem with the InvalidCastExceptions. Does anyone have any idea what's going on?

推荐答案

如果堆栈跟踪总是显示你已经改变了code这将表明对我来说,ProperyGrid没有运行同一版本,即使在同一行组装的。当你说你把一个断点,这进一步证实,但断点永远不会命中。如果您在调试器的Visual Studio内运行,我建议你看看输出窗口(按Ctrl + W,O),其中将列出所有在运行中加载的程序集(其路径)。我所看到的组件版本混乱,特别是当集是在GAC中,它坚持在加载组件的旧版本。

If the stack trace always shows the same line even after you have changed the code that would indicate to me that the ProperyGrid is not running the same version of the assembly. This is further confirmed when you say you put a breakpoint but the breakpoint is never hit. If you are running inside of the debugger for Visual Studio I suggest that you look at the output window (Ctrl+W, O), which will list all of the assemblies (with their path) that are loaded in the run. I have seen assembly version confusion particularly when the assembly is in the GAC, where it insist upon loading an older version of the assembly.

这篇关于InvalidCastException的铸造对象到其自己的类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆