Reflection over an array of objects

F

Flomo Togba Kwele

I have an array of objects. Each object has 14 private fields.

For each object, I would like to determine the maximum number of
characters contained within that field, trimmed of any trailing
spaces.

Is it possible to reflect over each of the private fields of an object
to determine the number of characters in each of the 14 fields?

TIA Lars
 
N

Nicholas Paldino [.NET/C# MVP]

Flomo,

Well, do you know what the type of the objects is ahead of time? If so,
why not just cast the instances to that type and access the fields?

If not, then yes, you could use reflection. Just call GetType on each
instance (if each type is different) and then call GetFields to get the
fields. Then you can call the GetValue method on the FieldInfo instances
and get the string values (assuming the field is a string).
 
L

Linda Liu[MSFT]

Hi Flomo,

No, we have no way to get the values of PRIVATE fields of an object from
outside.

If the field of an object is public, we can get the value of this field by
calling the GetFields method on the type of this object and then calling
the GetValue method on the FieldInfo instances.

If you have anything unclear, please feel free to let me know.

Sincerely,
Linda Liu
Microsoft Online Community Support

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 
J

Jon Skeet [C# MVP]

No, we have no way to get the values of PRIVATE fields of an object from
outside.

Yes, we do. If you call GetFields with the overload taking a
BindingFlags parameter, you can request non-public fields. For
instance, let's see what System.String contains:

using System;
using System.Reflection;

class Test
{
static void Main(string[] args)
{
foreach (FieldInfo fi in typeof(string).GetFields
(BindingFlags.Instance | BindingFlags.NonPublic))
{
Console.WriteLine ("{0} ({1}) = {2}",
fi.Name,
fi.FieldType,
fi.GetValue("foo"));
}
}
}

Resuts:
m_arrayLength (System.Int32) = 4
m_stringLength (System.Int32) = 3
m_firstChar (System.Char) = f

This depends on having the required permissions, of course.

Jon
 
L

Linda Liu[MSFT]

Thanks Jon for correcting me! I was ignoring the BindingFlags parameter!

Hi Flomo,

How about the problem now?

To get what you want, you can get the value of this field by calling the
GetFields method on the type of this object passing BindingFlags.Instance
plus BindingFlags.NonPublic as the parameter and then calling the GetValue
method on the FieldInfo instances.

The following is a sample.

class MyClass
{
private string name;
public string Name
{
get { return name; }
set { name = value; }
}
}
private void Form1_Load(object sender, EventArgs e)
{
MyClass obj = new MyClass();
obj.Name = "aa";
FieldInfo[] fis = obj.GetType().GetFields(BindingFlags.Instance |
BindingFlags.NonPublic);
object value = fis[0].GetValue(obj);
}

If you have any question, please feel free to let us know.

Sincerely,
Linda Liu
Microsoft Online Community Support
 
L

Linda Liu[MSFT]

Hi Flomo,

I am reviewing this post and would like to know the status of this issue.

If you have any question, please feel free to let me know.

Thank you for using our MSDN Managed Newsgroup Support Service!

Sincerely,
Linda Liu
Microsoft Online Community Support
 

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