Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C#: Accessing Inherited Private Instance Members Through Reflection

Tags:

c#

reflection

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.

like image 754
Odrade Avatar asked Mar 26 '09 16:03

Odrade


People also ask

What do you mean by C?

C is an imperative procedural language supporting structured programming, lexical variable scope, and recursion, with a static type system. It was designed to be compiled to provide low-level access to memory and language constructs that map efficiently to machine instructions, all with minimal runtime support.

Is C language easy?

Compared to other languages—like Java, PHP, or C#—C is a relatively simple language to learn for anyone just starting to learn computer programming because of its limited number of keywords.

What is %d in C programming?

In C programming language, %d and %i are format specifiers as where %d specifies the type of variable as decimal and %i specifies the type as integer. In usage terms, there is no difference in printf() function output while printing a number using %d or %i but using scanf the difference occurs.

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.


2 Answers

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);
}
like image 155
sisve Avatar answered Sep 20 '22 23:09

sisve


I haven't tried it, but you should be able to access the base type private members through the Type.BaseType property and recursively accumulate all the private fields through the inheritence hierarchy.

like image 27
Lee Avatar answered Sep 18 '22 23:09

Lee