内部构造函数“可见”在集会之外。编译错误? [英] Internal constructor "visible" outside of assembly. Compiler bug?

查看:73
本文介绍了内部构造函数“可见”在集会之外。编译错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我有编译问题。请看下面的代码。

我有一个基于三层的应用程序:一个数据层

(Foo.DataManager),一个业务层(Foo) .Kernel)和一个web

表示层(Foo.WebFiles)。数据层只能通过业务层访问,因此我不希望在表示层中引用

数据层。

在业务层中,我有一个名为CategoryItem的类,

有一个公共构造函数,它应该能够从表示层实例化
和一个内部构造函数只能在同一个程序集中实例化
。内部的
构造函数有一个参数,它引用了一个类,位于数据层的
TransactionHandler。这是

问题开始的地方。当我在

CategoryItem类中添加这样的构造函数然后尝试编译ActionMenu类时,我得到

以下编译错误:


错误CS0012:类型''Foo.DataManager.TransactionHandler''是在未引用的程序集中定义的
。你必须添加一个

引用汇编''Foo.WMS.DataManager''。


我真的不明白为什么我会收到这个错误,因为我明白了,

这根本不会有任何问题,因为对Foo.DataManager的

引用的构造函数是INTERNAL,甚至不会是

可用于表示层。这是编译器中的错误还是

我错过了什么?而且,我如何解决这个问题?

正如我之前所说,我真的不希望从

表示层引用到数据层。


顺便说一下,如果我将这个构造函数设置为

PROTECTED或PRIVATE,我仍然会得到相同的编译错误。


我感谢任何帮助。提前谢谢。


最好的问候

马丁

/ ***头等舱位于Foo.Datamanager.dll * ******************* /


名称空间Foo.DataManager {

公共类TransactionHandler {

公共TransactionHandler(){

}

}

}


/ ***位于Foo.Kernel.dll的第二类,有*************** /

/ ***对Foo的引用。 Datamanager.dll上面。 *************** /


使用Foo.DataManager;


名称空间Foo.Kernel {


公共类CategoryItem {


public CategoryItem(){

}


内部CategoryItem(TransactionHandler交易){

}


}

}


/ ***另一个没有引用Foo.Datamanager.dll ******* /

/ ***的类但想要实例化Foo.Kernel.CategoryItem * ****** /


使用Foo.Kernel;


名称空间Foo.WebFiles {

public class ActionMenu:System.Web.UI.Page {

private void AddItem(){

CategoryItem category = new CategoryItem();

}

}

}


/ ******************* ******************************* ***************** /

Hi,

I have a compiling problem. Please take a look at the code below.
I have an application that is built upon three tiers: one data tier
(Foo.DataManager), one business tier (Foo.Kernel) and one web
presentation tier (Foo.WebFiles). The data tier shall only be
accessible thru the business tier so I do NOT want a reference to
the data tier in the presentation tier.
In the business tier I have a class with the name CategoryItem that
have one public constructor, that shall be able to instantiate from
the presentation tier, and one internal constructor that shall only
be instantiated from within the same assembly. The internal
constructor have an argument with a reference to a class,
TransactionHandler, located in the data tier. And here is where the
problems begin. When I add a constructor like this in the
CategoryItem class and then try to compile the ActionMenu class I get
the following compiling error:

error CS0012: The type ''Foo.DataManager.TransactionHandler'' is
defined in an assembly that is not referenced. You must add a
reference to assembly ''Foo.WMS.DataManager''.

I really do not understand why I get this error, cause as I see it,
this shall not be any problem at all because the constructor with a
reference to the Foo.DataManager is INTERNAL and shall not even be
visible for the presentation tier. Is this a bug in the compiler or
have I missed something here? And, how do I come around this problem?
As I said before I realy do NOT want a reference from the
presentation tier to the data tier.

By the way, it doesn''t matter if I set this constructor to be
PROTECTED or PRIVATE, I still get the same compiling error.

I appreciate any help. Thank you in advance.

Best regards
Martin
/*** First class located in Foo.Datamanager.dll ********************/

namespace Foo.DataManager {
public class TransactionHandler {
public TransactionHandler(){
}
}
}

/*** Second class located in Foo.Kernel.dll that has ***************/
/*** a reference to the Foo.Datamanager.dll above. ***************/

using Foo.DataManager;

namespace Foo.Kernel {

public class CategoryItem {

public CategoryItem(){
}

internal CategoryItem(TransactionHandler transaction) {
}

}
}

/*** Another class with NO reference to Foo.Datamanager.dll *******/
/*** but that wants to instantiate Foo.Kernel.CategoryItem *******/

using Foo.Kernel;

namespace Foo.WebFiles {
public class ActionMenu : System.Web.UI.Page {
private void AddItem(){
CategoryItem category = new CategoryItem();
}
}
}

/************************************************** *****************/

推荐答案

马丁,

恕我直言你的问题在别的地方。我编译了你发布的代码和

编译好了。即使

构造函数是公开的,只要你不使用它就不会有编译问题。如果您调用方法或访问具有

参数或返回值为TransactionHandler的表示层中的属性,则会发生此错误



请稍等一下,在你的CategoryItem中你有


public TransactionHandler Foo()

{

返回...;

}


即使使用这种方法,你也必须能够编译。

但如果在某处您的演示级别


....

Foo();

....


即使你没有使用返回值你也会得到那个错误。


所以,检查你的代码。我想你错过了什么。


-

HTH

Stoitcho Goutsev(100)[C#MVP]

" Martin Oddman" < SP ** @ me.not>在消息中写道

news:eX ************** @ TK2MSFTNGP12.phx.gbl ...
Hi Martin,

IMHO your problem is somewhere else. I compiled the code you''ve posted and
it compiles ok. You shouldn''t have any problems compiling even if the
constructor was public as long as you don''t use it. This error will happen
if you call a method or access a property in you presentation tier that has
parameter or return value of type TransactionHandler.

Let say for a moment that in your CategoryItem you have

public TransactionHandler Foo()
{
return...;
}

Even with this method you have to be able to compile.
But if somewhere in your presentation tier you have

....
Foo();
....

even though you don''t use the return value you''ll get that error.

So, check you code. I think you''ve missed something.

--
HTH
Stoitcho Goutsev (100) [C# MVP]
"Martin Oddman" <sp**@me.not> wrote in message
news:eX**************@TK2MSFTNGP12.phx.gbl...

我有一个编译问题。请看下面的代码。
我有一个基于三层的应用程序:一个数据层
(Foo.DataManager),一个业务层(Foo.Kernel)和一个web 在业务层中,我有一个名为的类CategoryItem,它有一个公共构造函数,应该能够从表示层实例化,以及一个只能在同一个程序集中实例化的内部构造函数。内部构造函数有一个参数,它引用了位于数据层中的类,即TransactionHandler。这就是
问题开始的地方。当我在
CategoryItem类中添加这样的构造函数然后尝试编译ActionMenu类时,我得到以下编译错误:

错误CS0012:类型''Foo .DataManager.TransactionHandler''在未引用的程序集中定义。你必须添加一个
引用汇编''Foo.WMS.DataManager''。

我真的不明白为什么我会收到这个错误,因为我看到它,
这根本不应该是任何问题,因为具有对Foo.DataManager的引用的构造函数是INTERNAL,甚至对于表示层也不可见。这是编译器中的错误还是我错过了什么?而且,我如何解决这个问题?
正如我之前所说,我真的不希望从
表示层引用到数据层。

顺便说一下,如果我将此构造函数设置为
PROTECTED或PRIVATE,我仍然会得到相同的编译错误。

我感谢任何帮助。提前谢谢。

最好的问候
马丁

/ ***头等舱位于Foo.Datamanager.dll ********* *********** /

名称空间Foo.DataManager {
公共类TransactionHandler {
公共TransactionHandler(){
} >}
}
/ ***位于Foo.Kernel.dll中的第二个类有*************** /
/ ***对上面的Foo.Datamanager.dll的引用。 *************** /

使用Foo.DataManager;

命名空间Foo.Kernel {

公共类CategoryItem {
公共CategoryItem(){
}
内部CategoryItem(TransactionHandler事务){
}

}

/ ***另一个没有引用Foo.Datamanager.dll ******* /
/ ***的类但想要实例化Foo.Kernel.CategoryItem ******* /

使用Foo.Kernel;

命名空间Foo.WebFiles {
公共类ActionMenu:System.Web .UI.Page {
private void AddItem(){
CategoryItem category = new CategoryItem();
}
}
}

/ ******************* * ***************** /
Hi,

I have a compiling problem. Please take a look at the code below.
I have an application that is built upon three tiers: one data tier
(Foo.DataManager), one business tier (Foo.Kernel) and one web
presentation tier (Foo.WebFiles). The data tier shall only be
accessible thru the business tier so I do NOT want a reference to
the data tier in the presentation tier.
In the business tier I have a class with the name CategoryItem that
have one public constructor, that shall be able to instantiate from
the presentation tier, and one internal constructor that shall only
be instantiated from within the same assembly. The internal
constructor have an argument with a reference to a class,
TransactionHandler, located in the data tier. And here is where the
problems begin. When I add a constructor like this in the
CategoryItem class and then try to compile the ActionMenu class I get
the following compiling error:

error CS0012: The type ''Foo.DataManager.TransactionHandler'' is
defined in an assembly that is not referenced. You must add a
reference to assembly ''Foo.WMS.DataManager''.

I really do not understand why I get this error, cause as I see it,
this shall not be any problem at all because the constructor with a
reference to the Foo.DataManager is INTERNAL and shall not even be
visible for the presentation tier. Is this a bug in the compiler or
have I missed something here? And, how do I come around this problem?
As I said before I realy do NOT want a reference from the
presentation tier to the data tier.

By the way, it doesn''t matter if I set this constructor to be
PROTECTED or PRIVATE, I still get the same compiling error.

I appreciate any help. Thank you in advance.

Best regards
Martin
/*** First class located in Foo.Datamanager.dll ********************/

namespace Foo.DataManager {
public class TransactionHandler {
public TransactionHandler(){
}
}
}

/*** Second class located in Foo.Kernel.dll that has ***************/
/*** a reference to the Foo.Datamanager.dll above. ***************/

using Foo.DataManager;

namespace Foo.Kernel {

public class CategoryItem {

public CategoryItem(){
}

internal CategoryItem(TransactionHandler transaction) {
}

}
}

/*** Another class with NO reference to Foo.Datamanager.dll *******/
/*** but that wants to instantiate Foo.Kernel.CategoryItem *******/

using Foo.Kernel;

namespace Foo.WebFiles {
public class ActionMenu : System.Web.UI.Page {
private void AddItem(){
CategoryItem category = new CategoryItem();
}
}
}

/************************************************** *****************/



我对此进行了一些研究在编译器中发现了一个明显的错误。

如果你有一个内部构造函数和一个公共构造函数与

相同数量的参数和内部构造函数有一个

参数,其中引用了一个无法访问的类

对于调用公共构造函数的类,然后你得到这个

错误。

我没有注意到在我的第一封邮件中所以我的第一个例子是不是

100%正确。以下是如何重现它的确切例子:


/ *************************** *********************** ***************** /

/ ***创建一个名为Foo.Datamanager的项目。 *** /

/ ***在该项目中创建一个名为TransactionHandler.cs的文件*** /

/ ***并将此代码粘贴到该文件中。 *** /

/ ************************************* ************* ***************** /


名称空间Foo.DataManager {

公共类TransactionHandler {

公共TransactionHandler(){

}

}

}


/ ************************************* ************* ***************** /

/ ***创建一个名为Foo.Kernel的项目。 *** /

/ ***在该项目中创建一个名为CategoryItem.cs的文件*** /

/ ***并将此代码粘贴到该文件中。 *** /

/ ***然后从这个项目中引用Foo.Datamanager。 *** /

/ ************************************* ************* ***************** /


使用Foo.DataManager;


名称空间Foo.Kernel {


公共类CategoryItem {


public CategoryItem(){

}


public CategoryItem(string s){

}


internal CategoryItem( TransactionHandler交易){

}


}

}


/ ** ************************************************ ** *************** /

/ ***创建一个名为Foo.UserInterface的项目。 *** /

/ ***在该项目中创建一个名为UserInterface.cs的文件*** /

/ ***并将此代码粘贴到该文件中。 *** /

/ ***然后从这个项目中引用Foo.Kernel。 *** /

/ ************************************* ************* ***************** /


使用Foo.Kernel;


名称空间Foo.UserInterface {

公共类GUI {

public void AddItem(){

CategoryItem category = new CategoryItem(s);

}

}

}


/ ** ************************************************ ** *************** /

/ ************************* ************************* ***************** /

/ ********************************************* ** ***************** /


如果我删除
$ b中的中间构造函数或最后一个构造函数$ b CategoryItem它的工作原理。

如果我在内部构造函数上添加一个额外的伪参数,那么

内部和公共构造函数会得到不同数量的参数

也有效。

还有一个有趣的事情就是它只要你工作就可以使用

使用默认con结构,而不是CategoryItem(s)构造函数
来自UserInterface类的
...

但是一旦公共和内部构造函数具有相同的

参数的数量我在尝试编译时收到此错误消息:


错误CS0012:类型''Foo.DataManager.TransactionHandler''是

在未引用的程序集中定义。你必须在程序集''Foo.DataManager'中添加一个

引用。


公共参数的类型无关紧要

构造函数有。


我看不出这不是编译器中的错误。


/ Martin
I have made some research on this and found out an obvious bug in
the compiler.
If you have one internal constructor and one public constructor with
the same number of arguments and the internal constructor have an
argument with a reference to a class that shall not be accessible
for classes that calls the public constructor then you get this
error.
I hadn''t noticed that in my first mail so my first example wasn''t
100% correct. Here is an exact exempel how to reproduce it:

/************************************************** *****************/
/*** Create a project called Foo.Datamanager. ***/
/*** Create a file in that project named TransactionHandler.cs ***/
/*** and paste this code into that file. ***/
/************************************************** *****************/

namespace Foo.DataManager {
public class TransactionHandler {
public TransactionHandler(){
}
}
}

/************************************************** *****************/
/*** Create a project called Foo.Kernel. ***/
/*** Create a file in that project named CategoryItem.cs ***/
/*** and paste this code into that file. ***/
/*** Then make a reference from this project to Foo.Datamanager. ***/
/************************************************** *****************/

using Foo.DataManager;

namespace Foo.Kernel {

public class CategoryItem {

public CategoryItem(){
}

public CategoryItem(string s){
}

internal CategoryItem(TransactionHandler transaction) {
}

}
}

/************************************************** *****************/
/*** Create a project called Foo.UserInterface. ***/
/*** Create a file in that project named UserInterface.cs ***/
/*** and paste this code into that file. ***/
/*** Then make a reference from this project to Foo.Kernel. ***/
/************************************************** *****************/

using Foo.Kernel;

namespace Foo.UserInterface {
public class GUI {
public void AddItem(){
CategoryItem category = new CategoryItem(s);
}
}
}

/************************************************** *****************/
/************************************************** *****************/
/************************************************** *****************/

If I remove either the middle constructor or the last constructor in
CategoryItem it works.
If I put an extra dummy argument on the internal constructor, so the
internal and public constructor get different number of arguments it
also works.
And one other funny thing is that it also works as long as you just
use the default constructor and not the CategoryItem(s) constructor
from the UserInterface class...
But as soon as the public and the internal constructors has the same
numbers of arguments I get this error message when trying to compile:

error CS0012: The type ''Foo.DataManager.TransactionHandler'' is
defined in an assembly that is not referenced. You must add a
reference to assembly ''Foo.DataManager''.

It doesn''t even matter what type the argument of the public
constructor has.

I can''t see this is anything than a bug in the compiler.

/Martin


我刚刚注意到了这个问题,我在上一个例子中错过了一些代码。阅读我的上一篇文章,你会发现无法编译的代码。我们在3台不同的计算机上完成了3个不同的项目,结果相同...


Stoitcho Goutsev(100)[C#MVP]写道:
I just noticed the problem and that I missed some code in my last example. Read my last post and you will find code that do not compile. We''ve done 3 differnt projects on 3 different computers with the same result here...

Stoitcho Goutsev (100) [C# MVP] wrote:
嗨马丁,

恕我直言,你的问题就在别的地方。我编译了你发布的代码并且
编译好了。即使
构造函数是公开的,只要你不使用它就不会有编译问题。如果您调用方法或访问具有
参数或返回值为TransactionHandler的表示层中的属性,则会发生此错误。

请稍等一下你有你的CategoryItem

公共TransactionHandler Foo()
{
返回......;
}
即使有这种方法你也有能够编译。
但如果你的演示层中的某个地方你有......
Foo();
......
<即使你没有使用返回值,你也会得到错误。

所以,检查你的代码。我想你错过了什么。
Hi Martin,

IMHO your problem is somewhere else. I compiled the code you''ve posted and
it compiles ok. You shouldn''t have any problems compiling even if the
constructor was public as long as you don''t use it. This error will happen
if you call a method or access a property in you presentation tier that has
parameter or return value of type TransactionHandler.

Let say for a moment that in your CategoryItem you have

public TransactionHandler Foo()
{
return...;
}

Even with this method you have to be able to compile.
But if somewhere in your presentation tier you have

...
Foo();
...

even though you don''t use the return value you''ll get that error.

So, check you code. I think you''ve missed something.



这篇关于内部构造函数“可见”在集会之外。编译错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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