Calling InvokeMember on a Structure object does not update its value

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • adiel_g@hotmail.com

    Calling InvokeMember on a Structure object does not update its value

    I am trying to set a value on a structure object dynamically using
    InvokeMember. After I call InvokeMember, the value on the Admin flag
    does not get updated. Here is the following sample code to reproduce
    the behavior:

    Public Class clsTest

    Public Structure userGroupStruc
    Dim User As Boolean ' User Account
    Dim Admin As Boolean ' Administration Account
    End Structure

    Public objUserGroup As userGroupStruc

    Public Sub setupAccess()

    Dim currentGroup As String
    currentGroup = "Admin" ' For the test, lets set Admin to
    True

    objUserGroup.Ge tType().InvokeM ember(currentGr oup,
    Reflection.Bind ingFlags.SetFie ld, Nothing, objUserGroup, New Object()
    {True})

    End Sub

    End Class


    To test the behavior, simple create the class and call the setupAccess
    method:
    Dim objTest As New clsTest
    objTest.setupAc cess()

    If you then take a look at the value of objUserGroup.Ad min, it is
    still false! (Even though we called InvokeMember and set it to true.)
    I must be setting up the InvokeMember wrong but have been breaking my
    head on this one... I would appreciate your help on this one.

    Thanks Before Hand,
    Adiel
  • Armin Zingler

    #2
    Re: Calling InvokeMember on a Structure object does not update its value

    <adiel_g@hotmai l.comschrieb
    I am trying to set a value on a structure object dynamically using
    InvokeMember. After I call InvokeMember, the value on the Admin
    flag does not get updated. Here is the following sample code to
    reproduce the behavior:
    A structure is a value type. If you pass the object to InvokeMember, you
    pass a copy of the object because the argument is passed ByVal. InvokeMember
    would change the field value of the copy, but you never get the copy back.
    The bottom line is: It does not work with value types.

    Found this: (don't know if it helps or should be done)
    Yestarday I have seen a post in Experts-Exchange related with using reflection on structure in .net. The guy is trying to modify the conten...



    Armin

    Comment

    • adiel_g@hotmail.com

      #3
      Re: Calling InvokeMember on a Structure object does not update itsvalue

      Thank You Armin, you have answered my question. I will take a look at
      that article.

      Adiel

      Comment

      • adiel_g@hotmail.com

        #4
        Re: Calling InvokeMember on a Structure object does not update itsvalue

        I was able to resolve the problem by converting the structure into a
        new class.

        Thanks,
        Adiel

        Comment

        • Herfried K. Wagner [MVP]

          #5
          Re: Calling InvokeMember on a Structure object does not update its value

          <adiel_g@hotmai l.comschrieb:
          >I am trying to set a value on a structure object dynamically using
          InvokeMember. After I call InvokeMember, the value on the Admin flag
          does not get updated. Here is the following sample code to reproduce
          the behavior:
          >
          Public Class clsTest
          >
          Public Structure userGroupStruc
          Dim User As Boolean ' User Account
          Dim Admin As Boolean ' Administration Account
          End Structure
          >
          Public objUserGroup As userGroupStruc
          >
          Public Sub setupAccess()
          >
          Dim currentGroup As String
          currentGroup = "Admin" ' For the test, lets set Admin to
          True
          >
          objUserGroup.Ge tType().InvokeM ember(currentGr oup,
          Reflection.Bind ingFlags.SetFie ld, Nothing, objUserGroup, New Object()
          {True})
          >
          End Sub
          >
          End Class
          >[...]
          If you then take a look at the value of objUserGroup.Ad min, it is
          still false!
          Sample for 'SetValue', which can be easily adapted to be used with
          'InvokeMember':

          \\\
          Dim o As <structure type>
          Dim x As ValueType = o
          GetType(...).Ge tField(...).Set Value(x, <value>)
          o = CType(x, <structure type>)
          ///

          --
          M S Herfried K. Wagner
          M V P <URL:http://dotnet.mvps.org/>
          V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

          Comment

          Working...