Initializing fields using reflection

S

Sharon

Hello Gurus,

I have a class:

[StructLayout(LayoutKind.Sequential)]
public class Madads
{
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 8)]
public char[] MAD = new char[8];
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 31)]
public char[] NAME = new char[31];

public Madads() {}
}

Assuming there may be more fields and not all of them of type char[].
I want to add some code to class constructor, using reflection, to
initialize all its char members with ‘ ‘ (space) value.

I did this:

Type thisType = this.GetType();
System.Reflection.FieldInfo [] fieldsInfo = thisType.GetFields();
string str;
foreach( System.Reflection.FieldInfo fi in fieldsInfo )
{}

But I can see where to go from there.

How do I go from there?
 
P

Peter Duniho

Sharon said:
[...]
Assuming there may be more fields and not all of them of type char[].
I want to add some code to class constructor, using reflection, to
initialize all its char members with ‘ ‘ (space) value.

I did this:

Type thisType = this.GetType();
System.Reflection.FieldInfo [] fieldsInfo = thisType.GetFields();
string str;
foreach( System.Reflection.FieldInfo fi in fieldsInfo )
{}

But I can see where to go from there.

How do I go from there?

Each member variable is a "char[]" and you can easily retrieve the array
reference from the FieldInfo object and iterate through the array
setting each element as desired.

But IMHO you should not use reflection for this. Just explicitly
initialize each field you have by name. The code will be easier to
understand and more efficient.

Pete
 
S

Sharon

I tried that, but couldn’t find how to.

Can you post some sample on how to do that?



--
Thanks
Sharon


Peter Duniho said:
Sharon said:
[...]
Assuming there may be more fields and not all of them of type char[].
I want to add some code to class constructor, using reflection, to
initialize all its char members with ‘ ‘ (space) value.

I did this:

Type thisType = this.GetType();
System.Reflection.FieldInfo [] fieldsInfo = thisType.GetFields();
string str;
foreach( System.Reflection.FieldInfo fi in fieldsInfo )
{}

But I can see where to go from there.

How do I go from there?

Each member variable is a "char[]" and you can easily retrieve the array
reference from the FieldInfo object and iterate through the array
setting each element as desired.

But IMHO you should not use reflection for this. Just explicitly
initialize each field you have by name. The code will be easier to
understand and more efficient.

Pete
.
 
P

Peter Duniho

Sharon said:
I tried that, but couldn’t find how to.

Can you post some sample on how to do that?

[StructLayout(LayoutKind.Sequential)]
public class Madads
{
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 8)]
public char[] MAD = new char[8];
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 31)]
public char[] NAME = new char[31];

public Madads()
{
_FillArray(MAD, ' ');
_FillArray(NAME, ' ');
}

private void _FillArray<T>(T[] array, T element)
{
for (int i = 0; i < array.Length; i++)
{
array = element;
}
}
}

Or something like that.

Pete
 
S

Sharon

Your post is good for these two fields. It means that I need to add more line
_FillArray(FieldName, value) to the constructor if more fields are added and
they also may be of other types.

I want to write some code that does not depends on the number of fields, and
therefore I thought using the reflection, but I didn’t managed on doing that.

Can you post me some example for doing that?



--
Thanks
Sharon


Peter Duniho said:
Sharon said:
I tried that, but couldn’t find how to.

Can you post some sample on how to do that?

[StructLayout(LayoutKind.Sequential)]
public class Madads
{
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 8)]
public char[] MAD = new char[8];
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 31)]
public char[] NAME = new char[31];

public Madads()
{
_FillArray(MAD, ' ');
_FillArray(NAME, ' ');
}

private void _FillArray<T>(T[] array, T element)
{
for (int i = 0; i < array.Length; i++)
{
array = element;
}
}
}

Or something like that.

Pete
.
 
P

Peter Duniho

Sharon said:
Your post is good for these two fields. It means that I need to add more line
_FillArray(FieldName, value) to the constructor if more fields are added

If you have time to add a field to a class, you have time to add a line
to the constructor to initialize that field.
and they also may be of other types.

If you intend to support this kind of initialization of fields of other
types (and given your description of the initialization -- to fill with
spacess -- it's not clear what that would even mean), that would only
make a reflection-based solution that much more complicated and error-prone.
I want to write some code that does not depends on the number of fields, and
therefore I thought using the reflection, but I didn’t managed on doing that.

Reflection is for when you _can't_ do it any other way. It has
significant drawbacks (slow, easy to get it wrong) and shouldn't be used
as a way to avoid typing.

Pete
 

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