Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 155221
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T10:07:02+00:00 2026-05-11T10:07:02+00:00

I am an absolute novice at reflection in C#. I want to use reflection

  • 0

I am an absolute novice at reflection in C#. I want to use reflection to access all of private fields in a class, including those which are inherited.

I have succeeded in accessing all private fields excluding those which are inherited, as well as all of the public and protected inherited fields. However, I have not been able to access the private, inherited fields. The following example illustrates:

class A {     private string a;     public string c;     protected string d; }  class B : A {     private string b; }  class test {     public static void Main(string[] Args)     {         B b = new B();                Type t;         t = b.GetType();         FieldInfo[] fields = t.GetFields(BindingFlags.Public | BindingFlags.NonPublic                                          | BindingFlags.Instance);          foreach(FieldInfo fi in fields){              Console.WriteLine(fi.Name);         }         Console.ReadLine();     } } 

This fails to find the field B.a.

Is it even possible to accomplish this? The obvious solution would be to convert the private, inherited fields to protected fields. This, however, is out of my control at the moment.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T10:07:03+00:00Added an answer on May 11, 2026 at 10:07 am

    As Lee stated, you can do this with recursion.

    private static void FindFields(ICollection<FieldInfo> fields, Type t) {     var flags = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance;      foreach (var field in t.GetFields(flags)) {         // Ignore inherited fields.         if (field.DeclaringType == t)             fields.Add(field);     }      var baseType = t.BaseType;     if (baseType != null)         FindFields(fields, baseType); }  public static void Main() {     var fields = new Collection<FieldInfo>();     FindFields(fields, typeof(B));     foreach (FieldInfo fi in fields)         Console.WriteLine(fi.DeclaringType.Name + ' - ' + fi.Name); } 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Disclaimer: absolute novice in Scala :( I have the following defined: def tryAndReport(body: Unit)
I am an absolute novice to django-cms. I have gone through the tutorial and
I have a handful of raw SQL queries for SQL Server which use SCOPE_IDENTITY
Absolute beginner question: I have a template file index.html that looks like this: ...
i want to style a submit button which seems to be rendered abit differently
Currently I have a view which I render to a template and return it
I have following HTML+CSS markup: <div id=protofade style=position: relative;> <div style=position: absolute;><img src=slide-01.jpg></div> <div
Hey all. I have the following code: http://jsfiddle.net/g7Cgg/ As you can see, there are
I have a div that is positioned:absolute, this div extends outside the bounds of
I have an HTML editor widget that I want to appear within an absolutely

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.