2016-09-28 4 views
2

私は現在、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"); 
     } 
    } 
} 

とし:ここで

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?) 

コードイムを投げエラーです:ここでは

は、ソースコードであります何の価値があるのか​​ here is a image of the small application

+0

コンパイルしようとしているコードを教えてください。 – Rob

+0

ああ申し訳ありません!今すぐ追加してください! –

+0

Visual Studioを使用してこれを構築していますか? –

答えて

2

CompilerParameters.ReferencedAssemblies.Add()

+0

私は 'parameters.ReferencedAssemblies.Add(" Microsoft.CSharp ");見つけられませんでしたが、今私はこれを投げかけています 'メタデータファイル 'Microsoft.CSharp'が見つかりませんでした。 –

+0

[documentation](https://msdn.microsoft.com/en-us/library/system)にもかかわらず。 codedom.compiler.compilerparameters.referencedassemblies(v = vs.110).aspx)、アセンブリの_name_ではなく、アセンブリのメタデータファイルの完全パスまたは部分パスを追加します。ほとんどのアセンブリは1つのファイルで構成されているため、アセンブリメタデータのファイルになります。いずれの場合でも、.exeまたは.dllファイルは、そのようなファイルがアセンブリの一部である場合、ほとんどの場合、メタデータを含むファイルです。したがって、 'parameters.ReferencedAssemblies.Add(" Microsoft.CSharp ");' –

+0

を追加してください。私は最後に ".dll"をつけるつもりだった: 'parameters.ReferencedAssemblies.Add(" Microsoft.CSharp.dll ");' –

関連する問題