As we all know that private data members are not accessible from outside of class, but suppose if we need to assign any value to private data member then how we can do that without creating object of class or using any kind of reflection
Class ABC
{
private int x;
ABC(int temp_x){this.x = temp_x;}
}
Class XYZ : ABC
{
//class XYZ declaration
}
by constructor we can assign the value when we creating the object of class. But is there any way to assign value to variable x without creating any object of ABC.


VulpesRank #55 · 29.2K reputationPosted Jul 24, 2014, 2:08 AM EDT
class XYZ : ABC
{
public XYZ(int x) : base (x)
{}
}
Anupam SinghRank #643 · 3.7K reputationPosted Jul 24, 2014, 2:10 AM EDT
this could be helpful : http://dotnetfollower.com/wordpress/2012/12/c-how-to-set-or-get-value-of-a-private-or-internal-property-through-the-reflection/
Akhil KhareRank #7,443 · 241 reputationPosted Jul 24, 2014, 2:12 AM EDT
VulpesRank #55 · 29.2K reputationPosted Jul 24, 2014, 3:07 AM EDT
Normally, the private field 'i' couldn't exist at all unless an object of type ABC had been created.The method I gave earlier gets around this by creating an object of the derived class XYZ which incorporates the field inherited from its base class even though it's private and not therefore directly accessible from ABC.
This field is then set indirectly by calling the base class constructor from XYZ's constructor.