寻找错误的命名空间?

时间:2014-10-21 16:06:17

标签: c# .net

我遇到一些名称空间问题让我感到困惑。为什么会发生这种情况。

在下面的代码中,System.IO& System.Reflection试图引用abc.System,而不是使用我在顶部声明的System命名空间。那是为什么?

using System;
using System.Collections.Generic;
using System.Data.OleDb;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace abc.Data
{
public sealed class Access
{
    public static void Open(string dbPath)
    {
         // error here referencing abc.System in System.IO, and System.Reflection.

         string appPath = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);         }
}

然后我在另一个文件中有另一个命名空间,如下所示:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace abc.System
{
     public static class DateTimeExtensions
     {
    // Implemented from
    // http://stackoverflow.com/questions/38039/how-can-i-get-the-datetime-for-the-start-of-the-week
    public static DateTime StartOfWeek(this DateTime dt, DayOfWeek startOfWeek)
    {
        int diff = dt.DayOfWeek - startOfWeek;
        if (diff < 0)
        {
            diff += 7;
        }

        return dt.AddDays(-1 * diff).Date;
    }

    public static DateTime EndOfWeek(this DateTime dt, DayOfWeek startOfWeek)
    {
        int diff = dt.DayOfWeek - startOfWeek;
        if (diff < 0)
        {
            diff += 7;
        }

        return dt.AddDays(diff).Date;
    }
}
}

1 个答案:

答案 0 :(得分:6)

由于存在命名空间冲突,因此您需要使用global关键字来明确您尝试访问的内容。

string appPath = global::System.IO.Path.GetDirectoryName(global::System.Reflection.Assembly.GetExecutingAssembly().Location); 

或者,如果可以的话,更改您的名称空间,因为这会非常快地变得烦人!