jeudi 2 avril 2015

Access VB.Net structure element by variable name

Ok, some inherited code: I have a struct with a whole load of permissions:



public structure Perms
dim writeStaff as Boolean
dim readStaff as Boolean
dim writeSupervisor as Boolean
dim readSuperVisor as Boolean
' ... and many more
End Structure


And I want a function canDo which I created like so:



public function canDo(p as Perms, whichOne as String) as boolean
Dim b as System.Reflection.FieldInfo
b = p.GetType().GetField(whichOne)
return b
end function


and I call canDo with pre-filled structure and "writeSupervisor" parameters


In debug, b appears as {Boolean writeSupervisor}, but when I try to return b as a Boolean, I get error: Value of type 'System.Reflection.FieldInfo' cannot be converted to 'Boolean'.


Any ideas how I can "index" into a struct by element name and test / compare / return values?






Aucun commentaire:

Enregistrer un commentaire