/// <summary> /// 获取指定对象在XML文档注释中的ID字符串 /// </summary> /// <param name="info">要获取ID字符串的对象</param> /// <returns>指定对象在XML文档注释中的ID字符串</returns> public static string GetMemberID(MemberInfo info) { switch (info.MemberType) { case MemberTypes.TypeInfo: { return("T:" + DocumentBuilderUtility.GetTypeID(info as Type)); } case MemberTypes.NestedType: { return("T:" + DocumentBuilderUtility.GetTypeID(info as Type)); } case MemberTypes.Constructor: { string name = "M:", ps = ""; ConstructorInfo ctor = info as ConstructorInfo; name += DocumentBuilderUtility.GetTypeID(ctor.DeclaringType) + ".#ctor"; foreach (ParameterInfo param in ctor.GetParameters()) { string p; p = DocumentBuilderUtility.GetParameterTypeID(param.ParameterType); if (param.IsOut) { p = p.Replace('&', '@'); } if (string.IsNullOrEmpty(ps)) { ps = p; } else { ps += "," + p; } } if (!string.IsNullOrEmpty(ps)) { name += "(" + ps + ")"; } return(name); } case MemberTypes.Method: { string name = "M:", ps = ""; MethodInfo method = info as MethodInfo; name += DocumentBuilderUtility.GetTypeID(method.DeclaringType) + "." + method.Name; foreach (ParameterInfo param in method.GetParameters()) { string p; p = DocumentBuilderUtility.GetParameterTypeID(param.ParameterType); if (param.ParameterType.IsByRef) { p = p.Replace('&', '@'); } if (string.IsNullOrEmpty(ps)) { ps = p; } else { ps += "," + p; } } if (!string.IsNullOrEmpty(ps)) { name += "(" + ps + ")"; } return(name); } case MemberTypes.Property: { PropertyInfo property = info as PropertyInfo; MethodInfo pm = property.GetGetMethod(true); if (pm == null) { pm = property.GetSetMethod(true); } ParameterInfo[] parameters = pm.GetParameters(); if (pm == null || parameters.Length == 0) { return("P:" + DocumentBuilderUtility.GetTypeID(property.DeclaringType) + "." + property.Name); } else { string name = "P:" + DocumentBuilderUtility.GetTypeID(property.DeclaringType) + "." + property.Name; string ps = ""; foreach (ParameterInfo param in parameters) { string p; p = DocumentBuilderUtility.GetParameterTypeID(param.ParameterType); if (string.IsNullOrEmpty(ps)) { ps = p; } else { ps += "," + p; } } if (!string.IsNullOrEmpty(ps)) { name += "(" + ps + ")"; } return(name); } } case MemberTypes.Event: { EventInfo ent = info as EventInfo; return("E:" + DocumentBuilderUtility.GetTypeID(ent.DeclaringType) + "." + ent.Name); } case MemberTypes.Field: { FieldInfo field = info as FieldInfo; return("F:" + DocumentBuilderUtility.GetTypeID(field.DeclaringType) + "." + field.Name); } default: return(null); } }
/// <summary> /// 创建目录树,并确定命名空间,类型,成员对应的页面的文件名 /// </summary> /// <returns></returns> private void CreateContentTree() { int pageCount = 0; foreach (Type type in DataProvider.GetSelectedTypes()) { ContentTreeItem nsItem = null, typeParentItem = null; string typeParentName = ""; nsItem = _root.Find(type.Namespace); if (nsItem == null) { string nsPageFileName = (++pageCount).ToString("000000") + Resources.Extension; _root.Add(nsItem = new ContentTreeItem(type.Namespace, nsPageFileName)); if (!string.IsNullOrEmpty(type.Namespace)) { _namespaceFileName.Add(type.Namespace, nsPageFileName); } } if (type.IsClass) { typeParentName = "Class"; } else if (type.IsEnum) { typeParentName = "Enumeration"; } else if (type.IsValueType) { typeParentName = "Structure"; } else if (type.IsInterface) { typeParentName = "Interface"; } else { typeParentName = "Delegate"; } typeParentItem = nsItem.Find(typeParentName); if (typeParentItem == null) { nsItem.Add(typeParentItem = new ContentTreeItem(typeParentName, "")); } ContentTreeItem typeItem = new ContentTreeItem(DocumentBuilderUtility.GetTypeDefinition(type, false), (++pageCount).ToString("000000") + Resources.Extension, type); _memberFileName.Add(DocumentBuilderUtility.GetMemberID(type), typeItem.FileName); typeParentItem.Add(typeItem); foreach (MemberInfo memberInfo in DataProvider.GetTypeSelectedMembers(type)) { string parentItemName = ""; #region 添加成员 switch (memberInfo.MemberType) { case MemberTypes.Constructor: { parentItemName = "Constructor"; break; } case MemberTypes.Method: { MethodInfo method = memberInfo as MethodInfo; if (!method.IsSpecialName) { if (method.IsPublic) { parentItemName = "PublicMethod"; } else if (method.IsPrivate) { parentItemName = "PrivateMethod"; } else { parentItemName = "ProtectedMethod"; } } break; } case MemberTypes.Field: { FieldInfo field = memberInfo as FieldInfo; if (field.IsPublic) { parentItemName = "PublicField"; } else if (field.IsPrivate) { parentItemName = "PrivateField"; } else { parentItemName = "ProtectedField"; } break; } case MemberTypes.Property: { PropertyInfo property = memberInfo as PropertyInfo; MethodInfo pm = property.GetGetMethod(true); if (pm == null) { pm = property.GetSetMethod(true); } if (pm != null) { if (pm.IsPublic) { parentItemName = "PublicProperty"; } else if (pm.IsPrivate) { parentItemName = "PrivateProperty"; } else { parentItemName = "ProtectedProperty"; } } break; } case MemberTypes.Event: { parentItemName = "Event"; break; } } if (!string.IsNullOrEmpty(parentItemName)) { ContentTreeItem parentItem = typeItem.Find(parentItemName); if (parentItem == null) { parentItem = new ContentTreeItem(parentItemName, typeItem.FileName + "#" + parentItemName); typeItem.Add(parentItem); } if (memberInfo.MemberType == MemberTypes.Constructor) { string fileName = (++pageCount).ToString("000000") + Resources.Extension; parentItem.Add(new ContentTreeItem(DocumentBuilderUtility.GetTypeDefinition(memberInfo.ReflectedType, false), fileName, memberInfo)); } else { string fileName = _memberFileName[DocumentBuilderUtility.GetMemberID(memberInfo)] as string; if (string.IsNullOrEmpty(fileName)) { fileName = (++pageCount).ToString("000000") + Resources.Extension; _memberFileName.Add(DocumentBuilderUtility.GetMemberID(memberInfo), fileName); } parentItem.Add(new ContentTreeItem(memberInfo.Name, fileName, memberInfo)); } } #endregion } } _root.Sort(); _total = pageCount; }
/// <summary> /// 生成类型页面 /// </summary> /// <param name="memberItems">该类型包含的所有成员</param> /// <param name="typeItem">类型包含信息(名称,页面文件名等)</param> protected override void CreateTypePage(IGetMembers memberItems, ContentTreeItem typeItem) { HtmlText pageContent = new HtmlText(); Type type = typeItem.Info as Type; MemberXmlElement typeData = DataProvider.GetMemberXmlNode(type); int secID = 0; foreach (PageSection section in TypePageSections) { string secText = null; switch (section.SectionType) { case PageSectionType.Constructor: secText = GetTypeMemberCategorySection(memberItems, type, typeData, "Constructor"); break; case PageSectionType.PublicMethod: secText = GetTypeMemberCategorySection(memberItems, type, typeData, "PublicMethod"); break; case PageSectionType.ProtectedMethod: secText = GetTypeMemberCategorySection(memberItems, type, typeData, "ProtectedMethod"); break; case PageSectionType.PrivateMethod: secText = GetTypeMemberCategorySection(memberItems, type, typeData, "PrivateMethod"); break; case PageSectionType.PublicProperty: secText = GetTypeMemberCategorySection(memberItems, type, typeData, "PublicProperty"); break; case PageSectionType.ProtectedProperty: secText = GetTypeMemberCategorySection(memberItems, type, typeData, "ProtectedProperty"); break; case PageSectionType.PrivateProperty: secText = GetTypeMemberCategorySection(memberItems, type, typeData, "PrivateProperty"); break; case PageSectionType.PublicField: secText = GetTypeMemberCategorySection(memberItems, type, typeData, "PublicField"); break; case PageSectionType.ProtectedField: secText = GetTypeMemberCategorySection(memberItems, type, typeData, "ProtectedField"); break; case PageSectionType.PrivateField: secText = GetTypeMemberCategorySection(memberItems, type, typeData, "PrivateField"); break; case PageSectionType.Event: secText = GetTypeMemberCategorySection(memberItems, type, typeData, "Event"); break; case PageSectionType.Remarks: { if (typeData != null) { HtmlText remarks = new HtmlText(); remarks.AppendPre(GetChildNodeInnerText(typeData, "remarks")); if (remarks.Length > 0) { Hashtable sectionValues = new Hashtable(); sectionValues["ID"] = "remarks"; sectionValues["Title"] = Resources.String42; sectionValues["Content"] = tempRemarks.Render(remarks); secText = tempSection.Render(sectionValues); } } break; } case PageSectionType.Example: { if (typeData != null) { HtmlText example = new HtmlText(); example.AppendPre(GetChildNodeInnerText(typeData, "example")); if (example.Length > 0) { Hashtable sectionValues = new Hashtable(); sectionValues["ID"] = "example"; sectionValues["Title"] = Resources.String43; sectionValues["Content"] = tempRemarks.Render(example); secText = tempSection.Render(sectionValues); } } break; } case PageSectionType.FromXML: { if (typeData != null) { string text = GetChildNodeInnerText(typeData, section.XmlSource); if (!string.IsNullOrEmpty(text)) { Hashtable sectionValues = new Hashtable(); sectionValues["ID"] = "SEC" + secID.ToString("00"); sectionValues["Title"] = section.Name; sectionValues["Content"] = text; secText = tempSection.Render(sectionValues); } } break; } } if (!string.IsNullOrEmpty(secText)) { pageContent.AppendPre(secText); } secID++; } Hashtable values = new Hashtable(); values["CollapseAll"] = Resources.strCollapseAll; values["ExpandAll"] = Resources.strExpandAll; values["PIC"] = Resources.strPic; values["Title"] = HtmlText.HtmlFormat(DocumentBuilderUtility.GetTypeDefinition(type, false)) + " " + NameToTypeDictionary[typeItem.Parent.Name]; values["Summary"] = GetChildNodeInnerText(typeData, "summary"); values["Content"] = pageContent; values["Encoding"] = TargetEncoding.HeaderName; tempPage.SaveAs( HtmlFileDirectory + "\\" + typeItem.FileName, TargetEncoding, values ); }
private string GetTypeMemberCategorySection(IGetMembers memberItems, Type type, MemberXmlElement typeData, string category) { if (memberItems.GetMembers(category) != null) { string secText = null; string sectionTitle = NameToTitleDictionary[category] as string; HtmlText sectionContent = new HtmlText(); foreach (ContentTreeItem memberItem in memberItems.GetMembers(category)) { MemberXmlElement memberData = DataProvider.GetMemberXmlNode(memberItem.Info); string summary = ""; summary = GetChildNodeInnerText(memberData, "summary"); if (memberItem.Info.DeclaringType != type) { summary += string.Format(Resources.String56, memberItem.Info.DeclaringType.Name); } if (string.IsNullOrEmpty(summary)) { summary = " "; } HtmlText imgs = new HtmlText(); imgs.AppendPre(tempImg.Render(NameToTitleDictionary[category], category + ".bmp")); if (IsStatic(memberItem.Info)) { imgs.AppendPre(" "); imgs.AppendPre(tempImg.Render("static", "static.bmp")); } string mname = ""; mname += HtmlText.HtmlFormat(memberItem.Name); MethodInfo method = memberItem.Info as MethodInfo; //if (method != null) if (method != null) { //泛型方法 //if (method.IsGenericMethod == true) //{ if (memberData != null && memberData.Data != null) { XmlNodeList lst = memberData.Data.GetElementsByTagName("typeparam"); if (lst.Count > 0) { mname += HtmlText.HtmlFormat("<"); mname += (lst[0] as XmlElement).GetAttribute("name"); for (int i = 1; i < lst.Count; i++) { mname += ("," + (lst[i] as XmlElement).GetAttribute("name")); } //泛型方法处理 //name = name.Replace("``" + elem.GetElementsByTagName("typeparam").Count, ""); mname += HtmlText.HtmlFormat(">"); } } //} ParameterInfo[] parameters = method.GetParameters(); if (parameters.Length > 0) { string strParams = ""; foreach (ParameterInfo param in parameters) { if (string.IsNullOrEmpty(strParams)) { strParams = "" + SimpleParamName(DocumentBuilderUtility.GetTypeDefinition(param.ParameterType)) + ""; } else { strParams += "," + SimpleParamName(DocumentBuilderUtility.GetTypeDefinition(param.ParameterType)) + ""; } } mname += ("(" + strParams + ")"); } else { mname += ("()"); } } string strRow = tempTypeTable_Row.Render( imgs, memberItem.FileName, //HtmlText.HtmlFormat(memberItem.Name), mname, summary ); sectionContent.AppendPre(strRow); } if (sectionContent.Length > 0) { secText = CreateTableSection(category, sectionTitle, sectionContent.ToString()); } return(secText); } else { return(null); } }
private string CreateMemberDeclaration(MemberInfo info, MemberXmlElement memberData) { StringBuilder declaration = new StringBuilder(); switch (info.MemberType) { case MemberTypes.Method: { MethodInfo method = info as MethodInfo; if (method.IsStatic) { declaration.Append("static "); } if (method.IsVirtual) { declaration.Append("virtual "); } declaration.Append(DocumentBuilderUtility.GetTypeDefinition(method.ReturnType) + " " + method.Name); //泛型方法处理 //if (method.IsGenericMethod == true) //if (method.IsGenericMethod == true) //{ if (memberData != null && memberData.Data != null) { XmlNodeList lst = memberData.Data.GetElementsByTagName("typeparam"); if (lst.Count > 0) { declaration.Append("<"); declaration.Append((lst[0] as XmlElement).GetAttribute("name")); for (int i = 1; i < lst.Count; i++) { declaration.Append("," + (lst[i] as XmlElement).GetAttribute("name")); } declaration.Append(">"); } } //} ParameterInfo[] parameters = method.GetParameters(); if (parameters.Length > 0) { string strParams = ""; foreach (ParameterInfo param in parameters) { if (string.IsNullOrEmpty(strParams)) { strParams = " " + DocumentBuilderUtility.GetTypeDefinition(param.ParameterType) + " " + param.Name; } else { strParams += ",\r\n " + DocumentBuilderUtility.GetTypeDefinition(param.ParameterType) + " " + param.Name; } } declaration.Append("(\r\n" + strParams + "\r\n);"); } else { declaration.Append("();"); } break; } case MemberTypes.Property: { PropertyInfo property = info as PropertyInfo; MethodInfo method = property.GetGetMethod(true); if (method == null) { method = property.GetSetMethod(true); } if (method.IsStatic) { declaration.Append("static "); } if (method.IsVirtual) { declaration.Append("virtual "); } declaration.Append(DocumentBuilderUtility.GetTypeDefinition(method.ReturnType) + " " + property.Name); if (method != null) { ParameterInfo[] parameters = method.GetParameters(); if (parameters.Length > 0) { string strParams = ""; foreach (ParameterInfo param in parameters) { if (string.IsNullOrEmpty(strParams)) { strParams = " " + DocumentBuilderUtility.GetTypeDefinition(param.ParameterType) + " " + param.Name; } else { strParams += ",\r\n " + DocumentBuilderUtility.GetTypeDefinition(param.ParameterType) + " " + param.Name; } } declaration.Append("[\r\n" + strParams + "\r\n]"); } declaration.ToString(); } declaration.Append("{"); if (property.GetGetMethod(true) != null) { declaration.Append(" get;"); } if (property.GetSetMethod(true) != null) { declaration.Append(" set;"); } declaration.Append(" }"); break; } case MemberTypes.Constructor: { ConstructorInfo ctor = info as ConstructorInfo; declaration.Append(DocumentBuilderUtility.GetTypeDefinition(ctor.DeclaringType, false)); ParameterInfo[] parameters = ctor.GetParameters(); if (parameters.Length > 0) { string strParams = ""; foreach (ParameterInfo param in parameters) { if (string.IsNullOrEmpty(strParams)) { strParams = " " + DocumentBuilderUtility.GetTypeDefinition(param.ParameterType) + " " + param.Name; } else { strParams += ",\r\n " + DocumentBuilderUtility.GetTypeDefinition(param.ParameterType) + " " + param.Name; } } declaration.Append("(\r\n" + strParams + "\r\n);"); } else { declaration.Append("();"); } break; } case MemberTypes.Field: { FieldInfo field = info as FieldInfo; if (field.IsStatic) { declaration.Append("static "); } declaration.Append(field.ToString()); break; } case MemberTypes.Event: { EventInfo ent = info as EventInfo; declaration.Append(ent.ToString()); break; } } return(declaration.ToString()); }