正在查找错误的命名空间

本文关键字:命名空间 错误 查找 | 更新日期: 2023-09-27 18:27:17

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

在下面的代码中,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;
    }
}
}

正在查找错误的命名空间

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

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

或者,如果可以的话,更改你的名称空间,因为这会很快变得令人讨厌!