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.
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.
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.
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.
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.
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);
}
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With