2012-10-23 41 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