동적으로 생성 된 어셈블리의 확장 방법은 무엇입니까?
-
12-09-2020 - |
문제
라인 6에서 'Type 28,'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");
}
}
.
}
해결책
CsharpProvider 생성자에게 Compilerversion을 추가해야한다고 생각합니다 ... var provider= new csharpcodeprovider (새 사전 () {{ "compilerVersion", "v3.5"}}))
제휴하지 않습니다 StackOverflow