Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/elixir/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
为什么C#编译器会区分这两种情况?_C#_.net_Compiler Construction - Fatal编程技术网

为什么C#编译器会区分这两种情况?

为什么C#编译器会区分这两种情况?,c#,.net,compiler-construction,C#,.net,Compiler Construction,当你有一些类似于: using Algebra; public Algebra.Vector3 Direction { get { return this.direction; } } 然后编译并稍后将其更改为: using Algebra; public Vector3 Direction { get { return this.direction; } } 两个程序集之间的编译代码似乎不同,我可以使用Reflector看到这一点 为什么编译器会区分这两种代码?是否只需

当你有一些类似于:

using Algebra;

public Algebra.Vector3 Direction
{
    get { return this.direction; }
}
然后编译并稍后将其更改为:

using Algebra;

public Vector3 Direction
{
    get { return this.direction; }
}
两个程序集之间的编译代码似乎不同,我可以使用Reflector看到这一点


为什么编译器会区分这两种代码?是否只需要查看编译时是否有任何不明确的类型,如果没有,编译后的代码是否都相同?我假设编译后的代码在任何时候都为每个成员使用完全限定的名称。

我无法重现这一点。示例代码:

namespace Algebra
{
    public class Vector3 {}
}

namespace Test
{
    using Algebra;

    public class Program
    {
        private Vector3 direction = null;

        public Vector3 Direction1
        {
            get { return direction; }
        }

        public Algebra.Vector3 Direction2
        {
            get { return direction; }
        }
    }
}
为这两个属性生成的IL完全相同,并且它们在反射器中看起来相同

我的猜测是,实际上在“全局”名称空间(或您有using指令的其他名称空间)中有另一个名为
Vector3
的类


在Visual Studio中,尝试将鼠标悬停在两个类型名称上,查看它们显示的内容。

下面是一个示例,将生成您正在观察的结果。希望这能澄清你的问题。假设你有

  • 在命名空间
    代数
    中包含类型
    矢量3
    的单独库

  • 项目中的以下文件:

文件① 文件②
您能添加创建的代码吗?正如您在reflector中看到的那样?您发现编译代码中的实际差异是什么?唯一可能的情况是
Vector3
解析为与
代数不同的类型。Vector3
解析为不同的类型。当然,它实际上与我之前和之后发布的代码一样显示。我试图重现您的示例。但是reflector和il反汇编程序都没有显示出差异……如果使用ILDASM显示属性的元数据,会有任何差异吗?我不明白为什么在两个代码示例之间生成的元数据会有任何差异。文件1是否也应该是NotAlgebra名称空间?谢谢Jon,我在上面添加了一条关于它的评论。
namespace NotAlgebra
{
    public class Vector3
    {
        // ...
    }
}
using Algebra;

namespace NotAlgebra
{
    public class XYZ
    {
        // Refers to NotAlgebra.Vector3 (defined in File 1 above)
        Vector3 MyProperty1 { get; }

        // Refers to Algebra.Vector3 (defined in the external library)
        Algebra.Vector3 MyProperty2 { get; }
    }
}