PeterPan
PeterPan

Reputation: 3

Compiler error when compiling by CSharpCodeProvider

I'm kinda new here and this is my first question. So please don't rage at me when I'm doin something wrong (:

My problem: I'm trying to compile a C# source code in runtime, using CSharpCodeProvider.

It compiles without any problems unless i change the CompilerVersion to "v2.0" using this:

Dictionary<string, string> provOptions = new Dictionary<string, string>();
provOptions.Add("CompilerVersion", "v2.0");

CSharpCodeProvider provider = new CSharpCodeProvider(provOptions);

It shows errors in the 15th line which is this:

15: System.AppDomain.CurrentDomain.AssemblyResolve += (sender, args2) =>
16: {
17:     byte[] assemblydata = Convert.FromBase64String(data);
18:     return Assembly.Load(assemblydata);
19: };

For example:

Line 15, directly after "sender": ) expected

Line 15, directly after "sender": , invalid expression

And some more. I have no idea where the problem comes from. When I use the Visual Studio it compiles fine with "v2.0"

Upvotes: 0

Views: 360

Answers (1)

Salvatore Previti
Salvatore Previti

Reputation: 9050

I would not say something stupid but lambda expression and linq were introduced in C# 3.0, not in C# 2.0.

Instead of using += (sender, args2) => why you don't just use an anonymous delegate? C# 2.0 supports that.

System.AppDomain.CurrentDomain.AssemblyResolve += delegate (object sender, ResolveEventArgs args)
{
    byte[] assemblydata = Convert.FromBase64String(data);
    return Assembly.Load(assemblydata);
};

Or change the compiler to version 3.0.

If I'm wrong please don't rage at me :)

Upvotes: 4

Related Questions