実行時にこのコードをコンパイルしようとしています。このコードは、コード ファーストの EF4 クラスです。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel.DataAnnotations;
namespace EFCodeFirst.Model.Models
{
[Table("Blog")]
public class Blog
{
public Guid Id { get; set; }
[Column("txtTitle")]
public string Title { get; set; }
[DatabaseGenerated(DatabaseGeneratedOption.None)]
public string ShortTitle { get { return Title; } }
public string BloggerName { get; set; }
public virtual ICollection<Post> Posts { get; set; }
}
public class Post
{
public Guid Id { get; set; }
public string Title { get; set; }
public DateTime DateCreated { get; set; }
public string Content { get; set; }
public Guid BlogId { get; set; }
}
}
このメソッドを使用して、指定されたコードをコンパイルします。このコードを単純なクラスでテストしました。できます。しかし、特定のクラスでは、まったく機能しません。
private Assembly BuildAssembly(string code)
{
Microsoft.CSharp.CSharpCodeProvider provider = new CSharpCodeProvider();
ICodeCompiler compiler = provider.CreateCompiler();
CompilerParameters compilerparams = new CompilerParameters();
compilerparams.GenerateExecutable = false;
compilerparams.GenerateInMemory = true;
CompilerResults results = compiler.CompileAssemblyFromSource(compilerparams, code);
if (results.Errors.HasErrors)
{
StringBuilder errors = new StringBuilder("Compiler Errors :\r\n");
foreach (CompilerError error in results.Errors)
{
errors.AppendFormat("Line {0},{1}\t: {2}\n", error.Line, error.Column, error.ErrorText);
}
throw new Exception(errors.ToString());
}
else
{
return results.CompiledAssembly;
}
}
そして、私は次のようないくつかの例外を取得しています:
error CS0234: The type or namespace name 'Linq' does not exist in the namespace 'System' (are you missing an assembly reference?)}
何か助けはありますか?
ありがとうございました。