{"record":{"id":"6df5d372fefe0be7","repo":"dotnet/wpf","slug":"error-no-main-method-found","errorCode":null,"errorMessage":"Error: No Main method found","messagePattern":"Error: No Main method found","errorType":"exception","errorClass":"CspProjectException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/WpfGfx/tools/csp/Project.cs","lineNumber":290,"sourceCode":"\n            foreach (Type t in _assembly.GetTypes())\n            {\n                if (IsMainPresent(t))\n                {\n                    if (sRet != \"\")\n                    {\n                        throw new CspProjectException(\n                            \"Error: Multiple Main methods - in classes '\" + sRet + \"' and '\" + t.FullName + \"'\");\n                    }\n\n                    sRet = t.FullName;\n                }\n\n            }\n\n            if (sRet == \"\")\n            {\n                throw new CspProjectException(\n                    \"Error: No Main method found\");\n            }\n\n            return sRet;\n        }\n\n\n        /// <summary>\n        /// Tests whether the type has a Main method.\n        /// Throws an exception if more than one is present.\n        /// </summary>\n        private bool IsMainPresent(Type t)\n        {\n            MethodInfo method = null;\n            try\n            {\n                method = t.GetMethod(\n                    \"Main\",","sourceCodeStart":272,"sourceCodeEnd":308,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/WpfGfx/tools/csp/Project.cs#L272-L308","documentation":"After scanning every type in the assembly via reflection, FindMainClass (Project.cs:269) found no class with a public static Main method. Since the csp tool's ExecuteMain flow needs an entry point to run, it throws CspProjectException. Note the lookup is restricted to public static Main; other signatures are ignored.","triggerScenarios":"ExecuteMain is called on an assembly that contains no type with a public static Main method, i.e. FindMainClass iterated all types without ever setting sRet.","commonSituations":"Pointing the tool at a class library (DLL) instead of an executable, an entry point declared as private/internal static Main, an async Task Main not matching the expected shape, or top-level-statements/renamed entry methods that never produce a public static Main.","solutions":["Add a public static void Main(string[] args) method to the class that should be the entry point.","Make the existing Main public and static (internal or private Main is not found by this reflection lookup).","Verify you are passing the correct assembly — an executable, not a class library.","Check the Main signature: the reflection call uses only the name 'Main' with Public|Static flags, so overloaded/renamed entry points must be adjusted."],"exampleFix":"// before\ninternal class Program { static void Main(string[] args) { ... } }\n\n// after\npublic class Program { public static void Main(string[] args) { ... } }","handlingStrategy":"validation","validationCode":"bool hasMain = assembly.GetTypes().Any(t =>\n    t.GetMethod(\"Main\", BindingFlags.Public | BindingFlags.Static) != null);\nif (!hasMain)\n    throw new InvalidOperationException(\"Assembly has no public static Main; not an executable.\");","typeGuard":null,"tryCatchPattern":"try { project.ExecuteMain(); }\ncatch (CspProjectException ex) when (ex.Message == \"Error: No Main method found\") {\n    Console.Error.WriteLine(\"Target assembly has no public static Main; pass an executable assembly.\");\n}","preventionTips":["Verify the input is an .exe-style assembly with an entry point, not a class library.","Ensure Main is public static; check the compiled signature if using async Task Main.","Run a quick reflection check for a public static Main before invoking the tool."],"tags":["reflection","entry-point","assembly","missing-method"],"backgroundTag":"missing-required-argument","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-21T21:30:21.729Z"}