0
我正在使用的應用程序具有一個名爲RSChild的通用父窗體,用於執行一些操作,具體取決於它所包含的控件是否在MdiTabManager中或內部它自己的模態形式。然後,實際的用戶控件包含在從被調用的對象編輯(我們允許編輯的對象)接口繼承。在我的代碼中,我正在這樣做。VB .NET DirectCast和類型反射
Public Function doesTabExist(ByVal id As Integer, ByVal recordType As Enums.eRecordType) As Boolean
Dim alikePages As Object = (From tabs In DirectCast(Control.FromHandle(MainForm.SharedHandle), MainForm).XtraTabbedMdiManager1.Pages Where DirectCast(tabs.MdiChild, RSChild).RSObject.RecordType = recordType Select tabs)
For Each page As DevExpress.XtraTabbedMdi.XtraMdiTabPage In alikePages
Select Case recordType
Case Enums.eRecordType.Doctor
If id = DirectCast(castTabPageToRSChild(page).RSObject, UI.Doctor).ID Then
pageToActive(page)
Return True
End If
'rest of the cases so the case block is repeated 10 times'
End Function
而且我castTabPageToRSChild(頁)是一個lambda功能這樣的
Dim castTabPageToRSChild As Func(Of DevExpress.XtraTabbedMdi.XtraMdiTabPage, RSChild) = Function(page) DirectCast(page.MdiChild, RSChild)
所以我的問題是,我有大約10 case語句,都是因爲我似乎無法找到一種方法來使用反射來獲取RSObject對象的底層類型。所以我有整個If塊重複一遍又一遍。我試過castTabPageToRSChild(page)RSObject.GetType
並在DirectCast
中使用它,我也嘗試創建另一個與之分開的對象,並做同樣的事情。
我的代碼按預期工作我只是想看看是否有一種我沒有很多複製代碼的方式。我的目標是做類似
For Each page As XtraMdiTabPage In alikePages
If id = DirectCast(castTabPageToRSchild(page).RSObject, castTabPageToRSChild(page).RSObject.GetType).Id Then Return True
Next
但是我有一種感覺,這是不可能的,因爲的DirectCast
行爲。
這完全不是問題。我需要能夠將類型投射到。 – msarchet 2010-04-27 20:37:49
對不起,這沒有意義。一個對象已經知道它是什麼類型。你的代碼片段對我來說不夠清晰,無法幫助你做你正在尋找的東西。 – 2010-04-27 20:49:13
RSChild.RSObject是一個包含對象的屬性,我需要爲該直播的第二個參數獲取該對象的類型。 – msarchet 2010-04-27 21:06:48