动态生成装配中的扩展方法?

我正在尝试在动态生成的程序集中包含一个扩展方法静态类,除了我在第6行第28列的“预期类型”中遇到编译器错误,该错误恰好出现在“this”一词上。如果我删除'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");
    }
}
}     
已邀请:
我想我发现了,不得不将CompilerVersion添加到CSharpProvider构造函数中... var provider = new CSharpCodeProvider(new Dictionary(){{“CompilerVersion”,“v3.5”}})     

要回复问题请先登录注册