   [.net] Private member access
  Is giving another class access to the private and protected members of a class possible and if so, how?   
  In C++ there's the friend keyword. You didn't specify a language though. Since this in the .Net forum you might check out InternalsVisibleTo, but I've never used it myself.   ;  As I know there are no way to allow access to private/protected fields (except reflection).You should use internal or protected internal access modifiers.More info in MSDN.InternalsVisibleTo can be applied to whole assembly only and gives ability to share access to internal fields with other assemblies.   ;Quote:Original post by Narf the MouseIs giving another class access to the private and protected members of a class possible and if so, how?Before doing this, ask yourself why you want to do it? 'cos you better have a damn good reason.IIRC, it is possible to access private members via reflection. A quick google of "set private member by reflection" should turn up the methods. Once again, why are you doing this?   ;  ...I was thinking it might make my Factory methods more secure, if only they could access the creation functions. *Dry*Thanks for the info.   ;Quote:IIRC, it is possible to access private members via reflection. A quick google of "set private member by reflection" should turn up the methods. It is possible for sure, I wrote a unit test helper that did it, however I'd say that usually there's a better solution to the problem at hand.   ;  The question is do you want to just read the private members or do you want to change the data there?If you want to change it, then I am going to recommend finding another way.  Any time you allow changes to data from another class you lead yourself down a path of debug nightmares.theTroll   ;Quote:Original post by Narf the Mouse...I was thinking it might make my Factory methods more secure, if only they could access the creation functions. *Dry*Thanks for the info.It bears repeating: I was hoping to make the Initialize methods in a class that is created by a factory, private and "friend" such that only the factory could access them.   ;  Why don't you just initialize in the constructor? theTroll   ;Quote:Original post by TheTrollWhy don't you just initialize in the constructor? theTrollSeparation of logic. A constructor can get large and unwieldy. Plus, then you get multiple constructors. So you make a Set/Initialize method. And then SubSet/SubInitialize methods....Or, you make a Factory that makes Whatever according to a pattern.   