Assign Public or Private variable thru Reflection vb.net/asp.net

G

Guest

I try to initialize some Public Variable in a class use for code-behind

The idea is to check all uxMsgXXX variables, declare in top of my class and
assign the string value from a DB request. Why do That ? Multilinguage
apps...

If I'm able to do so, I already have an interface to enter the translation
for each variables. Also, this way I don't need to deploy extra files for the
translation.

I can get all my fields from my class with System.Reflection.FieldInfo but
I'm not able to getvalue or setvalue. I can not find the good method/syntax
to do so.

Please help !!!

Ex.:
Public Class Extension
Inherits IcbWebform

public uxMsgError as string = "English Error Message"

Private Sub Page_Init(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Init
Dim oTrans As New Translation
InitializeComponent()
oTrans.LoadLngString(GetType(Extension))
End Sub
End class

So when Page_Init() is finish, uxMsgError = "Message d'erreur Francais"

oTrans.LoadLngString()

Public Sub LoadLngString(ByVal oPage As Type)
Dim fieldInfo() As System.Reflection.FieldInfo
Dim value As String
Dim o As Type
Dim i As Integer
o = oPage
fieldInfo = oPage.GetFields()
For i = 0 To fieldInfo.Length - 1

Dim fd As String = fieldInfo(i).Name
o.GetType().InvokeMember(fd, Reflection.BindingFlags.SetField,
Nothing, o, New Object() {"set string value"})
Dim rtStr As String = CType(o.GetType.InvokeMember(fd,
Reflection.BindingFlags.GetField, Nothing, o, Nothing), String)
Next
End Sub
 
C

Chris Dunaway

André said:
I can get all my fields from my class with System.Reflection.FieldInfo but
I'm not able to getvalue or setvalue. I can not find the good method/syntax
to do so.
Dim fd As String = fieldInfo(i).Name
o.GetType().InvokeMember(fd, Reflection.BindingFlags.SetField,
Nothing, o, New Object() {"set string value"})

You probably just need to make sure you are specifying the right
combination of BindingFlags using the Or operator. For setting/getting
a private member, you probably need something like this:

o.GetType().InvokeMember(fd, BindingFlags.SetField Or
BindingFlags.Instance Or BindingFlags.NonPublic, Nothing, o, New
Object() {"set string value"})

Look at the docs for InvokeMember and there is an example.
 
P

Phill. W

André said:
The idea is to check all uxMsgXXX variables, declare in top of my
class and assign the string value from a DB request. Why do That ?
Multilinguage apps...

So you want to initialise every /possible/ error message when you
create an instance of your web form? Strikes me as a bit overkill,
especially if nothing every goes wrong! ;-)

I would suggest providing a unique way of identifying each
language-dependent message (say, a numeric prefix) which you
hard-code into your application, then add a "Translating" class
which takes that "tagged" message and translates it, on demand,
via the database lookup, if necessary?

Just a thought ...

HTH,
Phill W.
 

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