0

動的に生成されたアセンブリに拡張メソッドの静的クラスを含めようとしていますが、6 行目、28 列目で「this」という単語にある「Type expected」というコンパイラ エラーが引き続き発生します。「this」を削除しても、エラーは返されません (ただし、拡張メソッドではありません)。

 public static void CodeDomDooDad()
    {
        using (var provider = new CSharpCodeProvider())
        {
            var compilerParameters = new CompilerParameters();

            compilerParameters.ReferencedAssemblies.Add("system.dll");
            compilerParameters.CompilerOptions = "/t:library";
            compilerParameters.GenerateInMemory = true;

            var sb = new StringBuilder();

            sb.Append("namespace MooCow \n{ \n");
            sb.Append("public static class Extensions {\n");
            sb.Append("public static string ToMoo(this string s) {\n");
            sb.Append("return s.Replace(\" \",\"moo\");\n");
            sb.Append("}\n");
            sb.Append("}\n");
            sb.Append("}\n");

            //Console.WriteLine(sb.ToString());

            var cr = provider.CompileAssemblyFromSource(compilerParameters, sb.ToString());
            if (cr.Errors.Count > 0)
            {
                CompilerError error = cr.Errors[0];
                Console.WriteLine(
                    "error:"+error.ErrorText + 
                    " line:" +error.Line + 
                    " col:" +error.Column + 
                    " isWarning:" + error.IsWarning);
            }
        }
    }

これは正常に動作する生成コードです。

namespace MooCow {
public static class Extensions
{
    public static string ToMoo(this string s)
    {
        return s.Replace(" ", "moo");
    }
}

}

4

1 に答える 1

0

CompilerVersion を CSharpProvider コンストラクターに追加する必要があることがわかったと思います... var provider = new CSharpCodeProvider( new Dictionary() { { "CompilerVersion", "v3.5" }})

于 2010-11-20T15:28:43.437 に答える