[BUG]编译无法解析命名空间类差异 [英] [BUG] Compile can't resolve namespace class differences

查看:57
本文介绍了[BUG]编译无法解析命名空间类差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

似乎C#编译无法解决

命名空间与明确无误的情况下的类之间的区别。注意

它看到命名空间C在课程C之前。我相信它应该看到班级C。首先。


test1.cs:

=============


使用系统;

使用AC;


名称空间AB

{

公共类D

{

D(){C c = new C(); }

}

}


test2.cs:

======= ======


使用系统;


名称空间AC

{

公共舱C

{

int X;

}

}

=============

C:\> csc / target:library test1.cs test2.cs

Microsoft(R)Visual C#.NET编译器版本7.00.9466

for Microsoft(R).NET Framework版本1.0.3705

版权所有(C) Microsoft Corporation 2001.保留所有权利。


test1.cs(8,9):错误CS0118:''A.C''表示''命名空间''其中a'' class''是

预期

It seems like the C# compile can''t resolve the difference between a
namespace and a class in a situation that is clearly unambiguous. Note
that it sees the namespace "C" before the class "C". I believe it
should see the class "C" first.

test1.cs:
=============

using System;
using A.C;

namespace A.B
{
public class D
{
D() { C c = new C(); }
}
}

test2.cs:
=============

using System;

namespace A.C
{
public class C
{
int X;
}
}

=============

C:\>csc /target:library test1.cs test2.cs
Microsoft (R) Visual C# .NET Compiler version 7.00.9466
for Microsoft (R) .NET Framework version 1.0.3705
Copyright (C) Microsoft Corporation 2001. All rights reserved.

test1.cs(8,9): error CS0118: ''A.C'' denotes a ''namespace'' where a ''class'' was
expected

推荐答案

类不能具有相同的命名空间名称。

Christian。

" Matt Mastracci" <毫安** @ aclaro.com> ha scritto nel messaggio

新闻:%2 **************** @ TK2MSFTNGP10.phx.gbl ...
Classes can not have same name of namespaces.
Christian.
"Matt Mastracci" <ma**@aclaro.com> ha scritto nel messaggio
news:%2****************@TK2MSFTNGP10.phx.gbl...
它看起来C#编译无法解决
命名空间和类中明显无歧义的情况之间的区别。注意
它看到名称空间C。在课程C之前。我相信它应该看到班级C。首先。

test1.cs:
=============

使用系统;
使用AC;

名称空间AB
公共类D
{(D)(C c = new C(); }
}

test2.cs:
=============

使用系统;

命名空间AC
{
公共课C
{X /
}
}

=============

C:\> csc / target:library test1.cs test2.cs
Microsoft(R)Visual C#.NET编译器版本7.00.9466
Microsoft(R).NET Framework版本1.0.3705
版权所有(C)Microsoft Corporation 2001.保留所有权利。

test1。 cs(8,9):错误CS0118:''A.C''表示''名称空间'',其中''类''
是预期的
It seems like the C# compile can''t resolve the difference between a
namespace and a class in a situation that is clearly unambiguous. Note
that it sees the namespace "C" before the class "C". I believe it
should see the class "C" first.

test1.cs:
=============

using System;
using A.C;

namespace A.B
{
public class D
{
D() { C c = new C(); }
}
}

test2.cs:
=============

using System;

namespace A.C
{
public class C
{
int X;
}
}

=============

C:\>csc /target:library test1.cs test2.cs
Microsoft (R) Visual C# .NET Compiler version 7.00.9466
for Microsoft (R) .NET Framework version 1.0.3705
Copyright (C) Microsoft Corporation 2001. All rights reserved.

test1.cs(8,9): error CS0118: ''A.C'' denotes a ''namespace'' where a ''class'' was expected



马特,


这不是明确的,实际上是不允许的。因为这个原因,你不能在命名空间中使用与命名空间明确相同的名称的类中使用

。如果你在C类中有一个嵌套类型怎么办?编译器

必须在不知道C是什么的情况下进行猜测,并且我不会在任何编译器中信任




希望这会有所帮助。


-

- Nicholas Paldino [.NET / C#MVP]

- ni ************** @ exisconsulting.com


" Matt Mastracci" <毫安** @ aclaro.com>在留言中写道

新闻:%2 **************** @ TK2MSFTNGP10.phx.gbl ...
Matt,

It is not unambiguous, and it is actually disallowed. You can not have
a class in a namespace with the same name as the namespace explicitly
because of this. What if you had a nested type in class C? The compiler
would have to guess without knowing what C is, and I wouldn''t put my trust
in any compiler that did that.

Hope this helps.

--
- Nicholas Paldino [.NET/C# MVP]
- ni**************@exisconsulting.com

"Matt Mastracci" <ma**@aclaro.com> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
似乎就像C#编译无法在明确无误的情况下解决
命名空间和类之间的区别。注意
它看到名称空间C。在课程C之前。我相信它应该看到班级C。首先。

test1.cs:
=============

使用系统;
使用AC;

名称空间AB
公共类D
{(D)(C c = new C(); }
}

test2.cs:
=============

使用系统;

命名空间AC
{
公共课C
{X /
}
}

=============

C:\> csc / target:library test1.cs test2.cs
Microsoft(R)Visual C#.NET编译器版本7.00.9466
Microsoft(R).NET Framework版本1.0.3705
版权所有(C)Microsoft Corporation 2001.保留所有权利。

test1。 cs(8,9):错误CS0118:''A.C''表示''名称空间'',其中''类''
是预期的
It seems like the C# compile can''t resolve the difference between a
namespace and a class in a situation that is clearly unambiguous. Note
that it sees the namespace "C" before the class "C". I believe it
should see the class "C" first.

test1.cs:
=============

using System;
using A.C;

namespace A.B
{
public class D
{
D() { C c = new C(); }
}
}

test2.cs:
=============

using System;

namespace A.C
{
public class C
{
int X;
}
}

=============

C:\>csc /target:library test1.cs test2.cs
Microsoft (R) Visual C# .NET Compiler version 7.00.9466
for Microsoft (R) .NET Framework version 1.0.3705
Copyright (C) Microsoft Corporation 2001. All rights reserved.

test1.cs(8,9): error CS0118: ''A.C'' denotes a ''namespace'' where a ''class'' was expected



没有......试一试。 :)


BTW,经过一番思考后,我认为如果你同时拥有一个

命名空间C,那就不明确了。和C类关闭命名空间A.B。如果我在命名空间C中有一个带有内部类C的类b,那么编译器可以抛出

模糊引用错误,我有:


ABCC< - 外层

ABCCC< - 内层


如果我有C(1)类内部C类名称空间AB和一个C类

(2)在命名空间ABC中,它变得含糊不清:


ABCC< - C(1)

ABCC< - C(2)


马特。


Christian写道:
Nope... Give it a shot. :)

BTW, after some thought, I think it''s only ambiguous if you have both a
namespace "C" and a class "C" off the namespace "A.B". If I have a
class C with an inner class C in namespace C, the compiler can throw the
ambiguous reference error and I have:

A.B.C.C <- outer class
A.B.C.C.C <- inner class

If I have class C (1) with inner class C in namespace A.B and a class C
(2) in namespace A.B.C, it gets ambiguous:

A.B.C.C <- C (1)
A.B.C.C <- C (2)

Matt.

Christian wrote:
当然。
不是吗?
Matt Mastracci <毫安** @ aclaro.com> ha scritto nel messaggio
新闻:3F ************** @ aclaro.com ...
Of course.
Doesn''it?
"Matt Mastracci" <ma**@aclaro.com> ha scritto nel messaggio
news:3F**************@aclaro.com...
C#编译器应该是标记该类与名称相同的名称作为错误命名空间?

Nicholas Paldino [.NET / C#MVP]写道:

Should the C# compiler then be marking the class with the same name as
the namespace as an error?

Nicholas Paldino [.NET/C# MVP] wrote:

马特,

这不是明确的,实际上是不允许的。由于这个原因,你不能在命名空间中明确地使用与命名空间同名的类。如果你在C类中有一个嵌套类型怎么办?
编译器将不得不在不知道C是什么的情况下进行猜测,并且我不会在任何编译器中使用我的
信任

Matt,

It is not unambiguous, and it is actually disallowed. You can not
have
a class in a namespace with the same name as the namespace explicitly
because of this. What if you had a nested type in class C? The
compiler
would have to guess without knowing what C is, and I wouldn''t put my
trust
in any compiler that did that.

Hope this helps.







这篇关于[BUG]编译无法解析命名空间类差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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