Looping through the variables in a Class

J

Jason R. Soby

Hi,

I've heard that using the System.Reflection namespace, it is possible to
loop through variables as you would items in an array but the Reflection
namespace is still a little tricky to me as i'm still only really 1 year into
developing in .Net. Could anyone get me started here?

Thanks so much!!
Jason
 
F

Family Tree Mike

Jason said:
Hi,

I've heard that using the System.Reflection namespace, it is possible to
loop through variables as you would items in an array but the Reflection
namespace is still a little tricky to me as i'm still only really 1 year into
developing in .Net. Could anyone get me started here?

Thanks so much!!
Jason

The examples in MSDN are often a good starting point.
http://msdn.microsoft.com/en-us/library/system.reflection.propertyinfo.aspx.
 
J

Jeff Johnson

I've heard that using the System.Reflection namespace, it is possible to
loop through variables as you would items in an array but the Reflection
namespace is still a little tricky to me as i'm still only really 1 year
into
developing in .Net. Could anyone get me started here?

There are no "variables" in a class, at least not that are exposed for
reflection. What exactly are you looking for? Fields? Properties? Enums?
It's important to learn correct terminology, as this will aid you in seeking
help in the future.
 
P

Peter Duniho

Hi,

I've heard that using the System.Reflection namespace, it is possible to
loop through variables as you would items in an array but the Reflection
namespace is still a little tricky to me as i'm still only really 1 year
into
developing in .Net. Could anyone get me started here?

As "Family Tree Mike" says, MSDN has code examples as a starting point.

That said, you should be aware that reflection is usually the wrong way to
solve any .NET coding problem. Correct uses do come up, but not nearly as
often as incorrect ones. Almost all of the time, when someone thinks they
want to use reflection to solve a problem, they're mistaken.

Pete
 
P

Peter Duniho

There are no "variables" in a class, at least not that are exposed for
reflection. What exactly are you looking for? Fields? Properties? Enums?
It's important to learn correct terminology, as this will aid you in
seeking
help in the future.

Actually, fields are "variables". The C# specification describes them as
"static variables" and "instance variables" (for obvious reasons).
Technically even the "readonly" ones, though obviously they only "vary"
once. :)
 
H

Hoop

Hi,

I've heard that using the System.Reflection namespace, it is possible to
loop through variables as you would items in an array but the Reflection
namespace is still a little tricky to me as i'm still only really 1 year into
developing in .Net. Could anyone get me started here?

Thanks so much!!
Jason

Hi Jason,
I used reflection for creating an application that used plug-ins.
I could add various instances of a class type to a plugin directory
and when the app
would start, it would search through this directory and add the
plugins so they
could be used in the application.
Jeff
 
J

Jason R. Soby

To all of you, the idea i have is that i create class level properties that
are used through several methods, so maybe property is the appropriate word.
I then need to zero them out in a certain method and i'd like to be able to
do it without having a line for each
 
G

Gregory A. Beamer

Hi,

I've heard that using the System.Reflection namespace, it is possible
to loop through variables as you would items in an array but the
Reflection namespace is still a little tricky to me as i'm still only
really 1 year into developing in .Net. Could anyone get me started
here?

Thanks so much!!
Jason

Code sample (Once in the array, you can foreach):
http://www.csharp-examples.net/reflection-property-names/



Peace and Grace,

--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA

Twitter: @gbworld
Blog: http://gregorybeamer.spaces.live.com

*******************************************
| Think outside the box! |
*******************************************
 
P

Peter Duniho

To all of you, the idea i have is that i create class level properties
that
are used through several methods, so maybe property is the appropriate
word.
I then need to zero them out in a certain method and i'd like to be able
to
do it without having a line for each

I can think of at least three approaches better than reflection for
something like that:

-- Just do it explicitly. If you want conciseness in most of the
code, create a helper method
-- Implement the properties in some other way, such as items in a
dictionary
-- Put the values in a struct that's stored as a member of your class;
structs naturally have a "zero out" semantic you can use, letting you
reset everything in the struct to their default state in a single
statement of code
-- Rather than resetting an existing instance of the class, create a
new one; similar to the struct idea, except you take advantage of the
default initialization for reference types instead of value types

Sorry, that's four. :)

Pete
 
T

Tom Spink

Hi Jason,
To all of you, the idea i have is that i create class level properties that
are used through several methods, so maybe property is the appropriate word.
I then need to zero them out in a certain method and i'd like to be able to
do it without having a line for each

Please please PLEASE listen to Pete. This is exactly the wrong approach
to what you are trying to accomplish.

By all means study reflection as a learning tool, to help you understand
the inner-workings of the runtime. But, it is the wrong tool for doing
what you have described.

Ask yourself "Why do you need to zero the properties", not "How do you
zero properties".
 
S

Simon Hart [MVP]

NHibernate, RhinoMocks in fact any data mapping or proxy creation framework
uses reflection.
 
S

Simon Hart [MVP]

As everyone else has said, reflection here is way overkill for as something
as simple as what you describe - and expesive. You'd normally include this
kind of behavior in your class or domain if it is a domain.
 

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