Use reflection to init members?

  • Thread starter Thread starter Boni
  • Start date Start date
B

Boni

Dear all,
is it possible to get names and initialize members of a class.
i.e
class A
dim M1 as integer
dim M2 as integer
dim M3 as integer
dim M4 as integer
sub new ()
'get name of the first member->it is M1->M1=0
'get name of the second member->it is M2->M2=1
....
end class
 
Hi,

Take a look at the propertyinfo's setvalue method. Here is a simple
example.

http://msdn.microsoft.com/library/d...mreflectionpropertyinfoclasssetvaluetopic.asp


Dim f As New MyTestClass


Dim t As Type = f.GetType

Trace.WriteLine("Properties and Values")
For Each pi As Reflection.PropertyInfo In t.GetProperties
Trace.WriteLine(String.Format("{0} {1} {2}", pi.Name, _
pi.PropertyType.FullName, pi.GetValue(f, Nothing)))
If pi.PropertyType.FullName = "System.String" Then
pi.SetValue(f, "New Value", Nothing)
End If
Next


Ken
 

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

Similar Threads


Back
Top