Click here to Skip to main content
15,896,264 members
Please Sign up or sign in to vote.
2.00/5 (2 votes)
See more:
How can we make a member of class accessible inside the namespace ONLY.
please look the following scenerio

C#
namespace abc
{
 class a
 {
   internal int xyz;
 }
}
namespace def
{
 class d
 {
   public d()
   {
    a obj= new a();
    var x= obj.xyz;/// I want to restrict the programmer of namespace def not to  access xyz where as I want to keep xyz public inside the namespace abc

   }
 }
}

Thank you for assistance.
Posted

Create a separate project for your "abc" class and reference that for your developer, then they will not have access to the internals of the "abc" DLL.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 29-Nov-11 14:28pm    
This is irrelevant by two reasons: 1) OP needs explanation that access modifiers should be used instead of name spaces, 2) OP talks about a stack variables.
--SA
Name spaces has nothing to do with access. They do not open or restrict access to anything, they just introduce an additional level in naming of types.

Of course, you are recommended to use namespaces to have more freedom in naming. In other words, you can define name space Abc and a type A. In this way, you should name this type Abc.A, but you also can have some Cda.A and, in this way, in the same namespace you can have two As using fully qualified names. Using keyword 'using Abc' or 'using MyA = Abc.A; using Another.A = Cde.A;' does not change anything at all; this is only a way to fully-qualify type names or use aliases. This is only a matter of name resolution, not access.

The access in controlled by the type and member access modifiers private, internal protected, internal and public. Read about them and learn to use them, it will solve your problem.

Also, keep in mind that limitation of access does not do any access protection. It is designed to protect you from your own mistakes. Anyone can access any non-public types and members of any assemblies in a regular way using Reflection. This is not even a hack but a standard Reflection functionality. Access modifiers are only designed to restrict explicit access to types and their members.

[EDIT]

Your var x is a stack (local) variable, it is in the inner scope and is not visible from outside at all. The only way to make it visible from a different scope is anonymous method declared inside this scope, and this is a way to introduce the effect of closure, see http://en.wikipedia.org/wiki/Closure_%28computer_science%29[^].

This is an advanced topic though. Please see my past solutions:
Looping for creating new thread give some problem[^],
What are Anonymous Types in C#?[^].

—SA
 
Share this answer
 
v2
Comments
J imran 29-Nov-11 14:39pm    
i learned making assembly files we can limit the access of certain variables. Reflaction is new thing for me. I am going to learn it.
Sergey Alexandrovich Kryukov 29-Nov-11 18:16pm    
Reflection is kind of unrelated to it. Basically, you will need Reflection to create a system with plug-ins as this is a way to load assembly during run-time and then find the interfaces you need and invoke everything you need. I have detailed description here at CodeProject answers in case you need any help.

Good luck,
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900