gpt4 book ai didi

c# - 为什么我的应用程序说我缺少引用资料,即使我有引用资料?

转载 作者:行者123 更新时间:2023-11-30 23:20:29 27 4
gpt4 key购买 nike

我目前正在学习如何将 C# 代码编译成他们自己的 exe 文件。它的工作原理真的很有趣!

到目前为止,我已经有了这个非常小的应用程序,它应该将放入文本框的文本转换为 C# 代码,它有点这样做,但它一直告诉我每次按“构建”时添加引用,但我不知道为什么。

这是源代码:

using Microsoft.CSharp;
using System.CodeDom.Compiler;
using System.Collections.Generic;
using System.Linq;
using System.Windows;

namespace SimpleBuilder
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// store code
/// select what features
/// print out textfile with all the code from the features
/// compile that textfileContent to a exe
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}

private void fakeMessageOne()
{
Messagebox.Show("Hello World");
}

private void button_Click(object sender, RoutedEventArgs e)
{

CSharpCodeProvider csc = new CSharpCodeProvider(new Dictionary<string, string>() { { "CompilerVersion", frameworkTextbox.Text } });
CompilerParameters parameters = new CompilerParameters(new[] { "mscorlib.dll", "System.Core.dll" }, outputTextbox.Text, true);
parameters.GenerateExecutable = true;

CompilerResults result = csc.CompileAssemblyFromSource(parameters, sourceTextbox.Text);
if (result.Errors.HasErrors)
{
result.Errors.Cast<CompilerError>().ToList().ForEach(error => errorTextbox.Text += error.ErrorText + "\r\n");
}
else
{
errorTextbox.Text = "--- Build Succeeded! ---";
}
}
}
}

这是它抛给我的错误

The type or namespace name 'CSharp' does not exist in the namespace 'Microsoft' (are you missing an assembly reference?)
The type or namespace name 'CodeDom' does not exist in the namespace 'System' (are you missing an assembly reference?)
The type or namespace name 'Windows' does not exist in the namespace 'System' (are you missing an assembly reference?)
The type or namespace name 'Window' could not be found (are you missing a using directive or an assembly reference?)

我试图在我的应用程序内部编译的代码:

using Microsoft.CSharp;
using System.CodeDom.Compiler;
using System.Collections.Generic;
using System.Linq;
using System.Windows;


namespace SimpleBuilder
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// store code
/// select what features
/// print out textfile with all the code from the features
/// compile that textfileContent to a exe
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}

private void fakeMessageOne()
{
Messagebox.Show("Hello World");
}
}
}

以及它的值(value) here is a image of the small application

最佳答案

您应该将您的引用放入 CompilerParameters.ReferencedAssemblies.Add()

关于c# - 为什么我的应用程序说我缺少引用资料,即使我有引用资料?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39737784/

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