Creating a Custom BuildProvider : BuildProvider « Development « ASP.NET Tutorial






ASP.NET page gets compiled dynamically into a .NET class in the background by a BuildProvider.

Whenever you create a file that has the extension .simple, the SimpleBuilderProvider builds a new class with the same name as the file in the background. 

File: App_Code\SimpleBuildProvider.cs

using System;
using System.Web.Compilation;
using System.CodeDom;
using System.IO;

namespace MyNamespace
{
    public class SimpleBuildProvider : BuildProvider
    {
        public override void GenerateCode(AssemblyBuilder ab)
        {
            string fileName = Path.GetFileNameWithoutExtension(this.VirtualPath);
            string snippet = "public class " + fileName + @"{
                    public static void DoSomething(){}
                }";
            ab.AddCodeCompileUnit(this, new CodeSnippetCompileUnit(snippet));
        }

    }
}








9.5.BuildProvider
9.5.1.Creating a Custom BuildProvider
9.5.2.BuildProvider must be compiled into a different assembly than the other code in the App_Code folder