You could implement this with a custom MSBuild task that validates all pages. For example, here is a Task
that will check that only BasePage
inherits from Page
. If any other class inherits from Page
, an error will be logged in the output and the build will fail:
public class CheckBasePage : Task
{
public override bool Execute()
{
var assm = Assembly.LoadFile("/path/to/WebsiteAssembly.dll");
var types = assm.GetTypes();
var pages = new List<string>();
foreach (var t in types)
{
if (t.BaseType == typeof(Page) && t.Name != "BasePage")
{
pages.Add(t.FullName);
}
}
if (pages.Count > 0)
{
Log.LogError("The following pages need to inherit from BasePage: [" + string.Join(",", pages) + "]");
return false;
}
return true;
}
}
Then you would add this custom task as part of the build process in your project file:
<UsingTask TaskName="CheckBasePage" AssemblyFile="/path/to/MyCustomTasksAssembly.dll" />
<Target Name="PostBuild">
<CheckBasePage />
</Target>
Now this assumes that MyCustomTasksAssembly
is a separate project you create for managing custom MSBuild tasks like this. If you want to embed the CheckBasePage
task into the same project as the rest of the code, you will need to apply the same trick from here.
BasePage
. – Veradis