公共类扩展内部类 [英] Public class extends internal class
问题描述
我有一个内部类 A
,做了很多的东西,我有一组的其他类 B
, D
,电子
从类继承 A
。当然,这是行不通的,因为编译器会抱怨不能够延长内部之一(见的这个一个解释)。
I have an internal class A
that does a lot of stuff and I have a set of other classes B
, D
, E
that inherit from class A
. Of course this won't work as the compiler will complain about a public class not being able to extend an internal one (see this for an explanation).
我不希望 A
来公开,因为我不希望我的用户继承它。我希望他们只使用 B
, D
,电子
I don't want A
to be public as I don't want my users to inherit from it. I want them to only use B
, D
, E
.
我知道我可以使用隐藏智能感知类 [EditorBrowsable(EditorBrowsableState.Never)]
但用户仍然可以访问它使用反射:(
I know I can hide the class from Intellisense using but the users can still access it using reflection :(
有没有一种方法,我可以做到这一点。
Is there a way I can achieve this?
推荐答案
假设 A
是在装配的 A 和 B
, D
和电子
是装配的 BDE 的,你可以声明<一个HREF =http://msdn.microsoft.com/en-us/library/system.runtime.compilerservices.internalsvisibletoattribute.aspx> InternalsVisibleTo
属性上的 A 的制作的 BDE 的朋友总成。像这样, B
, D
和电子
将能够看到 A
。
Assuming that A
is in the assembly A, and B
, D
and E
are in the assembly BDE, you can declare the InternalsVisibleTo
attribute on A to make BDE a friend assembly. Like this, B
, D
and E
will be able to see A
.
不过,你不能从 A
继承,因为超不能比的子类的能见度较低,你可以解决这个问题通过声明 A
的公开,同时使所有构造函数的内部的的的。通过这种方式,只能用代码的内部的访问 A
将能够从 A
导出
Still, you cannot inherit from A
, as superclasses cannot have a lower visibility than subclasses. You can solve this by declaring A
public while making all of its constructors internal. This way, only code with internal access to A
will be able to derive from A
.
这篇关于公共类扩展内部类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!