Mercurial > servermonitor
diff ServerMonitor/Helpers.cs @ 17:68d7834dc28e
More comments.
author | Brad Greco <brad@bgreco.net> |
---|---|
date | Sat, 25 May 2019 15:14:26 -0400 |
parents | 052aa62cb42a |
children | 7645122aa7a9 |
line wrap: on
line diff
--- a/ServerMonitor/Helpers.cs Tue Apr 30 20:40:58 2019 -0400 +++ b/ServerMonitor/Helpers.cs Sat May 25 15:14:26 2019 -0400 @@ -1,49 +1,66 @@ -using Renci.SshNet; -using Renci.SshNet.Common; -using ServerMonitorApp.Properties; +using ServerMonitorApp.Properties; using System; -using System.Collections.Generic; using System.ComponentModel; using System.Drawing; using System.Linq; using System.Net.NetworkInformation; -using System.Text; using System.Windows.Forms; namespace ServerMonitorApp { static class Helpers { + /// <summary>Resizes the image on a button to fit inside the button's dimensions.</summary> + /// <param name="button">A button containing an image.</param> public static void FormatImageButton(Button button) { button.Image = new Bitmap(button.Image, button.Height - 8, button.Height - 8); } + /// <summary>Recursively collects the messages for an exception and its inner exceptions.</summary> + /// <param name="ex">The parent exception.</param> + /// <returns>A string containing the messages for the exception and its inner exceptions.</returns> public static string GetAllMessages(this Exception ex) { return "\t" + (ex.InnerException == null ? ex.Message : ex.Message + Environment.NewLine + ex.InnerException.GetAllMessages()); } + /// <summary>Gets the value of the DisplayNameAttribute for a type.</summary> + /// <param name="type">The type to get the display name of.</param> + /// <returns>The type's display name, or the type name if no display name is defined.</returns> public static string GetDisplayName(Type type) { return type?.GetAttribute<DisplayNameAttribute>()?.DisplayName ?? type?.Name ?? "null"; } + /// <summary>Checks whether a string is null, an empty string, or only contains white space.</summary> + /// <param name="aString">The string to test.</param> + /// <returns>True if the string is null, an empty string, or only contains white space. False otherwise.</returns> public static bool IsNullOrEmpty(this string aString) { return aString == null || aString.Trim() == string.Empty; } + /// <summary>Converts all newlines in a string to unix format.</summary> + /// <param name="aString">The string to convert.</param> + /// <returns>The string with all newlines converted to unix format.</returns> public static string ConvertNewlines(this string aString) { return aString.Replace("\r\n", "\n").Replace('\r', '\n'); } + /// <summary>Gets an attribute on a class.</summary> + /// <typeparam name="T">The type of the attribute to return.</typeparam> + /// <param name="type">The type of the class the attribute is on.</param> + /// <returns>The attribute, or null if the attribute does not exist on the class.</returns> public static T GetAttribute<T>(this Type type) where T : Attribute { return type.GetCustomAttributes(typeof(T), false).SingleOrDefault() as T; } + /// <summary>Gets an image associated with a check status for use in the UI.</summary> + /// <param name="checkStatus">The check status.</param> + /// <returns>The image associated with the check status.</returns> public static Image GetImage(this CheckStatus checkStatus) { switch (checkStatus) @@ -58,6 +75,9 @@ } } + /// <summary>Gets a program icon associated with a check status.</summary> + /// <param name="checkStatus">The check status.</param> + /// <returns>The program icon associated with the check status.</returns> public static Icon GetIcon(this CheckStatus checkStatus) { switch (checkStatus) @@ -69,6 +89,10 @@ } } + /// <summary>Returns whether an enum is in a list of values.</summary> + /// <param name="value">The value to check.</param> + /// <param name="values">The list of possible values.</param> + /// <returns>True if the value was in the list, false otherwise.</returns> public static bool In(this Enum value, params Enum[] values) { return values.Contains(value); }