You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Source Generator新增成员后IntelliSense不更新,需重启VS的问题

Source Generator更新后IntelliSense不识别新成员的问题

我正在学习Source Generator,已经实现了一个简单的生成器,代码如下:

[Generator]
public class KeysSourceGenerator : ISourceGenerator
{
    public void Execute(GeneratorExecutionContext context)
    {
        var code = """
        namespace MyLibrary;

        public partial class Keys
        {
            public static string Product1 = "1";
            public static string Product2 = "2";
            public static string Product3 = "3";
        }
        """;

        context.AddSource("GeneratedKeys.g.cs", code);
    }

    public void Initialize(GeneratorInitializationContext context)
    {

    }
}

在引用项目中可以正常使用生成的键:

internal class Program
{
    static void Main(string[] args)
    {
        string product1Key = MyLibrary.Keys.Product1;
        string product2Key = MyLibrary.Keys.Product2;
        string product3Key = MyLibrary.Keys.Product3;
    }
}

但遇到一个问题:当我在KeysSourceGenerator.Execute方法中新增键(比如Product4),引用项目能正常构建运行,但IntelliSense只显示前3个键,每次新增都得重启Visual Studio才能识别。请问操作是否有误?

以下是两个项目的proj文件:

KeysGenerator(生成器项目):

<Project Sdk="Microsoft.NET.Sdk">

    <PropertyGroup>
        <TargetFramework>netstandard2.0</TargetFramework>
        <EnforceExtendedAnalyzerRules>true</EnforceExtendedAnalyzerRules>
        <LangVersion>latest</LangVersion>
    </PropertyGroup>

    <ItemGroup>
        <PackageReference Include="Microsoft.CodeAnalysis.Analyzers" Version="3.3.4">
            <PrivateAssets>all</PrivateAssets>
            <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
        </PackageReference>
        <PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.7.0" />
    </ItemGroup>

</Project>

引用生成器的项目:

<Project Sdk="Microsoft.NET.Sdk">

    <PropertyGroup>
        <TargetFramework>net8.0</TargetFramework>
        <ImplicitUsings>enable</ImplicitUsings>
        <Nullable>enable</Nullable>
    </PropertyGroup>

    <ItemGroup>
        <ProjectReference Include="..\KeysGenerator\KeysGenerator.csproj" OutputItemType="Analyzer"/>
    </ItemGroup>

</Project>

问题原因及解决方法

这不是操作错误,属于Visual Studio IntelliSense的缓存机制问题,以下几种方法可以解决,无需重启VS:

  1. 清理并重新生成解决方案

    • 菜单栏选择 生成 -> 清理解决方案
    • 再选择 生成 -> 重新生成解决方案
      强制刷新生成器输出,让IntelliSense重新加载生成的代码。
  2. 刷新单个项目的IntelliSense

    • 按 Ctrl+Shift+B 重新生成当前引用项目
    • 或右键项目选择 卸载项目,再 重新加载项目,重置该项目的IntelliSense缓存。
  3. 优化生成器项目配置
    在生成器项目的<PropertyGroup>中添加以下配置,确保生成器依赖项被正确复制,避免IntelliSense无法找到更新后的程序集:

    <PropertyGroup>
        <TargetFramework>netstandard2.0</TargetFramework>
        <EnforceExtendedAnalyzerRules>true</EnforceExtendedAnalyzerRules>
        <LangVersion>latest</LangVersion>
        <CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies>
    </PropertyGroup>
    
  4. 重置IntelliSense设置

    • 打开 工具 -> 选项 -> 文本编辑器 -> C# -> 高级
    • 找到重置区域,点击重置所有设置(或仅重置IntelliSense相关选项)

额外提示

  • 开发Source Generator时,建议关闭Visual Studio的"快速编辑"模式,减少缓存干扰。
  • 可以借助Roslyn调试工具跟踪生成器执行流程,确认新代码是否正常输出。

内容的提问来源于stack exchange,提问作者jessejbweld

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.18 15:57:08