使构造函数仅对某个类可见 [英] Making constructor visible only to a certain class

查看:76
本文介绍了使构造函数仅对某个类可见的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个设计问题。


我有一个包含十几个类的项目,内有受保护的

内部构造函数。


我保护内部构造函数的原因是为了防止其他程序集中的
类实例化它们。有一个工厂

类实例化它们。只有工厂应该这样做,因为

这些类中的一些具有将被创建的子类。

决定实例化哪个类属于工厂。


我的工作正常,除了一个细节。


因为构造函数是内部保护的,所以项目中的

类可以绕过工厂。通过在项目中手动检查,确保不会发生这种情况并不太困难。

但如果编译器可以更容易和更安全抓住这个

的错误。


理想情况下,我希望构造函数的可见性更加受限

而不是b $ b。内部" ;.我想说明只有工厂才能看到它们的价值。即,而不是这个:


保护内部Foo(){}


我想能够写:


protected visibleto(工厂)Foo(){}


不幸的是,C#具有非常粗粒度的可见性修饰符。


我可以想到几个解决办法:


解决方法1.声明构造函数受保护并让工厂

使用反射来实例化它们。


解决方法2.在工厂内声明一个嵌套的受保护类,用于

它实例化的每个类。嵌套类将具有

公共构造函数。然后只有工厂或其后代才能实现这些类。正是我想要的!


两种解决方案都比原来的问题更糟糕。反思

(解决方法1)会导致类型安全,所以我要避免它,除非我绝对需要它b / b
。此外,它很慢。另一方面,解决方法2将是代码膨胀的坏例子。


在C#中有一个不错的解决方案吗?


- Peter Gummer

This is a design question.

I have a project containing a dozen or so classes with protected
internal constructors.

My reason for having protected internal constructors is to prevent
classes in other assemblies from instantiating them. There is a factory
class that instantiates them. Only the factory should do so, because
some of these classes have subclasses that will be created instead. The
decision of which class to instantiate belongs to the factory.

I''ve got this working just fine, except one detail.

Because the constructors are protected internal, it''s possible for
classes inside the project to by-pass the factory. It''s not too hard to
ensure this doesn''t happen, by checking manually within the project;
but it would be easier and safer if the compiler could catch this
mistake.

Ideally, I''d like the constructors to have a more restricted visibility
than "internal". I''d like to specify that only the factory can see
them. I.e., instead of this:

protected internal Foo() {}

I''d like to be be able to write:

protected visibleto(Factory) Foo() {}

Unfortunately, C# has extremely coarse-grained visiblity modifiers.

I can think of a couple of work-arounds:

Work-around 1. Declare the constructors protected and let the factory
use reflection to instantiate them.

Work-around 2. Declare a nested protected class within the factory for
each of the classes it instantiates. The nested classes would have
public constructors. Then only the factory, or its descendants, could
instantiate the classes. Exactly what I want!

Both solutions would be worse than the original problem. Reflection
(Work-around 1) kills type-safety, so I avoid it unless I absolutely
need it; besides, it''s slow. Work-around 2, on the other hand, would be
a bad case of code-bloat.

Is there a decent solution to this in C#?

-- Peter Gummer

推荐答案

如果有一个朋友,那就太好了。 c#中的概念。如果我可能会问,为什么

你能不相信你自己组装中的代码吗?


" Peter Gummer" < PE ****************** @ hotnospammail.com>在留言中写道

news:ec ************** @ TK2MSFTNGP09.phx.gbl ...
It would be nice if there was a "friend" concept in c#. If I may ask, why
can you not trust the code within your own assembly?

"Peter Gummer" <pe******************@hotnospammail.com> wrote in message
news:ec**************@TK2MSFTNGP09.phx.gbl...
这是一个设计问题。

我有一个项目包含十几个带有受保护内部构造函数的类。

我保护内部构造函数的原因是为了防止
其他程序集中的类来实例化它们。有一个工厂
类来实例化它们。只有工厂应该这样做,因为这些类中的一些具有将被创建的子类。确定实例化哪个类的决定属于工厂。

除了一个细节之外,我的工作正常。

因为构造函数是受保护的内部,项目内的
类可以绕过工厂。通过在项目中手动检查来确保不会发生这种情况并不太难;
但如果编译器能够捕获这个错误,它会更容易和更安全。

理想情况下,我希望构造函数的可见性比内部更受限制。我想说明只有工厂才能看到它们。即,而不是这个:

受保护的内部Foo(){}

我希望能够写:

受保护visibleto(工厂)Foo(){}

不幸的是,C#具有非常粗粒度的可见性修饰符。

我可以想到几个解决方法:

解决方法1.声明构造函数受保护并让工厂使用反射来实例化它们。

解决方法2.在工厂内声明一个嵌套的受保护类
它实例化的每个类。嵌套类将具有
公共构造函数。然后只有工厂或其后代才能实例化这些类。正是我想要的!

两种解决方案都比原来的问题更糟糕。反思
(解决方法1)会杀死类型安全,所以除非我绝对需要它,否则我会避免它;此外,它很慢。另一方面,解决方法2将是一个代码膨胀的坏情况。

在C#中有没有一个像样的解决方案?

- Peter Gummer
This is a design question.

I have a project containing a dozen or so classes with protected
internal constructors.

My reason for having protected internal constructors is to prevent
classes in other assemblies from instantiating them. There is a factory
class that instantiates them. Only the factory should do so, because
some of these classes have subclasses that will be created instead. The
decision of which class to instantiate belongs to the factory.

I''ve got this working just fine, except one detail.

Because the constructors are protected internal, it''s possible for
classes inside the project to by-pass the factory. It''s not too hard to
ensure this doesn''t happen, by checking manually within the project;
but it would be easier and safer if the compiler could catch this
mistake.

Ideally, I''d like the constructors to have a more restricted visibility
than "internal". I''d like to specify that only the factory can see
them. I.e., instead of this:

protected internal Foo() {}

I''d like to be be able to write:

protected visibleto(Factory) Foo() {}

Unfortunately, C# has extremely coarse-grained visiblity modifiers.

I can think of a couple of work-arounds:

Work-around 1. Declare the constructors protected and let the factory
use reflection to instantiate them.

Work-around 2. Declare a nested protected class within the factory for
each of the classes it instantiates. The nested classes would have
public constructors. Then only the factory, or its descendants, could
instantiate the classes. Exactly what I want!

Both solutions would be worse than the original problem. Reflection
(Work-around 1) kills type-safety, so I avoid it unless I absolutely
need it; besides, it''s slow. Work-around 2, on the other hand, would be
a bad case of code-bloat.

Is there a decent solution to this in C#?

-- Peter Gummer



Peter Rilling写道:
Peter Rilling wrote:
如果有一个朋友那就太好了; c#中的概念。如果我可能会问,为什么你不能相信你自己组装中的代码呢?
It would be nice if there was a "friend" concept in c#. If I may
ask, why can you not trust the code within your own assembly?




嗯,只有在我无懈可击的情况下,我才能相信它。我只是人类,

所以我肯定会犯错误:-)


忘记使用工厂很容易。新的开发人员将需要了解我的设计涉及到工厂。我有

记录了设计,但如果有人未能阅读文件

那么他们可能只会写new Foo()。而不是

Factory.NewFoo。


我的偏好是埃菲尔,而不是C ++,所以我希望选择性

export"而不是朋友。但是你明白了。


- Peter Gummer



Well, I can trust it only insofar as I am infallible. I am but human,
so I''m sure to make mistakes :-)

It would be very easy to forget to use the factory. New developers will
also have to learn that my design involves calling the factory. I have
documented the design, but if someone fails to read the documentation
then they will probably just write "new Foo()" instead of
"Factory.NewFoo".

My preference is for Eiffel, not C++, so I was wishing for "selective
export" rather than "friend". But you get the idea.

-- Peter Gummer


" Peter Gummer" < PE ****************** @ hotnospammail.com> aécritdansle

message de news: ec *** ***********@TK2MSFTNGP09.phx.gbl ...


|因为构造函数是内部保护的,所以

|是可能的项目内部的类绕过工厂。

|并不太难通过在项目中手动检查确保不会发生这种情况;

|但如果编译器可以捕获这个更容易和更安全

|错误。


如何使用InternalsVisibleTo属性允许一个程序集

访问另一个程序集内部的内容。


这意味着您可以在一个程序集中使用内部构造函数声明您的类,并在朋友中声明工厂。部件。只要工厂

是朋友中唯一的班级。汇编,然后它将是唯一可以实例化你的类型的类。


乔安娜


-

Joanna Carter [TeamB]

顾问软件工程师
"Peter Gummer" <pe******************@hotnospammail.com> a écrit dans le
message de news: ec**************@TK2MSFTNGP09.phx.gbl...

| Because the constructors are protected internal, it''s possible for
| classes inside the project to by-pass the factory. It''s not too hard to
| ensure this doesn''t happen, by checking manually within the project;
| but it would be easier and safer if the compiler could catch this
| mistake.

How about using the InternalsVisibleTo attribute to allow one assembly
access to something internal from another assembly.

This means that you could declare your classes with internal constructors in
one assembly and the factory in a "friend" assembly. As long as the factory
is the only class in the "friend" assembly, then it will be the only class
that can instantiate your types.

Joanna

--
Joanna Carter [TeamB]
Consultant Software Engineer


这篇关于使构造函数仅对某个类可见的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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