1

I face a problem with Ambiguous match found What I am trying to do is described in :GetType.GetProperties

In two words I am trying to run through all the properties of a control and find if user had made any changes to control's properties , then I take only the changed properties and store the values for these properties

I followed the suggestions but I get an error for propery Padding when the control is a TabControl (the tabControl has 2 tabPages).

Community
  • 1
  • 1
Nianios
  • 1,391
  • 3
  • 20
  • 45

2 Answers2

4

Ok with help from Ravindra Bagale I manage to solve it: The problem wasn't the new modifier but my stupidity: In MSDN is says:

Situations in which AmbiguousMatchException occurs include the following:
A type contains two indexed properties that have the same name but different numbers of parameters. To resolve the ambiguity, use an overload of the GetProperty method that specifies parameter types.
A derived type declares a property that hides an inherited property with the same name, by using the new modifier (Shadows in Visual Basic). To resolve the ambiguity, use the GetProperty(String, BindingFlags) method overload and include BindingFlags.DeclaredOnly to restrict the search to members that are not inherited.

So I used BindingFlags.DeclaredOnly and the problem solved:

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

End Sub

Nianios
  • 1,391
  • 3
  • 20
  • 45
0

I have to apologize.
My previous answer was wrong.
With BindingFlags.DeclaredOnly I don't get the properties that I wanted.
So I had to correct the problem with other way.

The problem occurs because two properties have the same name.
So I searched where the same named properties are different and I found that they have: have different declaringType,MetadataToken and PropertyType.
So I change the way I get the value and problem solved:

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

Sorry if I misguided someone.

Nianios
  • 1,391
  • 3
  • 20
  • 45