public void LoadXmlDocumentation() { if (!loaded) { TowelSystemExtensions.LoadXmlDocumentation(File.ReadAllText(@"Towel_Testing.xml")); loaded = true; } }
static void Main() { // This program generates HTML from the XML documentation of the code in the Towel project. TowelSystemExtensions.LoadXmlDocumentation(File.ReadAllText(@"..\..\..\..\..\Sources\Towel\Towel.xml")); Assembly assembly = typeof(Towel.Stepper).Assembly; Type[] exportedTypes = assembly.GetExportedTypes(); // First Pass: Build Namespace Hierarchy List <Namespace> namespaceHierarchy = new List <Namespace>(); Dictionary <string, Namespace> namespaceMap = new Dictionary <string, Namespace>(); foreach (string fullNamespace in exportedTypes.Select(x => x.Namespace).Distinct()) { if (!namespaceMap.ContainsKey(fullNamespace)) { string[] namespaces = fullNamespace.Split('.'); string currentFullNamespace = null; Namespace?parent = null; foreach (string @namespace in namespaces) { currentFullNamespace = currentFullNamespace is null ? @namespace : currentFullNamespace + "." + @namespace; if (!namespaceMap.TryGetValue(currentFullNamespace, out Namespace current)) { current = (@namespace, new List <string>(), new List <Type>()); if (parent.HasValue) { parent.Value.Item2.Add(currentFullNamespace); } else { namespaceHierarchy.Add(current); } namespaceMap[currentFullNamespace] = current; } parent = current; } } } // Second Pass: Add Types To the Namespace Hierarchy foreach (Type type in exportedTypes) { if (!type.IsNested) { namespaceMap[type.Namespace].Item3.Add(type); } } // Third Pass: Convert The Namespace Hierarchy To HTML (And Sort Alphabetically) StringBuilder stringBuilder = new StringBuilder(); namespaceHierarchy.Sort((a, b) => a.Item1.CompareTo(b.Item1)); foreach (Namespace @namespace in namespaceHierarchy) { ConvertNamespaceToHtml(@namespace); } void ConvertNamespaceToHtml(Namespace @namespace) { stringBuilder.Append("<li class=\"namespace\">"); stringBuilder.AppendLine(@namespace.Item1); stringBuilder.AppendLine("<ul>"); // Namespaces @namespace.Item2.Sort(); @namespace.Item2.ForEach(x => ConvertNamespaceToHtml(namespaceMap[x])); // Types @namespace.Item3.Sort((a, b) => a.Name.CompareTo(b.Name)); @namespace.Item3.ForEach(ConvertTypeToHtml); stringBuilder.AppendLine("</ul>"); stringBuilder.AppendLine("</li>"); } void ConvertTypeToHtml(Type type) { string typeToString = type.ConvertToCsharpSource(); if (!string.IsNullOrEmpty(typeToString)) { stringBuilder.Append("<li class=\"type\">"); stringBuilder.AppendLine(typeToString.Substring(typeToString.LastIndexOf('.') + 1)); stringBuilder.AppendLine(HttpUtility.HtmlEncode(type.GetDocumentation())); stringBuilder.AppendLine("<ul>"); // Nested Types List <Type> nestedTypes = type.GetNestedTypes().ToList(); nestedTypes.Sort((a, b) => a.Name.CompareTo(b.Name)); nestedTypes.ForEach(ConvertTypeToHtml); // Fields foreach (FieldInfo fieldInfo in type.GetFields()) { if (!(fieldInfo.DeclaringType.FullName is null)) { stringBuilder.AppendLine("<li class=\"field\">"); stringBuilder.AppendLine(fieldInfo.Name); stringBuilder.AppendLine(HttpUtility.HtmlEncode(fieldInfo.GetDocumentation())); stringBuilder.AppendLine("</li>"); } } // Properties foreach (PropertyInfo propertyInfo in type.GetProperties()) { if (!(propertyInfo.DeclaringType.FullName is null)) { stringBuilder.AppendLine("<li class=\"property\">"); stringBuilder.AppendLine(propertyInfo.Name); stringBuilder.AppendLine(HttpUtility.HtmlEncode(propertyInfo.GetDocumentation())); stringBuilder.AppendLine("</li>"); } } // Constructors foreach (ConstructorInfo constructorInfo in type.GetConstructors()) { if (!(constructorInfo.DeclaringType.FullName is null)) { stringBuilder.AppendLine("<li class=\"constructor\">"); stringBuilder.AppendLine(constructorInfo.Name); stringBuilder.AppendLine(HttpUtility.HtmlEncode(constructorInfo.GetDocumentation())); stringBuilder.AppendLine("</li>"); } } // Methods foreach (MethodInfo methodInfo in type.GetMethods()) { if (!(methodInfo.DeclaringType.FullName is null)) { stringBuilder.AppendLine("<li class=\"method\">"); stringBuilder.AppendLine(methodInfo.Name); stringBuilder.AppendLine(HttpUtility.HtmlEncode(methodInfo.GetDocumentation())); stringBuilder.AppendLine("</li>"); } } stringBuilder.AppendLine("</ul>"); stringBuilder.AppendLine("</li>"); } } File.WriteAllText("TowelDocumentation.html", stringBuilder.ToString()); }
static void Main(string[] args) { Console.WriteLine("You are runnning the Extensions example."); Console.WriteLine("=========================================="); Console.WriteLine(); #region Decimal To Words Console.WriteLine(" Converting Decimal To Words---------------------------"); Console.WriteLine(); decimal value1 = 12345.6789m; Console.WriteLine(" Value1 = " + value1); Console.WriteLine(" Value1 To Words = " + value1.ToEnglishWords()); Console.WriteLine(); decimal value2 = 999.888m; Console.WriteLine(" Value2 = " + value2); Console.WriteLine(" Value2 To Words = " + value2.ToEnglishWords()); Console.WriteLine(); decimal value3 = 1111111.2m; Console.WriteLine(" Value3 = " + value3); Console.WriteLine(" Value3 To Words = " + value3.ToEnglishWords()); Console.WriteLine(); #endregion #region Type To C# Source Code Console.WriteLine(" Type To C# Source Code---------------------------"); Console.WriteLine(); Console.WriteLine(" Note: this can be useful for runtime compilation from strings"); Console.WriteLine(); Console.WriteLine(" " + typeof(IOmnitreePoints <Vector <double>, double, double, double>).ConvertToCsharpSource()); Console.WriteLine(); Console.WriteLine(" " + typeof(Symbolics.Add).ConvertToCsharpSource()); Console.WriteLine(); #endregion #region Random Extensions Console.WriteLine(" Random Extensions---------------------------"); Console.WriteLine(); Console.WriteLine(" Note: there are overloads of these methods"); Console.WriteLine(); Random random = new Random(); Console.WriteLine(" Random.NextLong(): " + random.NextLong()); Console.WriteLine(" Random.NextDateTime(): " + random.NextDateTime()); Console.WriteLine(" Random.NextAlphaNumericString(15): " + random.NextAlphaNumericString(15)); Console.WriteLine(" Random.NextChar('a', 'z'): " + random.NextChar('a', 'z')); Console.WriteLine(" Random.NextDecimal(): " + random.NextDecimal()); Console.WriteLine(" Random.NextTimeSpan(): " + random.NextTimeSpan()); Console.WriteLine(); #endregion #region XML Code Documentation Via Reflection Console.WriteLine(" XML Code Documentation Extensions------------"); Console.WriteLine(); Console.WriteLine(" You can access XML on source code via reflection"); Console.WriteLine(" using Towel's extension methods."); Console.WriteLine(); // This function loads in XML documentation so you can access it via reflection. TowelSystemExtensions.LoadXmlDocumentation(File.ReadAllText(@"..\..\..\..\..\Sources\Towel\Towel.xml")); Console.WriteLine(" XML Documentation On Towel.Mathematics.Compute:"); Console.WriteLine(typeof(Compute).GetDocumentation()); Console.WriteLine(); Console.WriteLine(" XML Documentation On Towel.Mathematics.Constant<float>.Pi:"); Console.WriteLine(typeof(Constant <float>).GetField(nameof(Constant <float> .Pi)).GetDocumentation()); #endregion Console.WriteLine(); Console.WriteLine("================================================="); Console.WriteLine("Example Complete..."); Console.ReadLine(); }