2012-10-23 55 views
0

我面對不明確的匹配問題發現 我所試圖做的描述:GetType.GetProperties曖昧找到匹配

兩個詞我想通過一個控制的所有屬性運行,並發現如果用戶有對控件的屬性進行了任何更改,然後我只取出已更改的屬性並存儲這些屬性的值

我遵循了這些建議,但當控件是TabControl時,我得到一個錯誤代碼控件(TabControl有2個tabPages) 。

回答

0

還向我道歉。
我以前的答案是錯誤的。
隨着BindingFlags.DeclaredOnly我沒有得到我想要的屬性。
所以我不得不以其他方式糾正問題。

發生此問題的原因是兩個屬性具有相同的名稱。
所以我搜索了相同的命名屬性不同,我發現他們有:不同的declaringType,MetadataTokenPropertyType
所以我改變我得到解決的價值和問題的方式:

Dim val = cntrl.GetType().GetProperty(prop.Name, prop.PropertyType).GetValue(cntrl, Nothing)   
Dim defVal = newCnt.GetType().GetProperty(prop.Name,prop.PropertyType).GetValue(newCnt,Nothing) 

很抱歉,如果我誤導別人。

3

好與幫助,從拉溫德拉Bagale我設法解決它: 問題是不是新修改,但我的愚蠢: 在MSDN是說:在AmbiguousMatchException發生包括以下

情況:
一個類型包含兩個具有相同名稱但參數個數不同的索引屬性。要解決歧義問題,請使用指定參數類型的GetProperty方法的重載。
通過使用新修飾符(Visual Basic中的陰影),派生類型聲明瞭一個用相同名稱隱藏繼承屬性的屬性。要解決歧義問題,請使用GetProperty(String,BindingFlags)方法重載幷包含BindingFlags.DeclaredOnly以將搜索限制爲未繼承的成員。

所以我用BindingFlags.DeclaredOnly和解決的問題:

Private Sub WriteProperties(ByVal cntrl As Control) 

Try 
    Dim oType As Type = cntrl.GetType 

    'Create a new control the same type as cntrl to use it as the default control      
    Dim newCnt As New Control 
    newCnt = Activator.CreateInstance(oType) 

    For Each prop As PropertyInfo In newCnt.GetType().GetProperties(BindingFlags.DeclaredOnly) 
     Dim val = cntrl.GetType().GetProperty(prop.Name).GetValue(cntrl, Nothing) 
     Dim defVal = newCnt.GetType().GetProperty(prop.Name).GetValue(newCnt, Nothing) 

     If val.Equals(defVal) = False Then 
      'So if something is different.... 
     End If 

    Next 
Catch ex As Exception 
    MsgBox("WriteProperties : " & ex.Message) 
End Try 

末次

+0

請提供指向MSDN文章的鏈接。 –

+0

我剛剛添加了鏈接 – Nianios