dotnet/wpf · error · CspProjectException

Error: Project does not contain type

Error message

Error: Project does not contain type '{0}'

What it means

Project.ExecuteMain in the csp tool looks up the project's main class in the compiled assembly via _assembly.GetType(sMainClass) and throws CspProjectException when the type cannot be found. This means the project file declares a main class that is not present in the built assembly, so the tool cannot invoke its entry point.

Solutions

  1. Check the fully-qualified type name in the project file against the actual class (case-sensitive, includes namespace).
  2. Rebuild the assembly so it contains the declared main class.
  3. Verify csp is loading the correct assembly file for this project.
  4. Fix the project's main-class setting after any rename or namespace change.

Example fix

// before (project file)
<mainClass>MyTools.OldRunner</mainClass>
// after
<mainClass>MyTools.Runner</mainClass>
Defensive patterns

Strategy: validation

Validate before calling

var asm = Assembly.LoadFrom(assemblyPath);
var mainType = asm.GetType(sMainClass, throwOnError: false);
if (mainType == null)
    throw new Exception($"Type '{sMainClass}' not found in {assemblyPath}");

Type guard

static bool AssemblyContainsType(Assembly asm, string fullName) => asm != null && asm.GetType(fullName, throwOnError: false) != null;

Try / catch

try { project.ExecuteMain(); }
catch (CspProjectException ex) when (ex.Message.Contains("does not contain type")) { /* fix mainClass name in project file and rebuild */ }

Prevention

When it happens

Trigger: Running csp on a project whose declared main class name (namespace-qualified) does not match any type in the assembly: typo in the project config, class renamed/deleted, or the assembly is stale/missing the class.

Common situations: Renaming a namespace or class without updating the project file; running against an out-of-date build output; wrong casing of the type name; pointing csp at the wrong DLL.

Related errors


AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14). Data as JSON: /api/errors/2eb5540959aa2c33. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/WpfGfx/tools/csp/Project.cs:194

        /// <summary>
        /// Execute the Main function. (A static member of a certain hardcoded class).
        /// </summary>
        internal int ExecuteMain(
            string sMainClass,
            string[] parameters
            )
        {
            if (sMainClass == null)
            {
                sMainClass = FindMainClass();
            }

            Object returnValue = null;

            Type tMainClass = _assembly.GetType(sMainClass);
            if (tMainClass == null)
            {
                throw new CspProjectException(
                    "Error: Project does not contain type '" + sMainClass +"'");
            }

            if (!IsMainPresent(tMainClass))
            {
                throw new CspProjectException(
                    "Error: Type '" + sMainClass +"' has no Main method");
            }
            
            try
            {
                if (_breakBeforeInvoke)
                {
                    // This breakpoint is just before we execute the project we've compiled.
                    // If you want to debug the project, step into the following "InvokeMember"
                    // call.

                    System.Diagnostics.Debugger.Break();

View on GitHub (pinned to 81131a70a4)