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 !
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 !