static IType CreateCodeBesideClass(DocumentInfo info, DocumentReferenceManager refman) { var v = new MemberListVisitor(info.AspNetDocument, refman); info.AspNetDocument.RootNode.AcceptVisit(v); var t = new DomType(info.ClassName); t.CompilationUnit = new CompilationUnit(info.AspNetDocument.FileName); var dom = refman.TypeCtx.ProjectDom; var baseType = dom.GetType(info.BaseType); foreach (var m in CodeBehind.GetDesignerMembers(v.Members.Values, baseType, null, dom, null)) { t.Add(new DomField(m.Name, Modifiers.Protected, m.Location, new DomReturnType(m.Type))); } return(t); }
static IUnresolvedTypeDefinition CreateCodeBesideClass(DocumentInfo info, DocumentReferenceManager refman) { var v = new MemberListVisitor(refman); info.AspNetDocument.RootNode.AcceptVisit(v); var t = new ICSharpCode.NRefactory.TypeSystem.Implementation.DefaultUnresolvedTypeDefinition(info.ClassName); var dom = refman.TypeCtx.Compilation; var baseType = ReflectionHelper.ParseReflectionName(info.BaseType).Resolve(dom); foreach (var m in CodeBehind.GetDesignerMembers(v.Members.Values, baseType, null)) { t.Members.Add(new ICSharpCode.NRefactory.TypeSystem.Implementation.DefaultUnresolvedField(t, m.Name) { Accessibility = Accessibility.Protected, ReturnType = m.Type.ToTypeReference() }); } return(t); }
public static System.CodeDom.CodeCompileUnit GenerateCodeBehind(AspNetAppProject project, string filename, AspNetParsedDocument document, List <CodeBehindWarning> errors) { string className = document.Info.InheritedClass; if (document.HasErrors) { AddFail(errors, document, document.Errors.Where(x => x.ErrorType == ErrorType.Error).First()); return(null); } if (string.IsNullOrEmpty(className)) { return(null); } var refman = new DocumentReferenceManager(project) { Doc = document }; var memberList = new MemberListVisitor(document, refman); document.RootNode.AcceptVisit(memberList); var err = memberList.Errors.Where(x => x.ErrorType == ErrorType.Error).FirstOrDefault(); if (err != null) { AddFail(errors, document, err); return(null); } //initialise the generated type var ccu = new CodeCompileUnit(); var namespac = new CodeNamespace(); ccu.Namespaces.Add(namespac); var typeDecl = new System.CodeDom.CodeTypeDeclaration() { IsClass = true, IsPartial = true, }; namespac.Types.Add(typeDecl); //name the class and namespace int namespaceSplit = className.LastIndexOf('.'); string namespaceName = null; 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 { ProjectFile resolvedMaster = project.ResolveVirtualPath(document.Info.MasterPageTypeVPath, document.FileName); AspNetParsedDocument masterParsedDocument = null; if (resolvedMaster != null) { masterParsedDocument = ProjectDomService.Parse(project, resolvedMaster.FilePath) as AspNetParsedDocument; } if (masterParsedDocument != null && !String.IsNullOrEmpty(masterParsedDocument.Info.InheritedClass)) { masterTypeName = masterParsedDocument.Info.InheritedClass; } else { errors.Add(new CodeBehindWarning(String.Format("Could not find type for master '{0}'", document.Info.MasterPageTypeVPath), document.FileName)); } } catch (Exception ex) { errors.Add(new CodeBehindWarning(String.Format("Could not find type for master '{0}'", document.Info.MasterPageTypeVPath), document.FileName)); LoggingService.LogWarning("Error resolving master page type", ex); } } if (masterTypeName != null) { var masterProp = new CodeMemberProperty() { Name = "Master", Type = new CodeTypeReference(masterTypeName), HasGet = true, HasSet = false, Attributes = System.CodeDom.MemberAttributes.Public | System.CodeDom.MemberAttributes.New | System.CodeDom.MemberAttributes.Final, }; masterProp.GetStatements.Add(new System.CodeDom.CodeMethodReturnStatement( new System.CodeDom.CodeCastExpression(masterTypeName, new System.CodeDom.CodePropertyReferenceExpression( new System.CodeDom.CodeBaseReferenceExpression(), "Master")))); typeDecl.Members.Add(masterProp); } //shortcut building the existing members type map if (memberList.Members.Count == 0) { return(ccu); } var dom = refman.TypeCtx.ProjectDom; var cls = dom.GetType(className); var members = GetDesignerMembers(memberList.Members.Values, cls, filename, dom, dom); //add fields for each control in the page foreach (var member in members) { var type = new CodeTypeReference(member.Type.FullName); typeDecl.Members.Add(new CodeMemberField(type, member.Name) { Attributes = MemberAttributes.Family }); } return(ccu); }