create a private object for a class with private constructor in c# -
this question has answer here:
i want access of private members of class has constructor defined private. how create privateobject such class can access private members ?
i tried cannot instantiate class "myclass1" not able instantite privateobject.
myclass1 myclass = new myclass1(); //gives compilation error privateobject po = new privateobject(myclass); //gives compilation error
is there workaround ?
class private constructor can create own static method. example:
class myclass1 { private myclass1() { } public static myclass1 createinstance() { return new myclass1(); } }
it's private members fields or properties accessible inside of class (unless make tricks reflection). if field protected can access deriving class. other way it's design created restrict access fields , should not try accessing them outside.
edited: noticed use privateobject class created make reflection trickes mentioned above. need create instance. should check designed way of initializing object static method?
or check link more hacks reflaction , using activator: http://www.ipreferjim.com/2011/08/c-instantiating-an-object-with-a-private-constructor/
Comments
Post a Comment