1

I've created a very simple custom compiler:

public class SimpleCompiler : CSharpCodeProvider
{
    public SimpleCompiler() : base()
    {
        File.AppendText("d:\foo.txt","bar");
    }
}

In my web.config file, I copied/pasted existing configuration to declare this new compiler.

<compiler language="c#;cs;csharp" extension=".cs" type="XXX.SimpleCompiler, XXX" warningLevel="4">
    <providerOption name="CompilerVersion" value="v3.5"/>
    <providerOption name="WarnAsError" value="false"/>
</compiler>

But I get errors when accessing user controls with code like:

<asp:Label runat="server" ID="Label3">
    <%# 
         string.Join("<br/>",((ReadOnlyCollection<AAA>)Eval("ListOfAs")).Select(x => x.Name).ToArray())                                 
    %>
 </asp:Label>

It fails on > of <AAA>. Something should be missing somewhere (using?), but it builds fine with standard compiler Microsoft.CSharp.CSharpCodeProvider.

Error message is:

Compiler Error Message: CS1525: Invalid expression term '>'

Update: It seems to be related to LINQ. It fails on all pages with LINQ in control code. However System.Core.dll is included in Web.config.

Update2: I was able to reproduce it in a new Web Application project.

  1. Add a new class:

    namespace WebApplication1
    {
        public class SimpleCompiler : CSharpCodeProvider
        {
            private void Log(string message)
            {
                File.AppendAllText("d:\\foo.txt", DateTime.Now.Ticks+Environment.NewLine);
            } 
       }
    

    }

  2. Update web.config

    <system.codedom>
        <compilers>
          <compiler language="c#;cs;csharp" extension=".cs" warningLevel="4" type="WebApplication1.SimpleCompiler, WebApplication1">
              <providerOption name="CompilerVersion" value="v3.5"/>
              <providerOption name="WarnAsError" value="false"/>
            </compiler>
        </compilers>
    </system.codedom>
    
  3. change Default.aspx to:

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication1._Default" %>
    <%@ Import Namespace="System.Linq" %>
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <html xmlns="http://www.w3.org/1999/xhtml">
        <head runat="server"></head>
        <body>
            <form id="form1" runat="server">
            <div>
                <%
                    int x = (new int[] { 1, 2, 3, 4, 5 }).Where(z => z > 2).Count();
                    this.Response.Write(x);
                %>
            </div>
            </form>
        </body>
    </html>
    

If I update project to 4.0, it works. But I can't do that in other one.

abatishchev
  • 98,240
  • 88
  • 296
  • 433
Francois
  • 10,730
  • 7
  • 47
  • 80

1 Answers1

0

Try add to Web.config:

<pages>
   <namespaces>
      <add namespace="System.Linq" />
   </namespaces>
</pages>

and

<compilation>
    <assemblies>
        <add assembly="System.Core" />
    </assemblies>
</compilation>
abatishchev
  • 98,240
  • 88
  • 296
  • 433