Создание assembly из текста с референс на проект?
От: Аноним  
Дата: 14.03.10 17:28
Оценка:
Создаю assembly кодом:


public static bool MakeAssembly(string sourceFile, string targetAssembly, string[] references)
    {
      // delete existing assembly first 
      if (File.Exists(targetAssembly))
      {
        try
        {
          // this might fail if assembly is in use 
          File.Delete(targetAssembly);
        }
        catch (Exception ex)
        {
          //this.ErrorMessage = ex.Message;
          return false;
        }
      }

      // read the C# source file
      StreamReader sr = new StreamReader(sourceFile);
      string fileContent = sr.ReadToEnd();
      sr.Close();

      // set up compiler and add required references
      CodeDomProvider compiler = new CSharpCodeProvider();
      CompilerParameters parameter = new CompilerParameters();
      parameter.OutputAssembly = targetAssembly;
      parameter.IncludeDebugInformation = false;
      parameter.CompilerOptions = "/target:library /optimize";
      parameter.ReferencedAssemblies.Add("System.dll");

      foreach (string reference in references)
      {
        parameter.ReferencedAssemblies.Add(reference);
      }

      // Do it: Final compilation to disk
      CompilerResults results = compiler.CompileAssemblyFromFile(parameter, sourceFile);

      if (File.Exists(targetAssembly))
        return true;

      // flatten the compiler error messages into a single string
      foreach (CompilerError err in results.Errors)
      {
        //this.ErrorMessage += err.ToString() + "\r\n";
      }

      return false;
    }


Utils.MakeAssembly("3.cs", "WpfApplication1.A.dll", new []{"System.Windows.Forms.dll", "WpfApplication1.exe"});

Текст класса в 3.cs

using System;
using System.Windows.Forms;

namespace WpfApplication1
{
  public class A
  {
    public A()
    {
    }

    public void SayHello(){
      //Form1 f = new Form1();
      //f.Show();
      MessageBox.Show("Hello from loaded assembly");
    }
  }
}



При попытке расскомментировать
      //Form1 f = new Form1();
      //f.Show();


выкидывает ошибку и не создает assembly говорит что нет референса кототый бы содержал класс Form1
но я ж указал при создании assembly референс на само приложение WpfApplication1.exe а в приложении есть класс Form1

Что я не так делаю? Не пинайте — я еще чайник )))
 
Подождите ...
Wait...
Пока на собственное сообщение не было ответов, его можно удалить.