【问题标题】:Why is my application say that im missing references even though I have them?为什么我的应用程序说我缺少参考资料,即使我有参考资料?
【发布时间】:2016-09-28 03:56:41
【问题描述】:

我目前正在学习如何将 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");
        }
    }
}

以及它的价值 here is a image of the small application

【问题讨论】:

  • 您能告诉我们您要编译的代码吗?
  • 对不起!我现在就加!
  • 您是否使用 Visual Studio 来构建它?
  • 是的,好吧,要构建应用程序,然后我想构建源代码,但应用程序给了我错误,添加了一张图片。
  • 您需要添加对编译器的引用。不是你的项目。 CSharpCodeProvider 不会自动链接到您的项目链接到的任何程序集。

标签: c# .net wpf winforms compilation


【解决方案1】:

您应该使用CompilerParameters.ReferencedAssemblies.Add() 填写您的推荐信

【讨论】:

  • 我为 parameters.ReferencedAssemblies.Add("Microsoft.CSharp");' could not be found 尝试过,但现在它把这个 Metadata file 'Microsoft.CSharp' could not be found 扔给我
  • 尽管有documentation,添加程序集元数据文件的完整或部分路径,而不是程序集的名称。大多数程序集由一个文件组成,因此该文件将包含程序集元数据。在任何情况下,.exe 或 .dll 文件(如果此类文件是程序集的一部分)几乎总是包含元数据的文件。所以,添加parameters.ReferencedAssemblies.Add("Microsoft.CSharp");
  • 糟糕。我的意思是最后加上“.dll”:parameters.ReferencedAssemblies.Add("Microsoft.CSharp.dll");
猜你喜欢
  • 2017-08-27
  • 1970-01-01
  • 1970-01-01
  • 2013-06-28
  • 2011-02-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多