Reflection and PropertyInfo

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Oskar Lundgren

    Reflection and PropertyInfo

    I want to iterate over fields declared like this, using reflection
    (and there are good reasons for that):

    Protected WithEvents Foo1 As Bar
    Protected WithEvents Foo2 As Bar
    ....

    I use the following code to get an array containing the fields
    (wrapped in PropertyInfo objects):

    Dim objs As Object() =
    Me.GetType().Ge tProperties(Ref lection.Binding Flags.Instance Or
    Reflection.Bind ingFlags.NonPub lic)

    My question is: How do I get the wrapped "Bar" objects (Foo1, Foo2 and
    so on)out of the PropertyInfo objects, so I can make calls to the
    methods of the "Bar" objects. Using GetValue on the PropertyInfo
    objects does not work.
  • Mattias Sjögren

    #2
    Re: Reflection and PropertyInfo

    Oskar,
    [color=blue]
    >Using GetValue on the PropertyInfo
    >objects does not work.[/color]

    No? How does it fail?



    Mattias

    --
    Mattias Sjögren [MVP] mattias @ mvps.org
    http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
    Please reply only to the newsgroup.

    Comment

    • Jorge

      #3
      Reflection and PropertyInfo

      Hello Oskar
      See the following example its for MenuItem

      <code>
      Dim myForm As Type = f.GetType()

      Dim fields As FieldInfo() = myForm.GetField s
      (BindingFlags.I nstance Or BindingFlags.No nPublic)

      For Each field As FieldInfo In fields

      If field.FieldType .Name = "MenuItem" Then
      Dim menu As MenuItem = DirectCast(fiel d.GetValue
      (f), MenuItem)
      '' you can use it here.
      end if
      next
      </code>

      Kind Regards
      Jorge[color=blue]
      >-----Original Message-----
      >I want to iterate over fields declared like this, using[/color]
      reflection[color=blue]
      >(and there are good reasons for that):
      >
      >Protected WithEvents Foo1 As Bar
      >Protected WithEvents Foo2 As Bar
      >....
      >
      >I use the following code to get an array containing the[/color]
      fields[color=blue]
      >(wrapped in PropertyInfo objects):
      >
      >Dim objs As Object() =
      >Me.GetType().G etProperties[/color]
      (Reflection.Bin dingFlags.Insta nce Or[color=blue]
      >Reflection.Bin dingFlags.NonPu blic)
      >
      >My question is: How do I get the wrapped "Bar" objects[/color]
      (Foo1, Foo2 and[color=blue]
      >so on)out of the PropertyInfo objects, so I can make[/color]
      calls to the[color=blue]
      >methods of the "Bar" objects. Using GetValue on the[/color]
      PropertyInfo[color=blue]
      >objects does not work.
      >.
      >[/color]

      Comment

      • Oskar Lundgren

        #4
        Re: Reflection and PropertyInfo

        The error message I get (at runtime):

        Property Get method was not found

        This does sound reasonable to me, since I haven't declared such a
        method...

        Anyway, I have found a solution of my problem. I didn't need to use
        reflection, since the Bar class is a subclass System.Web.UI.W ebControl
        and these objects easily can be retrieved using the Controls collection.
        And, yes, I'm new to Visual Basic .NET...

        *** Sent via Developersdex http://www.developersdex.com ***
        Don't just participate in USENET...get rewarded for it!

        Comment

        Working...