Groups | Blog | Home
all groups > vb.net > january 2006 >

vb.net : Is It Possible to Retrieve a List of Declared Variables?


Jay B. Harlow [MVP - Outlook]
1/27/2006 2:31:52 PM
Don,
You can using Reflection.

You need to start with a System.Type object. You can use the GetType keyword
to get a type object of known class, or you can use Object.GetType to get a
type object of a known object.

Once you have a Type object, you can call Type.GetFields &
Type.GetProperties to get fields & properties of the respective type. Watch
the overloads, as you can "fine tune" what you are looking for.

Dim aType As Type = GetType([MyClass])
For Each fi As System.Reflection.FieldInfo In aType.GetFields()
Debug.WriteLine(fi.Name, "field")
Next
For Each pi As System.Reflection.PropertyInfo In
aType.GetProperties()
Debug.WriteLine(pi.Name, "property")
Next
For Each mi As System.Reflection.MethodInfo In aType.GetMethods()
Debug.WriteLine(mi.Name, "method")
Next

Look for other methods on System.Type to retrieve other information on that
type.

--
Hope this helps
Jay [MVP - Outlook]
..NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net


[quoted text, click to view]
| Say I have a class like so:
|
| Public Class MyClass
| Public Prop1 as Integer
| Public Prop2 As Integer
| Public Prop3 As Integer
| End Class
|
| Is it possible to retrieve a list of the variables or objects declared
| within an instance of that class if they are declared with Public (or
| Friend) scope?
|
| e.g.
|
| Public Class SomeOtherClass
|
| Public Sub Experiment
|
| Dim obj As MyClass = New MyClass
|
| ' Get list of variables declared within obj
| '
| ' List the variable names:
| ' "Prop1"
| ' "Prop2"
| ' "Prop3"
|
| End Sub
|
| End Class
|
| Or can something like this only be done with Properties and Methods?
|
| - Don
|
|

Jay B. Harlow [MVP - Outlook]
1/27/2006 4:22:11 PM
Don,
Use the overloaded GetFields, & specify the option that indicates Friend.

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemtypeclassgetfieldstopic2.asp

--
Hope this helps
Jay [MVP - Outlook]
..NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net


[quoted text, click to view]
|
| >
| > 'GetType(MyClass1).GetFields(...)'.
|
| This worked for Publicly declared variables (which is great! thanks!),
but
| is there a way to get it to work with variables of scope Friend as well?
|
| - Don
|
|

Don
1/27/2006 8:18:44 PM
Say I have a class like so:

Public Class MyClass
Public Prop1 as Integer
Public Prop2 As Integer
Public Prop3 As Integer
End Class

Is it possible to retrieve a list of the variables or objects declared
within an instance of that class if they are declared with Public (or
Friend) scope?

e.g.

Public Class SomeOtherClass

Public Sub Experiment

Dim obj As MyClass = New MyClass

' Get list of variables declared within obj
'
' List the variable names:
' "Prop1"
' "Prop2"
' "Prop3"

End Sub

End Class

Or can something like this only be done with Properties and Methods?

- Don

Herfried K. Wagner [MVP]
1/27/2006 9:27:30 PM
"Don" <unknown@oblivion.com> schrieb:
[quoted text, click to view]

'GetType(MyClass1).GetFields(...)'.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
Don
1/27/2006 9:46:58 PM

[quoted text, click to view]

This worked for Publicly declared variables (which is great! thanks!), but
is there a way to get it to work with variables of scope Friend as well?

- Don

Don
1/27/2006 10:34:25 PM
I figured it had something to do with that, but I couldn't find any obvious
argument or combination of arguments that works.

In my test case, I have a class (a Form, actually) with one Public variable,
one Friend, and one Private. Within the form's Load event I call
Me.GetType.GetFields().

If I call GetFields with no arguments, then the fieldinfo for the Public
variable is returned. If I call GetField with BindingFlags.NonPublic, I get
nothing. And if I call GetField with BindingFlags.Public, I still get
nothing. I don't know if I'm missing some other BindingFlag or not. It
didn't behave as I expected it to, given what I could deduce from the flag
name and the online help.


[quoted text, click to view]

Jay B. Harlow [MVP - Outlook]
1/27/2006 10:59:47 PM
Don,
Did you review the URL I gave you?

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemtypeclassgetfieldstopic2.asp

As Herfried points out, you need both BindingFlags.Instance to say instance
fields (or BindingFlags.Static for Shared fields) plus you need
BindingFlags.Public to include public fields...

There are other BindingFlags available to include or exclude other fields
(such as inherited fields).

So be certain to review the above page.



--
Hope this helps
Jay [MVP - Outlook]
..NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net


[quoted text, click to view]
|I figured it had something to do with that, but I couldn't find any obvious
| argument or combination of arguments that works.
|
| In my test case, I have a class (a Form, actually) with one Public
variable,
| one Friend, and one Private. Within the form's Load event I call
| Me.GetType.GetFields().
|
| If I call GetFields with no arguments, then the fieldinfo for the Public
| variable is returned. If I call GetField with BindingFlags.NonPublic, I
get
| nothing. And if I call GetField with BindingFlags.Public, I still get
| nothing. I don't know if I'm missing some other BindingFlag or not. It
| didn't behave as I expected it to, given what I could deduce from the flag
| name and the online help.
|
|
[quoted text, click to view]
| > "Don" <unknown@oblivion.com> schrieb:
| >>> 'GetType(MyClass1).GetFields(...)'.
| >>
| >> This worked for Publicly declared variables (which is great! thanks!),
| >> but is there a way to get it to work with variables of scope Friend as
| >> well?
| >
| > Take a look at the overloaded versions of 'GetFields' which accept a
| > 'BindingFlags' parameter. Check out 'BindingFlags.NonPublic'.
| >
| > --
| > M S Herfried K. Wagner
| > M V P <URL:http://dotnet.mvps.org/>
| > V B <URL:http://classicvb.org/petition/>
|
|

Herfried K. Wagner [MVP]
1/27/2006 11:20:47 PM
"Don" <unknown@oblivion.com> schrieb:
[quoted text, click to view]

Take a look at the overloaded versions of 'GetFields' which accept a
'BindingFlags' parameter. Check out 'BindingFlags.NonPublic'.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>
Herfried K. Wagner [MVP]
1/28/2006 1:38:17 AM
"Don" <unknown@oblivion.com> schrieb:
[quoted text, click to view]

Include 'BindingFlags.Instance' ('BindingFlags.Instance Or
BindingFlags.Public', for example).

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>
Don
1/30/2006 2:44:45 PM

"Jay B. Harlow [MVP - Outlook]" <Jay_Harlow_MVP@tsbradley.net> wrote in
message news:%231pAre8IGHA.596@TK2MSFTNGP10.phx.gbl...
[quoted text, click to view]

Sorry, no. I read Herfried's response first and went with that, not
noticing your post later on. I was poring over the BindingFlags Enumeration
help, but I find the documentation provided with VS.NET to be typically
pretty obscurely written with poor code examples (at least compared with
pre-.NET Visual Studio documentation).

- Don

AddThis Social Bookmark Button