Home All Groups Group Topic Archive Search About

Is It Possible to Retrieve a List of Declared Variables?

Author
27 Jan 2006 8:18 PM
Don
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

Author
27 Jan 2006 8:27 PM
Herfried K. Wagner [MVP]
"Don" <unkn***@oblivion.com> schrieb:
>    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?

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

--
M S   Herfried K. Wagner
M V P  <URL:http://dotnet.mvps.org/>
V B   <URL:http://classicvb.org/petition/>
Author
27 Jan 2006 9:46 PM
Don
"Herfried K. Wagner [MVP]" <hirf-spam-me-here@gmx.at> wrote in message
news:OCjWRA4IGHA.1188@TK2MSFTNGP14.phx.gbl...

>
> '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
Author
27 Jan 2006 10:20 PM
Herfried K. Wagner [MVP]
"Don" <unkn***@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/>
Author
27 Jan 2006 10:34 PM
Don
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.


Show quoteHide quote
"Herfried K. Wagner [MVP]" <hirf-spam-me-here@gmx.at> wrote in message
news:OZf4k$4IGHA.676@TK2MSFTNGP10.phx.gbl...
> "Don" <unkn***@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/>
Author
28 Jan 2006 12:38 AM
Herfried K. Wagner [MVP]
"Don" <unkn***@oblivion.com> schrieb:
> 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

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/>
Author
28 Jan 2006 4:59 AM
Jay B. Harlow [MVP - Outlook]
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


"Don" <unkn***@oblivion.com> wrote in message
news:RdxCf.233435$tl.52321@pd7tw3no...
Show quoteHide quote
|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.
|
|
| "Herfried K. Wagner [MVP]" <hirf-spam-me-here@gmx.at> wrote in message
| news:OZf4k$4IGHA.676@TK2MSFTNGP10.phx.gbl...
| > "Don" <unkn***@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/>
|
|
Author
30 Jan 2006 2:44 PM
Don
"Jay B. Harlow [MVP - Outlook]" <Jay_Harlow_***@tsbradley.net> wrote in
message news:%231pAre8IGHA.596@TK2MSFTNGP10.phx.gbl...
> Don,
> Did you review the URL I gave you?

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
Author
27 Jan 2006 10:22 PM
Jay B. Harlow [MVP - Outlook]
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


"Don" <unkn***@oblivion.com> wrote in message
news:mxwCf.233143$tl.88327@pd7tw3no...
Show quoteHide quote
|
| "Herfried K. Wagner [MVP]" <hirf-spam-me-here@gmx.at> wrote in message
| news:OCjWRA4IGHA.1188@TK2MSFTNGP14.phx.gbl...
|
| >
| > '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
|
|
Author
27 Jan 2006 8:31 PM
Jay B. Harlow [MVP - Outlook]
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


"Don" <unkn***@oblivion.com> wrote in message
news:EevCf.457280$ki.60104@pd7tw2no...
Show quoteHide quote
| 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
|
|