如何在解决方案内部建立类 [英] How to make a class internal to a solution

查看:109
本文介绍了如何在解决方案内部建立类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我有一个包含多个项目的解决方案.

我想在另一个项目中使用在一个项目中声明的类,因此从我要在其中使用它的项目中引用它.

理想情况下,该类为内部",但是这使我无法在第二个项目中使用它,但是如果我将其公开,则它将在解决方案外部可见.

有没有一种方法可以声明一个类,该类仅在解决方案中的项目中可见,而在分发时则对外部客户不可见.这可能吗?

在此先感谢

George

Hi All,

I have a solution with several projects in it.

I would like to use a class declared in one of the projects in another one and therefore reference it from the project in which i wish to use it.

Ideally this class would be "Internal" however this prevents me from using it in the second project however if i make it public it will be visible outside of the solution.

Is there a way of declaring a class that will only be visible to projects within a solution and not external customers when it is distributed. Is this possible?

Thanks in advance

George

推荐答案

否.考虑一下:

public:访问不受限制.

protected:访问仅限于包含类或从包含类派生的类型.

Internal:访问仅限于当前程序集.

protected internal:访问限于从包含的类派生的当前程序集或类型.

private:访问仅限于包含类型.


干杯
No. Consider this:

public: Access is not restricted.

protected: Access is limited to the containing class or types derived from the containing class.

Internal: Access is limited to the current assembly.

protected internal: Access is limited to the current assembly or types derived from the containing class.

private: Access is limited to the containing type.


Cheers


Runtime.CompilerServices.InternalsVisibleToAttribute应该这样做.

请参见 http://msdn.microsoft.com/en-us/library/0tke9fxk.aspx [ ^ ]有关使用的详细信息. br/>
艾伦.
The Runtime.CompilerServices.InternalsVisibleToAttribute should do this.

See http://msdn.microsoft.com/en-us/library/0tke9fxk.aspx[^] for details of use.

Alan.


到目前为止,无法通过在C#中使用作用域关键字来实现.您只能实现从其他程序集(而不是您的程序集)访问时会引发异常的逻辑.

附带说明一下,让我告诉您,甚至可以使用Reflection来构造具有私有构造函数的类.

这是我的看法.让我们看看其他人可以向您提出什么建议:)

问候
As of now, it is not possible via using scope keywords in C#. You can only implement a logic that would throw an exception when accessed from another assembly other than yours.

On a side note, let me tell you that even a class with private constructor can be constructed using Reflection.

This is my opinion on it. Let''s see what others can propose to you :)

Regards


这篇关于如何在解决方案内部建立类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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