Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / January 2008

Tip: Looking for answers? Try searching our database.

Reflection over an array of objects

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Flomo Togba Kwele - 24 Jan 2008 01:01 GMT
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
Nicholas Paldino [.NET/C# MVP] - 24 Jan 2008 04:22 GMT
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).

Signature

         - Nicholas Paldino [.NET/C# MVP]
         - mvp@spam.guard.caspershouse.com

>I have an array of objects. Each object has 14 private fields.
>
[quoted text clipped - 6 lines]
>
> TIA Lars
Linda Liu[MSFT] - 24 Jan 2008 08:07 GMT
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.
Jon Skeet [C# MVP] - 24 Jan 2008 08:56 GMT
On Jan 24, 8:07 am, v-l...@online.microsoft.com (Linda Liu[MSFT])
wrote:
> 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
Linda Liu[MSFT] - 28 Jan 2008 06:45 GMT
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
Linda Liu[MSFT] - 31 Jan 2008 02:36 GMT
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

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.