Click here to Skip to main content
11,479,096 members (59,221 online)
Click here to Skip to main content

Tagged as

Accessing Base private members from Child TIP

, 20 Oct 2011 LGPL3 11.3K
Rate this:
Please Sign up or sign in to vote.
Dear Reader, I had been thinking about how to prove myself that base class members do get inherited to their child classes (sub classes) but still they remain private. Actually this lines i had read in many places in my earlier days. But always wanted to validate it. So i set to write code for [...]

Dear Reader,

I had been thinking about how to prove myself that base class members do get inherited to their child classes (sub classes) but still they remain private. Actually this lines i had read in many places in my earlier days. But always wanted to validate it.

So i set to write code for that and hence i started using Reflection. It’s the only way one can open the package (object) and see inside it at run time, because i knew this inheritance really come alive at run time. Of course its known fact you might think.

Just to let you know this idea popped up in my mind when i started my real programming career after fresher stage. So its shame that i am writing about it now.

To access members, i am using Type.BaseType.GetMethods() API and for fields i am using Type.BaseType.GetFields() API.

This is how i did, although i must honestly admit that the entire idea i could not conceive by myself on how to implement so i had to take a look at Stackoverflow threads on the same.

So below is the code:

public class Base
{
int basePrivateVariable;

void Hello() { }
}

public class Child : Base
{
}

BindingFlags flag = BindingFlags.NonPublic | BindingFlags.Instance;
var privateMethods = typeof(Child).BaseType.GetMethods(flag);
var privateFields = typeof(Child).BaseType.GetFields(flag);

Although i am still looking (curious or craziness of mine you can say) to achieve the same without using BaseType property on a Type.  No this is not solving any real world problem for me, but i am just curious because using BaseType property of Type class i am convinced that while writing it in my code editor (visual studio) i know for sure that i am accessing base class members because i am using BaseType property. So i want some thing like generics where in at compile time you’re not convinced/know what type you will be accessing or what type your block code gets. The same i want to access base class members by using only Child object and its level reflection API i.e with out using BaseType property. If you know, kindly comment.

Thanks

License

This article, along with any associated source code and files, is licensed under The GNU Lesser General Public License (LGPLv3)

Share

About the Author

zenwalker1985
Software Developer (Senior) Siemens
India India
A .net developer since 4+ years, wild, curious and adventurous nerd.

Loves Trekking/Hiking, animals and nature.

A FOSS/Linux maniac by default Wink | ;)

An MVP aspirant and loves blogging -> https://adventurouszen.wordpress.com/
Follow on   Twitter

Comments and Discussions

 
-- There are no messages in this forum --
| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.150520.1 | Last Updated 20 Oct 2011
Article Copyright 2011 by zenwalker1985
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid