gpt4 book ai didi

c# - 为什么 intellisense 与 Using 指令一起工作,但在命名空间中指定类型时却不起作用?

转载 作者:太空宇宙 更新时间:2023-11-03 20:39:41 25 4
gpt4 key购买 nike

我可以使用智能感知将语句 using System.Configuration; 添加到我的代码中,但是当我访问类型 ConfigurationManager 时,我无法获得任何智能感知。

为什么智能感知在输入 using 指令时起作用,但在指定来自该命名空间的类型时不起作用?

using System.Configuration;

namespace TestDBMSConnection
{
class Program
{
static void Main(string[] args)
{
var dataProviderName = ConfigurationManager.AppSettings["provider"];
}
}
}

我可以通过添加对 System.Configuration 的引用来解决这个问题,但我不明白为什么我不需要这样做就可以让智能感知为 using 指令工作。

最佳答案

命名空间中的类不需要位于同一个程序集中。

System.Configuration 命名空间中的某些类位于 System.dll(例如 SettingsBase)中,而其他一些类位于 System.Configuration.dll(例如 ConfigurationManager)中。

IntelliSense 只能在引用的程序集中建议类和命名空间。因此,如果您引用了 System.dll 而不是 System.Configuration.dll,IntelliSense 可以建议位于 System.dll 中的 System.Configuration 命名空间和 System.Configuration 类,而不是位于 System.Configuration.dll 中的那些。

IntelliSense 也不知道某个类可能位于哪个未引用的程序集中。所以你必须手动引用 System.Configuration.dll 才能使用它。

关于c# - 为什么 intellisense 与 Using 指令一起工作,但在命名空间中指定类型时却不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3601368/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com