Reading fields with reflection

S

shaish

Hey, I have a question.
I wrote code which goes through an object and using reflection sets/
gets that objects fields.
I flag the fields I want to read/write with an attribute.
The order of the fields is very important to me, meaning I have to
read/write the fields each time in the same predefined order.
I've noticed that the when I go through the fields using reflection
the order is similar to the order they appear in the code.
The question is can I rely on that to always be that way ? Can .net
assure me that the order will stay the same ? If not how can I force
it to stay the same ?

Here is some code:

public class MyAttribute : Attribute
{
}

public class SomeObject
{
[MyAttribute]
public int firstFieldToRead;

[MyAttribute]
public int secondFieldToRead;
}

public ReadingMethod(Object obj)
{
foreach (FieldInfo field in obj.GetType().GetFields())
{
foreach (MyAttribute attr in field.GetCustomAttributes
(typeof(MyAttribute), true))
{
// Now I expect to get the first field and only
then the second field...
// Order of the fields here is important.
}
}
}

Thank you !
 
L

Larry Smith

Hey, I have a question.
I wrote code which goes through an object and using reflection sets/
gets that objects fields.
I flag the fields I want to read/write with an attribute.
The order of the fields is very important to me, meaning I have to
read/write the fields each time in the same predefined order.
I've noticed that the when I go through the fields using reflection
the order is similar to the order they appear in the code.
The question is can I rely on that to always be that way ? Can .net
assure me that the order will stay the same ? If not how can I force
it to stay the same ?

The docs clearly state that the '"GetFields method does not return fields in
a particular order". Why don't you just update "MyAttribute" so that it
takes an "Order" parameter or add an "OrderAttribute" attribute. You can
then iterate all fields, pick out the ones that have your attribute (loading
them into a new collection), sort this new collection based on the "Order",
and then iterate them again since they're now in the desired order. There
are various ways to do this but so long as you've added the "Order" yourself
you can accomplish your goal (though I agree that a native way would be
better).
 
L

Larry Smith

Hey, I have a question.
I wrote code which goes through an object and using reflection sets/
gets that objects fields.
I flag the fields I want to read/write with an attribute.
The order of the fields is very important to me, meaning I have to
read/write the fields each time in the same predefined order.
I've noticed that the when I go through the fields using reflection
the order is similar to the order they appear in the code.
The question is can I rely on that to always be that way ? Can .net
assure me that the order will stay the same ? If not how can I force
it to stay the same ?

The docs clearly state that the '"GetFields method does not return fields in
a particular order". Why don't you just update "MyAttribute" so that it
takes an "Order" parameter or add an "OrderAttribute" attribute. You can
then iterate all fields, pick out the ones that have your attribute (loading
them into a new collection), sort this new collection based on the "Order",
and then iterate them again since they're now in the desired order. There
are various ways to do this but so long as you've added the "Order" yourself
you can accomplish your goal (though I agree that a native way would be
better).
 
I

Ignacio Machin ( .NET/ C# MVP )

Hey, I have a question.
I wrote code which goes through an object and using reflection sets/
gets that objects fields.
I flag the fields I want to read/write with an attribute.
The order of the fields is very important to me, meaning I have to
read/write the fields each time in the same predefined order.
I've noticed that the when I go through the fields using reflection
the order is similar to the order they appear in the code.
The question is can I rely on that to always be that way ? Can .net
assure me that the order will stay the same ? If not how can I force
it to stay the same ?

You cannot rely on that, GetFields does not return the list in a given
order, basically because there is no concept of order. You will have
to sort the fields after they are returned.

Why is the order important to you? that might be a underlying problem
in your design
 
I

Ignacio Machin ( .NET/ C# MVP )

Hey, I have a question.
I wrote code which goes through an object and using reflection sets/
gets that objects fields.
I flag the fields I want to read/write with an attribute.
The order of the fields is very important to me, meaning I have to
read/write the fields each time in the same predefined order.
I've noticed that the when I go through the fields using reflection
the order is similar to the order they appear in the code.
The question is can I rely on that to always be that way ? Can .net
assure me that the order will stay the same ? If not how can I force
it to stay the same ?

You cannot rely on that, GetFields does not return the list in a given
order, basically because there is no concept of order. You will have
to sort the fields after they are returned.

Why is the order important to you? that might be a underlying problem
in your design
 
P

Paul

Sorry for replying to you Larry but the Original Post seems not to have
gotten downloaded.

It seems to me that the OP is using reflection as an expensive form of
custom serialization.

OP, you need to provide more information on what you are trying to do and
why.

Presumably this is some sort of ORM, in which case look no further than Linq
for an example of how to decorate your attributes to identify data fields
against properties.

Another way to ensure they are set in the right order is to use GetField
method rather than GetFields.

Can I also be an a** and suggest you use properties rather than fields.
 
P

Paul

Sorry for replying to you Larry but the Original Post seems not to have
gotten downloaded.

It seems to me that the OP is using reflection as an expensive form of
custom serialization.

OP, you need to provide more information on what you are trying to do and
why.

Presumably this is some sort of ORM, in which case look no further than Linq
for an example of how to decorate your attributes to identify data fields
against properties.

Another way to ensure they are set in the right order is to use GetField
method rather than GetFields.

Can I also be an a** and suggest you use properties rather than fields.
 

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