2010-10-09 95 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(對於其他對象同樣如此)。

相關問題