Calling InvokeMember on a Structure object does not update its value

A

adiel_g

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.GetType().InvokeMember(currentGroup,
Reflection.BindingFlags.SetField, 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.setupAccess()

If you then take a look at the value of objUserGroup.Admin, 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
 
A

Armin Zingler

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)
http://mmarinov.blogspot.com/2007/01/reflection-modify-value-types-by.html


Armin
 
A

adiel_g

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

Adiel
 
A

adiel_g

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

Thanks,
Adiel
 
H

Herfried K. Wagner [MVP]

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.GetType().InvokeMember(currentGroup,
Reflection.BindingFlags.SetField, Nothing, objUserGroup, New Object()
{True})

End Sub

End Class
[...]
If you then take a look at the value of objUserGroup.Admin, 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(...).GetField(...).SetValue(x, <value>)
o = CType(x, <structure type>)
///
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top