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

.NET7 WPF应用中Roslyn编译DLL引发InitializeComponent错误的问询

问题:Roslyn编译生成的DLL导致WPF窗口InitializeComponent报错

同一个.cs文件通过Visual Studio编译为DLL时,可在WPF程序中正常运行;但通过Roslyn编译为DLL后,会导致所有窗口的InitializeComponent方法报错:“名称‘InitializeComponent’在当前上下文中不存在”。运行环境为.NET7,核心代码如下:

void CompileCode(string codeString)
{
    SyntaxTree syntaxTree = CSharpSyntaxTree.ParseText(codeString);
    var references = new[]
    {
        typeof(object).Assembly,
        Assembly.Load("SqlSugar"),
        Assembly.Load("System.Linq"),
        Assembly.Load("netstandard"),
        Assembly.Load("System.Runtime"),
        Assembly.Load("System"),
    }
    .Select(assembly => assembly.Location)
        .Distinct()
        .Select(l => MetadataReference.CreateFromFile(l))
        .Cast<MetadataReference>()
        .ToArray();
    var DefaultNamespaces =
            new[]
            {
                "System",
                "System.IO",
                "System.Net",
                "System.Linq",
                "System.Text",
                "System.Text.RegularExpressions",
                "System.Collections.Generic"
            };
    var compilationOptions = new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary)
            .WithPlatform(Platform.AnyCpu) 
            .WithOverflowChecks(true)
            .WithOptimizationLevel(OptimizationLevel.Release) 
            .WithUsings(DefaultNamespaces);
    ;
    var runtimePath = System.Runtime.InteropServices.RuntimeEnvironment.GetRuntimeDirectory(); 
    CSharpCompilation cSharpCompilation = CSharpCompilation.Create(tableName)
                    .WithOptions(compilationOptions) 
                    .AddReferences(references) 
                    .AddSyntaxTrees(syntaxTree);
    var assemblyPath = @"D:\myCode\DataTableCS\" + tableName + ".dll";
    var pdbPath = @"D:\myCode\DataTableCS\" + tableName + ".pdb";
    var dll_result = cSharpCompilation.Emit(assemblyPath, pdbPath);     
}

对应的.cs文件内容如下:

using System;
using System.Linq;
using System.Text;
using SqlSugar;

namespace CommonClass
{
    ///<summary>
    ///
    ///</summary>
    [SugarTable("Tb_Project")]
    public partial class Tb_Project
    {
           public Tb_Project(){}

           /// <summary>
           /// Desc:
           /// Default:
           /// Nullable:False
           /// </summary>           
           [SugarColumn(IsPrimaryKey=true)]
           public string ID {get;set;}

           /// <summary>
           /// Desc:
           /// Default:
           /// Nullable:True
           /// </summary>           
           public string? Name {get;set;}

           /// <summary>
           /// Desc:
           /// Default:
           /// Nullable:True
           /// </summary>           
           public decimal? Quantity {get;set;}

           /// <summary>
           /// Desc:
           /// Default:
           /// Nullable:True
           /// </summary>           
           public string? RootItemID {get;set;}

           /// <summary>
           /// Desc:
           /// Default:
           /// Nullable:True
           /// </summary>           
           public string? RootItemConfig {get;set;}

           /// <summary>
           /// Desc:
           /// Default:
           /// Nullable:True
           /// </summary>           
           public string? ProjectNo {get;set;}

           /// <summary>
           /// Desc:
           /// Default:
           /// Nullable:True
           /// </summary>           
           public string? ProjectManagerID {get;set;}

           /// <summary>
           /// Desc:
           /// Default:
           /// Nullable:True
           /// </summary>           
           public string? MDPTime {get;set;}

           /// <summary>
           /// Desc:
           /// Default:
           /// Nullable:True
           /// </summary>           
           public string? Status {get;set;}

           /// <summary>
           /// Desc:
           /// Default:
           /// Nullable:True
           /// </summary>           
           public string? Description {get;set;}

           /// <summary>
           /// Desc:
           /// Default:
           /// Nullable:True
           /// </summary>           
           public int? Version {get;set;}

           /// <summary>
           /// Desc:
           /// Default:
           /// Nullable:True
           /// </summary>           
           public string? CTime {get;set;}

           /// <summary>
           /// Desc:
           /// Default:
           /// Nullable:True
           /// </summary>           
           public int? del {get;set;}
    }
}

复现步骤:

  • 创建一个WPF C#示例应用
  • 使用CompileCode()方法将上述.cs文件编译为DLL
  • 将步骤2生成的DLL添加到应用中
  • 运行窗口触发上述错误

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.29 20:33:25