如何使用名称空间保持访问级别 [英] how to keep access level withing namespace

查看:69
本文介绍了如何使用名称空间保持访问级别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们怎样才能使类的成员只能在名称空间内访问.
请看下面的场景

How can we make a member of class accessible inside the namespace ONLY.
please look the following scenerio

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.

推荐答案

为您的"abc"类创建一个单独的项目,并为您的开发人员引用该项目,那么他们将无权访问"abc" DLL.
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.


名称空间与访问无关.它们不打开或限制对任何内容的访问,它们只是在命名中引入了新的层次类型.

当然,建议您使用名称空间来命名时有更多的自由.换句话说,您可以定义名称空间Abc和类型A.这样,您应该将此类型命名为Abc.A,但是您也可以具有一些Cda.A,并且通过这种方式,在同一个命名空间中,您可以具有两个使用完全限定名称的A.使用关键字"using Abc"或"using MyA = Abc.A; using Another.A = Cde.A;"根本不会改变任何内容.这只是完全限定类型名称或使用别名的一种方法. 这仅是名称解析的问题,而不是访问权限.

访问由类型和成员访问修饰符privateinternal protectedinternalpublic控制.阅读并学习使用它们,它将解决您的问题.

另外,请记住,访问限制不会做任何访问保护.它旨在保护您免受自己的错误的影响.任何人都可以使用Reflection定期访问任何非公共类型和任何程序集的成员.这甚至不是黑客,而是标准的反射功能. 访问修饰符仅用于限制对类型及其成员的显式访问.



您的var x是一个堆栈(本地)变量,它在内部范围内,根本无法从外部看到.使它在不同范围内可见的唯一方法是在该范围内声明匿名方法,这是一种引入 closure 效果的方法,请参见http://en.wikipedia.org/wiki/Closure_%28computer_science%29 [创建新线程的循环会出现问题 [ ^ ],
什么是C#中的匿名类型? [ ^ ].

—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.



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


这篇关于如何使用名称空间保持访问级别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆