Jump to content
Xtreme .Net Talk

Recommended Posts

  • Leaders
Posted

Anyone know a combination to find only overridden properties using GetProperty() of a Type? I keep getting ambiguousmatchexception because it finds both the base class' property and my overridden one. Here's a mock-up of what I'm trying to do.

 

My class:

Public Class NewTextBox
   Inherits System.Windows.Forms.TextBox

   Private m_strText As String = ""

   Public Overrides Property Text() As String
       Get
           Return m_strText
       End Get
       Set(ByVal Value As String)
           m_strText = Value
       End Set
   End Property
End Class

 

GetProperty attempt:

   Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
       Dim typ As Type
       typ = Me.NewTextBox1.GetType()

       Dim prop As PropertyInfo
       prop = typ.GetProperty("Text")

       If Not prop Is Nothing Then
           prop.SetValue(Me.NewTextBox1, "My New Value", Nothing)
       Else
           MessageBox.Show("Not found.")
       End If

   End Sub

 

This returns ambigous match and I've tried several BindingFlag parameters to the GetProperty() method to no avail. Seems like it should be fairly easy.

--tim
  • Leaders
Posted

Answer:

props = typ.GetProperty("Text", BindingFlags.Public Or BindingFlags.Instance Or BindingFlags.DeclaredOnly)

In my opinion, this ain't exactly straightforward, oh well.

--tim

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...