Last active
June 23, 2016 03:15
-
-
Save lolp1/e7600911deef193ec443fb506865743d to your computer and use it in GitHub Desktop.
Misc. Extensions for C#
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Threading.Tasks; | |
namespace Stylesoft.Common.Extensions | |
{ | |
public static class ActionExtensions | |
{ | |
public static async void ExecuteDelayed(this Action action, TimeSpan span) | |
{ | |
await Task.Delay(span); | |
action(); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Concurrent; | |
using System.Collections.Generic; | |
using System.Diagnostics; | |
namespace Stylesoft.Common.Extensions | |
{ | |
public static class DictionaryExtensions | |
{ | |
public static void AddItem<T>(this Dictionary<string, object> dictionary, string key, T value) | |
{ | |
if (dictionary.ContainsKey(key)) | |
{ | |
throw new Exception("The dictionary already contains the key: " + key); | |
} | |
dictionary[key] = value; | |
} | |
public static T GetItem<T>(this Dictionary<string, object> dictionary, string key) | |
{ | |
if (!dictionary.ContainsKey(key)) | |
{ | |
throw new KeyNotFoundException("The dictionary does not contain the key: " + key); | |
} | |
return dictionary[key].RequireType<T>(); | |
} | |
public static T GetItem<T>(this ConcurrentDictionary<string, object> dictionary, string key) | |
{ | |
if (!dictionary.ContainsKey(key)) | |
{ | |
throw new KeyNotFoundException("The dictionary does not contain the key: " + key); | |
} | |
return dictionary[key].RequireType<T>(); | |
} | |
public static void AddItem<T>(this ConcurrentDictionary<string, object> dictionary, string key, T value) | |
{ | |
if (dictionary.ContainsKey(key)) | |
{ | |
throw new Exception("The dictionary already contains the key: " + key); | |
} | |
dictionary.Add(key, value); | |
} | |
public static void Add<TKey, TValue>(this ConcurrentDictionary<TKey, TValue> dict, TKey key, TValue value) | |
{ | |
Add(dict, key, value, TimeSpan.FromSeconds(2)); | |
} | |
public static void Remove<TKey, TValue>(this ConcurrentDictionary<TKey, TValue> dict, TKey key) | |
{ | |
Remove(dict, key, TimeSpan.FromSeconds(2)); | |
} | |
public static void Add<TKey, TValue>(this ConcurrentDictionary<TKey, TValue> dict, TKey key, TValue value, | |
TimeSpan timeout) | |
{ | |
if (dict.ContainsKey(key)) | |
{ | |
return; | |
} | |
var stopWatch = Stopwatch.StartNew(); | |
while (!dict.TryAdd(key, value)) | |
{ | |
if (stopWatch.Elapsed <= timeout) | |
{ | |
continue; | |
} | |
stopWatch.Stop(); | |
throw new TimeoutException( | |
$"Could not add the item to the concurrent dictionary with in the given time {timeout}"); | |
} | |
stopWatch.Stop(); | |
} | |
public static void Remove<TKey, TValue>(this ConcurrentDictionary<TKey, TValue> dict, TKey key, TimeSpan timeout) | |
{ | |
if (!dict.ContainsKey(key)) | |
{ | |
return; | |
} | |
TValue value; | |
var stopWatch = Stopwatch.StartNew(); | |
while (!dict.TryRemove(key, out value)) | |
{ | |
if (stopWatch.Elapsed < timeout) | |
{ | |
continue; | |
} | |
stopWatch.Stop(); | |
throw new TimeoutException( | |
$"Could not remove the item to the concurrent dictionary with in the given time {timeout}"); | |
} | |
stopWatch.Stop(); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
namespace Stylesoft.Common.Extensions | |
{ | |
public static class ObjextExtensions | |
{ | |
public static T RequireType<T>(this object @object) | |
{ | |
if (!(@object is T)) | |
{ | |
throw new InvalidCastException("RequireType<T> cast"); | |
} | |
return (T) @object; | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Linq; | |
using System.Windows.Documents; | |
using System.Windows.Forms; | |
using System.Windows.Media; | |
using RichTextBox = System.Windows.Controls.RichTextBox; | |
namespace Stylesoft.Common.Extensions | |
{ | |
public static class RichTextBoxExtensions | |
{ | |
private const int MaxCol1Of2 = 45; | |
private const string ColSeparator = " : "; | |
private static class Lines | |
{ | |
public static string New => Environment.NewLine; | |
} | |
public static void Append(this RichTextBox rtb, | |
Color color, string text) | |
{ | |
var endPos = rtb.Document.ContentEnd; | |
var range = new TextRange(endPos, endPos); | |
rtb.Dispatcher.Invoke(() => | |
{ | |
range.Text = text; | |
range.ApplyPropertyValue(TextElement.ForegroundProperty, new SolidColorBrush(color)); | |
rtb.ScrollToEnd(); | |
}); | |
} | |
public static void Clear(this RichTextBox rtb) | |
=> rtb.Document?.Blocks.Clear(); | |
public static void WriteColumns(this RichTextBox rtb, | |
Color color, params string[] columnTexts) | |
{ | |
// ReSharper disable once SwitchStatementMissingSomeCases | |
switch (columnTexts.Length) | |
{ | |
case 1: | |
rtb.WriteLine(color, columnTexts[0]); | |
break; | |
case 2: | |
rtb.Write2Cols(color, columnTexts[0], columnTexts[1]); | |
break; | |
} | |
} | |
public static void WriteLine(this RichTextBox rtb, Color color, string text) | |
{ | |
rtb.Append(color, text + Lines.New); | |
} | |
public static void WriteCol1Of2(this RichTextBox rtb, Color color, string text, int colWidth = MaxCol1Of2) | |
{ | |
rtb.Append(color, text.AlignLeft(colWidth, ColSeparator)); | |
} | |
public static void WriteCol2Of2(this RichTextBox rtb, Color color, string text) | |
{ | |
rtb.WriteLine(color, text); | |
} | |
public static void WriteBlankLine(this RichTextBox rtb, int lineCount = 1) | |
{ | |
rtb.Append(Colors.Transparent, Lines.New.Repeat(lineCount)); | |
} | |
public static void Write2Cols(this RichTextBox rtb, | |
Color color, string col1Text, string col2Text, | |
int col1Width = MaxCol1Of2) | |
{ | |
rtb.WriteCol1Of2(color, col1Text, col1Width); | |
rtb.WriteCol2Of2(color, col2Text); | |
} | |
public static void AppendText(this System.Windows.Forms.RichTextBox box, string text, System.Drawing.Color color, | |
params object[] args) | |
{ | |
text = string.Format(text, args); | |
if (color == System.Drawing.Color.Empty) | |
{ | |
box.AppendText(text); | |
return; | |
} | |
box.SelectionStart = box.TextLength; | |
box.SelectionLength = 0; | |
box.SelectionColor = color; | |
box.AppendText(text); | |
box.SelectionColor = box.ForeColor; | |
box.SelectionStart = box.TextLength; | |
box.ScrollToCaret(); | |
} | |
public static void AppendLine(this System.Windows.Forms.RichTextBox box, string text, System.Drawing.Color color, | |
params object[] args) | |
{ | |
box.AppendText("\n" + text, color == System.Drawing.Color.Empty ? box.ForeColor : color, args); | |
} | |
public static void InvokeAppendText(this System.Windows.Forms.RichTextBox box, string text, | |
System.Drawing.Color color, params object[] args) | |
{ | |
box.Invoke((MethodInvoker) delegate { box.AppendText(text, color, args); }); | |
} | |
public static void InvokeAppendLine(this System.Windows.Forms.RichTextBox box, string text, | |
System.Drawing.Color color, params object[] args) | |
{ | |
box.Invoke((MethodInvoker) delegate { box.AppendLine(text, color, args); }); | |
} | |
private static string AlignLeft(this string text, int maxChars, string trimMarker = "...") | |
{ | |
return text.Length > maxChars | |
? text.Truncate(maxChars, trimMarker) | |
: text.PadRight(maxChars); | |
} | |
private static string Truncate(this string value, int maxLength, string truncatedMark = null) | |
{ | |
if (string.IsNullOrEmpty(value)) return value; | |
if (value.Length <= maxLength) return value; | |
if (truncatedMark == null) | |
return value.Substring(0, maxLength); | |
return value.Substring(0, maxLength - truncatedMark.Length) + truncatedMark; | |
} | |
private static string Repeat(this string text, int count) | |
{ | |
return string.Concat(Enumerable.Repeat(text, count)); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
namespace Stylesoft.Common.Extensions | |
{ | |
public static class TimeSpanExtensions | |
{ | |
public static TimeSpan Days(this int days) | |
{ | |
return new TimeSpan(days, 0, 0, 0); | |
} | |
public static TimeSpan Hours(this int hours) | |
{ | |
return new TimeSpan(0, hours, 0, 0); | |
} | |
public static TimeSpan Minutes(this int minutes) | |
{ | |
return new TimeSpan(0, 0, minutes, 0); | |
} | |
public static TimeSpan Seconds(this int seconds) | |
{ | |
return new TimeSpan(0, 0, 0, seconds); | |
} | |
public static TimeSpan Milliseconds(this int milliseconds) | |
{ | |
return new TimeSpan(0, 0, 0, 0, milliseconds); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment