public static BuildResult GenerateCodeBehind ( DotNetProject project, string filename, WebFormsParsedDocument document, out CodeCompileUnit ccu) { ccu = null; var result = new BuildResult (); string className = document.Info.InheritedClass; AddErrorsToResult (result, filename, document.GetErrorsAsync().Result); if (result.ErrorCount > 0) return result; if (string.IsNullOrEmpty (className)) return result; var refman = new WebFormsTypeContext { Project = project, Doc = document }; refman.CreateCompilation (default(CancellationToken)).Wait (); var memberList = new WebFormsMemberListBuilder (refman, document.XDocument); memberList.Build (); AddErrorsToResult (result, filename, memberList.Errors); if (result.ErrorCount > 0) return result; //initialise the generated type ccu = new CodeCompileUnit (); var namespac = new CodeNamespace (); ccu.Namespaces.Add (namespac); var typeDecl = new CodeTypeDeclaration { IsClass = true, IsPartial = true, }; namespac.Types.Add (typeDecl); //name the class and namespace int namespaceSplit = className.LastIndexOf ('.'); if (namespaceSplit > -1) { namespac.Name = project.StripImplicitNamespace (className.Substring (0, namespaceSplit)); typeDecl.Name = className.Substring (namespaceSplit + 1); } else { typeDecl.Name = className; } string masterTypeName = null; if (!String.IsNullOrEmpty (document.Info.MasterPageTypeName)) { masterTypeName = document.Info.MasterPageTypeName; } else if (!String.IsNullOrEmpty (document.Info.MasterPageTypeVPath)) { try { var ext = project.GetService<AspNetAppProjectFlavor> (); ProjectFile resolvedMaster = ext.ResolveVirtualPath (document.Info.MasterPageTypeVPath, document.FileName); WebFormsParsedDocument masterParsedDocument = null; if (resolvedMaster != null) masterParsedDocument = TypeSystemService.ParseFile (project, resolvedMaster.FilePath).Result as WebFormsParsedDocument; if (masterParsedDocument != null && !String.IsNullOrEmpty (masterParsedDocument.Info.InheritedClass)) masterTypeName = masterParsedDocument.Info.InheritedClass; } catch (Exception ex) { LoggingService.LogWarning ("Error resolving master page type", ex); } if (string.IsNullOrEmpty (masterTypeName)) { var msg = string.Format ("Could not find type for master '{0}'", document.Info.MasterPageTypeVPath); result.AddError (filename, msg); return result; } } if (masterTypeName != null) { var masterProp = new CodeMemberProperty { Name = "Master", Type = new CodeTypeReference (masterTypeName), HasGet = true, HasSet = false, Attributes = MemberAttributes.Public | MemberAttributes.New | MemberAttributes.Final, }; masterProp.GetStatements.Add (new CodeMethodReturnStatement ( new CodeCastExpression (masterTypeName, new CodePropertyReferenceExpression ( new CodeBaseReferenceExpression (), "Master")))); typeDecl.Members.Add (masterProp); } //shortcut building the existing members type map if (memberList.Members.Count == 0) return result; var cls = refman.GetTypeByMetadataName (className); var members = GetDesignerMembers (memberList.Members.Values, cls, filename); //add fields for each control in the page foreach (var member in members) { var type = new CodeTypeReference (member.Type.ToDisplayString (SymbolDisplayFormat.CSharpErrorMessageFormat)); typeDecl.Members.Add (new CodeMemberField (type, member.Name) { Attributes = MemberAttributes.Family }); } return result; }