diff --git a/ICD.Common.Utils/Extensions/AssemblyExtensions.cs b/ICD.Common.Utils/Extensions/AssemblyExtensions.cs index fafd5a9..4f87c1c 100644 --- a/ICD.Common.Utils/Extensions/AssemblyExtensions.cs +++ b/ICD.Common.Utils/Extensions/AssemblyExtensions.cs @@ -18,7 +18,7 @@ namespace ICD.Common.Utils.Extensions /// /// [CanBeNull] - public static string GetPath(this Assembly extends) + public static string GetPath([NotNull]this Assembly extends) { if (extends == null) throw new ArgumentNullException("extends"); @@ -29,7 +29,7 @@ namespace ICD.Common.Utils.Extensions #endif .CodeBase; - if (path == null) + if (string.IsNullOrEmpty(path)) { #if STANDARD path = extends.Location; @@ -51,7 +51,7 @@ namespace ICD.Common.Utils.Extensions /// /// [PublicAPI] - public static DateTime GetCreationTime(this Assembly extends) + public static DateTime GetCreationTime([NotNull]this Assembly extends) { if (extends == null) throw new ArgumentNullException("extends"); @@ -66,7 +66,7 @@ namespace ICD.Common.Utils.Extensions /// /// [PublicAPI] - public static string GetInformationalVersion(this Assembly extends) + public static string GetInformationalVersion([NotNull]this Assembly extends) { if (extends == null) throw new ArgumentNullException("extends"); @@ -85,7 +85,7 @@ namespace ICD.Common.Utils.Extensions /// /// [PublicAPI] - public static bool TryGetInformationalVersion(this Assembly extends, out string version) + public static bool TryGetInformationalVersion([NotNull]this Assembly extends, out string version) { if (extends == null) throw new ArgumentNullException("extends"); diff --git a/ICD.Common.Utils/Extensions/CultureInfoExtensions.cs b/ICD.Common.Utils/Extensions/CultureInfoExtensions.cs index 20f9053..3fb4ff0 100644 --- a/ICD.Common.Utils/Extensions/CultureInfoExtensions.cs +++ b/ICD.Common.Utils/Extensions/CultureInfoExtensions.cs @@ -1,5 +1,6 @@ using System; using System.Globalization; +using ICD.Common.Properties; namespace ICD.Common.Utils.Extensions { @@ -10,7 +11,7 @@ namespace ICD.Common.Utils.Extensions /// /// /// - public static bool Uses24HourFormat(this CultureInfo extends) + public static bool Uses24HourFormat([NotNull]this CultureInfo extends) { if (extends == null) throw new ArgumentNullException("extends"); @@ -22,7 +23,7 @@ namespace ICD.Common.Utils.Extensions /// Updates the time patterns for the given culture to use 12 hour time. /// /// - public static void ConvertTo12HourCulture(this CultureInfo extends) + public static void ConvertTo12HourCulture([NotNull]this CultureInfo extends) { if (extends == null) throw new ArgumentNullException("extends"); @@ -41,7 +42,7 @@ namespace ICD.Common.Utils.Extensions /// Updates the time patterns for the given culture to use 24 hour time. /// /// - public static void ConvertTo24HourCulture(this CultureInfo extends) + public static void ConvertTo24HourCulture([NotNull]this CultureInfo extends) { if (extends == null) throw new ArgumentNullException("extends"); diff --git a/ICD.Common.Utils/Extensions/DateTimeExtensions.cs b/ICD.Common.Utils/Extensions/DateTimeExtensions.cs index acdc661..6fde0be 100644 --- a/ICD.Common.Utils/Extensions/DateTimeExtensions.cs +++ b/ICD.Common.Utils/Extensions/DateTimeExtensions.cs @@ -58,7 +58,7 @@ namespace ICD.Common.Utils.Extensions public static DateTime? PreviousLatestTime(this DateTime target, bool inclusive, params DateTime[] times) { if (times == null) - throw new ArgumentNullException("null"); + throw new ArgumentNullException("times"); DateTime latestTime; bool success = times.OrderByDescending(dt => dt).TryFirst(dt => inclusive ? target >= dt : target > dt, out latestTime); diff --git a/ICD.Common.Utils/Extensions/DictionaryExtensions.cs b/ICD.Common.Utils/Extensions/DictionaryExtensions.cs index 58bf7f8..7bcb3df 100644 --- a/ICD.Common.Utils/Extensions/DictionaryExtensions.cs +++ b/ICD.Common.Utils/Extensions/DictionaryExtensions.cs @@ -14,7 +14,8 @@ namespace ICD.Common.Utils.Extensions /// /// /// - public static void RemoveAll(this IDictionary extends, IEnumerable keys) + public static void RemoveAll([NotNull] this IDictionary extends, + [NotNull] IEnumerable keys) { if (extends == null) throw new ArgumentNullException("extends"); @@ -35,7 +36,7 @@ namespace ICD.Common.Utils.Extensions /// /// False if value is not found in the dictionary. [PublicAPI] - public static bool RemoveValue(this IDictionary extends, TValue value) + public static bool RemoveValue([NotNull] this IDictionary extends, TValue value) { if (extends == null) throw new ArgumentNullException("extends"); @@ -52,7 +53,7 @@ namespace ICD.Common.Utils.Extensions /// /// [PublicAPI] - public static void RemoveAllValues(this IDictionary extends, TValue value) + public static void RemoveAllValues([NotNull] this IDictionary extends, TValue value) { if (extends == null) throw new ArgumentNullException("extends"); @@ -71,12 +72,12 @@ namespace ICD.Common.Utils.Extensions /// [CanBeNull] [PublicAPI] - public static TValue GetDefault(this IDictionary extends, TKey key) + public static TValue GetDefault([NotNull] this IDictionary extends, + [NotNull] TKey key) { if (extends == null) throw new ArgumentNullException("extends"); - // ReSharper disable once CompareNonConstrainedGenericWithNull if (key == null) throw new ArgumentNullException("key"); @@ -93,12 +94,13 @@ namespace ICD.Common.Utils.Extensions /// /// [PublicAPI] - public static TValue GetDefault(this IDictionary extends, TKey key, TValue defaultValue) + public static TValue GetDefault([NotNull] this IDictionary extends, + [NotNull] TKey key, + TValue defaultValue) { if (extends == null) throw new ArgumentNullException("extends"); - // ReSharper disable once CompareNonConstrainedGenericWithNull if (key == null) throw new ArgumentNullException("key"); @@ -116,13 +118,13 @@ namespace ICD.Common.Utils.Extensions /// /// [PublicAPI] - public static TValue GetOrAddDefault(this IDictionary extends, TKey key, + public static TValue GetOrAddDefault([NotNull] this IDictionary extends, + [NotNull] TKey key, TValue defaultValue) { if (extends == null) throw new ArgumentNullException("extends"); - // ReSharper disable once CompareNonConstrainedGenericWithNull if (key == null) throw new ArgumentNullException("key"); @@ -141,7 +143,8 @@ namespace ICD.Common.Utils.Extensions /// /// [PublicAPI] - public static TValue GetOrAddNew(this IDictionary extends, TKey key) + public static TValue GetOrAddNew([NotNull] this IDictionary extends, + [NotNull] TKey key) where TValue : new() { if (extends == null) @@ -165,7 +168,9 @@ namespace ICD.Common.Utils.Extensions /// /// [PublicAPI] - public static TValue GetOrAddNew(this IDictionary extends, TKey key, Func valueFunc) + public static TValue GetOrAddNew([NotNull] this IDictionary extends, + [NotNull] TKey key, + [NotNull] Func valueFunc) { if (extends == null) throw new ArgumentNullException("extends"); @@ -175,6 +180,9 @@ namespace ICD.Common.Utils.Extensions // ReSharper restore CompareNonConstrainedGenericWithNull throw new ArgumentNullException("key"); + if (valueFunc == null) + throw new ArgumentNullException("valueFunc"); + TValue value; if (!extends.TryGetValue(key, out value)) { @@ -195,7 +203,7 @@ namespace ICD.Common.Utils.Extensions /// /// The value does not exist in the dictionary. [PublicAPI] - public static TKey GetKey(this IDictionary extends, TValue value) + public static TKey GetKey([NotNull] this IDictionary extends, TValue value) { if (extends == null) throw new ArgumentNullException("extends"); @@ -218,7 +226,8 @@ namespace ICD.Common.Utils.Extensions /// /// [PublicAPI] - public static bool TryGetKey(this IDictionary extends, TValue value, out TKey key) + public static bool TryGetKey([NotNull] this IDictionary extends, TValue value, + out TKey key) { if (extends == null) throw new ArgumentNullException("extends"); @@ -235,7 +244,8 @@ namespace ICD.Common.Utils.Extensions /// /// [PublicAPI] - public static IEnumerable GetKeys(this IDictionary extends, TValue value) + public static IEnumerable GetKeys([NotNull] this IDictionary extends, + TValue value) { if (extends == null) throw new ArgumentNullException("extends"); @@ -253,8 +263,8 @@ namespace ICD.Common.Utils.Extensions /// /// [PublicAPI] - public static bool Update(this IDictionary extends, - IEnumerable> other) + public static bool Update([NotNull] this IDictionary extends, + [NotNull] IEnumerable> other) { if (extends == null) throw new ArgumentNullException("extends"); @@ -275,9 +285,9 @@ namespace ICD.Common.Utils.Extensions /// /// [PublicAPI] - public static bool Update(this IDictionary extends, - IEnumerable> other, - IEqualityComparer comparer) + public static bool Update([NotNull] this IDictionary extends, + [NotNull] IEnumerable> other, + [NotNull] IEqualityComparer comparer) { if (extends == null) throw new ArgumentNullException("extends"); @@ -285,6 +295,9 @@ namespace ICD.Common.Utils.Extensions if (other == null) throw new ArgumentNullException("other"); + if (comparer == null) + throw new ArgumentNullException("comparer"); + bool change = false; foreach (KeyValuePair pair in other) @@ -308,7 +321,8 @@ namespace ICD.Common.Utils.Extensions /// /// [PublicAPI] - public static void AddRange(this IDictionary extends, IEnumerable> items) + public static void AddRange([NotNull] this IDictionary extends, + [NotNull] IEnumerable> items) { if (extends == null) throw new ArgumentNullException("extends"); @@ -329,8 +343,9 @@ namespace ICD.Common.Utils.Extensions /// /// [PublicAPI] - public static void AddRange(this IDictionary extends, IEnumerable items, - Func getKey) + public static void AddRange([NotNull] this IDictionary extends, + [NotNull] IEnumerable items, + [NotNull] Func getKey) { if (extends == null) throw new ArgumentNullException("extends"); @@ -354,8 +369,9 @@ namespace ICD.Common.Utils.Extensions /// /// [PublicAPI] - public static void AddRange(this IDictionary extends, IEnumerable items, - Func getValue) + public static void AddRange([NotNull] this IDictionary extends, + [NotNull] IEnumerable items, + [NotNull] Func getValue) { if (extends == null) throw new ArgumentNullException("extends"); @@ -378,8 +394,8 @@ namespace ICD.Common.Utils.Extensions /// /// [PublicAPI] - public static void AddRange(this Dictionary extends, - IEnumerable> items) + public static void AddRange([NotNull] this Dictionary extends, + [NotNull] IEnumerable> items) { if (extends == null) throw new ArgumentNullException("extends"); @@ -400,12 +416,15 @@ namespace ICD.Common.Utils.Extensions /// /// [PublicAPI] - public static bool DictionaryEqual(this IDictionary extends, - IDictionary other) + public static bool DictionaryEqual([NotNull] this IDictionary extends, + [NotNull] IDictionary other) { if (extends == null) throw new ArgumentNullException("extends"); + if (other == null) + throw new ArgumentNullException("other"); + return extends.DictionaryEqual(other, EqualityComparer.Default); } @@ -419,13 +438,16 @@ namespace ICD.Common.Utils.Extensions /// /// [PublicAPI] - public static bool DictionaryEqual(this IDictionary extends, - IDictionary other, - IEqualityComparer valueComparer) + public static bool DictionaryEqual([NotNull] this IDictionary extends, + [NotNull] IDictionary other, + [NotNull] IEqualityComparer valueComparer) { if (extends == null) throw new ArgumentNullException("extends"); + if (other == null) + throw new ArgumentNullException("other"); + if (valueComparer == null) throw new ArgumentNullException("valueComparer"); @@ -442,20 +464,21 @@ namespace ICD.Common.Utils.Extensions /// /// [PublicAPI] - public static bool DictionaryEqual(this IDictionary extends, - IDictionary other, - Func valueComparer) + public static bool DictionaryEqual([NotNull] this IDictionary extends, + [NotNull] IDictionary other, + [NotNull] Func valueComparer) { if (extends == null) throw new ArgumentNullException("extends"); + if (other == null) + throw new ArgumentNullException("other"); + if (valueComparer == null) throw new ArgumentNullException("valueComparer"); if (extends == other) return true; - if (other == null) - return false; if (extends.Count != other.Count) return false; @@ -467,6 +490,7 @@ namespace ICD.Common.Utils.Extensions if (!valueComparer(kvp.Value, secondValue)) return false; } + return true; } @@ -479,7 +503,7 @@ namespace ICD.Common.Utils.Extensions /// [PublicAPI] public static IEnumerable> OrderByKey( - this IEnumerable> extends) + [NotNull] this IEnumerable> extends) { if (extends == null) throw new ArgumentNullException("extends"); @@ -495,7 +519,8 @@ namespace ICD.Common.Utils.Extensions /// /// [PublicAPI] - public static IEnumerable OrderValuesByKey(this IEnumerable> extends) + public static IEnumerable OrderValuesByKey( + [NotNull] this IEnumerable> extends) { if (extends == null) throw new ArgumentNullException("extends"); @@ -511,7 +536,8 @@ namespace ICD.Common.Utils.Extensions /// /// [PublicAPI] - public static Dictionary> ToInverse(this IEnumerable> extends) + public static Dictionary> ToInverse( + [NotNull] this IEnumerable> extends) { if (extends == null) throw new ArgumentNullException("extends"); @@ -532,5 +558,22 @@ namespace ICD.Common.Utils.Extensions return output; } + + /// + /// Turns an enumerable of KeyValuePairs back into a dictionary + /// + /// + /// + /// + /// + [PublicAPI] + public static Dictionary ToDictionary( + [NotNull] this IEnumerable> extends) + { + if (extends == null) + throw new ArgumentNullException("extends"); + + return extends.ToDictionary(x => x.Key, x => x.Value); + } } } diff --git a/ICD.Common.Utils/Extensions/EnumerableExtensions.cs b/ICD.Common.Utils/Extensions/EnumerableExtensions.cs index ada0f46..1cfb944 100644 --- a/ICD.Common.Utils/Extensions/EnumerableExtensions.cs +++ b/ICD.Common.Utils/Extensions/EnumerableExtensions.cs @@ -17,7 +17,7 @@ namespace ICD.Common.Utils.Extensions /// /// /// - public static T FirstOrDefault(this IEnumerable extends, T defaultItem) + public static T FirstOrDefault([NotNull] this IEnumerable extends, T defaultItem) { if (extends == null) throw new ArgumentNullException("extends"); @@ -34,7 +34,8 @@ namespace ICD.Common.Utils.Extensions /// /// /// - public static T FirstOrDefault(this IEnumerable extends, Func predicate, T defaultItem) + public static T FirstOrDefault([NotNull] this IEnumerable extends, [NotNull] Func predicate, + T defaultItem) { if (extends == null) throw new ArgumentNullException("extends"); @@ -53,7 +54,7 @@ namespace ICD.Common.Utils.Extensions /// /// Outputs the first item in the sequence. /// - public static bool TryFirst(this IEnumerable extends, out T item) + public static bool TryFirst([NotNull] this IEnumerable extends, out T item) { if (extends == null) throw new ArgumentNullException("extends"); @@ -80,7 +81,8 @@ namespace ICD.Common.Utils.Extensions /// /// Outputs the first item in the sequence. /// - public static bool TryFirst(this IEnumerable extends, Func predicate, out T item) + public static bool TryFirst([NotNull] this IEnumerable extends, [NotNull] Func predicate, + out T item) { if (extends == null) throw new ArgumentNullException("extends"); @@ -112,7 +114,7 @@ namespace ICD.Common.Utils.Extensions /// /// Outputs the last item in the sequence. /// - public static bool TryLast(this IEnumerable extends, out T item) + public static bool TryLast([NotNull] this IEnumerable extends, out T item) { if (extends == null) throw new ArgumentNullException("extends"); @@ -139,7 +141,8 @@ namespace ICD.Common.Utils.Extensions /// /// Outputs the last item in the sequence. /// - public static bool TryLast(this IEnumerable extends, Func predicate, out T item) + public static bool TryLast([NotNull] this IEnumerable extends, [NotNull] Func predicate, + out T item) { if (extends == null) throw new ArgumentNullException("extends"); @@ -173,7 +176,7 @@ namespace ICD.Common.Utils.Extensions /// /// /// - public static bool TryElementAt(this IEnumerable extends, int index, out T item) + public static bool TryElementAt([NotNull] this IEnumerable extends, int index, out T item) { if (extends == null) throw new ArgumentNullException("extends"); @@ -200,6 +203,7 @@ namespace ICD.Common.Utils.Extensions item = value; return true; } + current++; } @@ -214,7 +218,7 @@ namespace ICD.Common.Utils.Extensions /// /// /// - public static T ElementAtOrDefault(this IEnumerable extends, int index, T defaultValue) + public static T ElementAtOrDefault([NotNull] this IEnumerable extends, int index, T defaultValue) { if (extends == null) throw new ArgumentNullException("extends"); @@ -231,7 +235,8 @@ namespace ICD.Common.Utils.Extensions /// /// /// - public static bool SequenceEqual(this IEnumerable extends, IEnumerable other, IEqualityComparer comparer) + public static bool SequenceEqual([NotNull] this IEnumerable extends, [NotNull] IEnumerable other, + [NotNull] IEqualityComparer comparer) { if (extends == null) throw new ArgumentNullException("extends"); @@ -253,7 +258,8 @@ namespace ICD.Common.Utils.Extensions /// /// /// - public static bool SequenceEqual(this IEnumerable extends, IEnumerable other, Func comparer) + public static bool SequenceEqual([NotNull] this IEnumerable extends, [NotNull] IEnumerable other, + [NotNull] Func comparer) { if (extends == null) throw new ArgumentNullException("extends"); @@ -298,7 +304,7 @@ namespace ICD.Common.Utils.Extensions /// /// /// - public static bool ScrambledEquals(this IEnumerable extends, IEnumerable other) + public static bool ScrambledEquals([NotNull] this IEnumerable extends, [NotNull] IEnumerable other) { if (extends == null) throw new ArgumentNullException("extends"); @@ -317,7 +323,8 @@ namespace ICD.Common.Utils.Extensions /// /// /// - public static bool ScrambledEquals(this IEnumerable extends, IEnumerable other, IEqualityComparer comparer) + public static bool ScrambledEquals([NotNull] this IEnumerable extends, [NotNull] IEnumerable other, + [NotNull] IEqualityComparer comparer) { if (extends == null) throw new ArgumentNullException("extends"); @@ -352,7 +359,7 @@ namespace ICD.Common.Utils.Extensions /// /// /// - public static int FindIndex(this IEnumerable extends, Predicate match) + public static int FindIndex([NotNull] this IEnumerable extends, [NotNull] Predicate match) { if (extends == null) throw new ArgumentNullException("extends"); @@ -370,7 +377,8 @@ namespace ICD.Common.Utils.Extensions /// /// /// - public static IEnumerable FindIndices(this IEnumerable extends, Predicate match) + public static IEnumerable FindIndices([NotNull] this IEnumerable extends, + [NotNull] Predicate match) { if (extends == null) throw new ArgumentNullException("extends"); @@ -388,7 +396,8 @@ namespace ICD.Common.Utils.Extensions /// /// /// - private static IEnumerable FindIndicesIterator(IEnumerable sequence, Predicate match) + private static IEnumerable FindIndicesIterator([NotNull] IEnumerable sequence, + [NotNull] Predicate match) { int index = 0; @@ -408,7 +417,8 @@ namespace ICD.Common.Utils.Extensions /// /// /// - public static IEnumerable SelectMulti(this IEnumerable extends, + public static IEnumerable SelectMulti([NotNull] this IEnumerable extends, + [NotNull] params Func[] selectors) { if (extends == null) @@ -427,7 +437,7 @@ namespace ICD.Common.Utils.Extensions /// /// [PublicAPI] - public static void Execute(this IEnumerable extends) + public static void Execute([NotNull] this IEnumerable extends) { if (extends == null) throw new ArgumentNullException("extends"); @@ -446,7 +456,7 @@ namespace ICD.Common.Utils.Extensions /// /// [PublicAPI] - public static void ForEach(this IEnumerable extends, Action action) + public static void ForEach([NotNull] this IEnumerable extends, [NotNull] Action action) { if (extends == null) throw new ArgumentNullException("extends"); @@ -464,7 +474,7 @@ namespace ICD.Common.Utils.Extensions /// /// [PublicAPI] - public static void ForEach(this IEnumerable extends, Action action) + public static void ForEach([NotNull] this IEnumerable extends, [NotNull] Action action) { if (extends == null) throw new ArgumentNullException("extends"); @@ -485,7 +495,7 @@ namespace ICD.Common.Utils.Extensions /// /// /// - public static IEnumerable Prepend(this IEnumerable extends, T item) + public static IEnumerable Prepend([NotNull]this IEnumerable extends, T item) { if (extends == null) throw new ArgumentNullException("extends"); @@ -500,7 +510,7 @@ namespace ICD.Common.Utils.Extensions /// /// /// - private static IEnumerable PrependIterator(IEnumerable sequence, T item) + private static IEnumerable PrependIterator([NotNull]IEnumerable sequence, T item) { yield return item; @@ -517,7 +527,7 @@ namespace ICD.Common.Utils.Extensions /// /// [PublicAPI] - public static IEnumerable PrependMany(this IEnumerable extends, params T[] items) + public static IEnumerable PrependMany([NotNull] this IEnumerable extends, [NotNull] params T[] items) { if (extends == null) throw new ArgumentNullException("extends"); @@ -535,7 +545,8 @@ namespace ICD.Common.Utils.Extensions /// /// /// - private static IEnumerable PrependManyIterator(IEnumerable sequence, params T[] items) + private static IEnumerable PrependManyIterator([NotNull] IEnumerable sequence, + [NotNull] params T[] items) { foreach (T item in items) yield return item; @@ -552,7 +563,7 @@ namespace ICD.Common.Utils.Extensions /// /// /// - public static IEnumerable Append(this IEnumerable extends, T item) + public static IEnumerable Append([NotNull]this IEnumerable extends, T item) { if (extends == null) throw new ArgumentNullException("extends"); @@ -567,7 +578,7 @@ namespace ICD.Common.Utils.Extensions /// /// /// - private static IEnumerable AppendIterator(IEnumerable sequence, T item) + private static IEnumerable AppendIterator([NotNull]IEnumerable sequence, T item) { foreach (T first in sequence) yield return first; @@ -584,7 +595,7 @@ namespace ICD.Common.Utils.Extensions /// /// [PublicAPI] - public static IEnumerable AppendMany(this IEnumerable extends, params T[] items) + public static IEnumerable AppendMany([NotNull] this IEnumerable extends, [NotNull] params T[] items) { if (extends == null) throw new ArgumentNullException("extends"); @@ -602,7 +613,8 @@ namespace ICD.Common.Utils.Extensions /// /// /// - private static IEnumerable AppendManyIterator(IEnumerable sequence, params T[] items) + private static IEnumerable AppendManyIterator([NotNull] IEnumerable sequence, + [NotNull] params T[] items) { foreach (T each in sequence) yield return each; @@ -619,7 +631,7 @@ namespace ICD.Common.Utils.Extensions /// /// [PublicAPI] - public static IEnumerable PadRight(this IEnumerable extends, int count) + public static IEnumerable PadRight([NotNull] this IEnumerable extends, int count) { if (extends == null) throw new ArgumentNullException("extends"); @@ -634,7 +646,7 @@ namespace ICD.Common.Utils.Extensions /// /// /// - private static IEnumerable PadRightIterator(IEnumerable sequence, int count) + private static IEnumerable PadRightIterator([NotNull] IEnumerable sequence, int count) { int index = 0; @@ -654,7 +666,7 @@ namespace ICD.Common.Utils.Extensions /// /// /// - public static bool AreOrdered(this IEnumerable extends) + public static bool AreOrdered([NotNull] this IEnumerable extends) { if (extends == null) throw new ArgumentNullException("extends"); @@ -669,7 +681,7 @@ namespace ICD.Common.Utils.Extensions /// /// /// - public static bool AreOrdered(this IEnumerable extends, IComparer comparer) + public static bool AreOrdered([NotNull] this IEnumerable extends, [NotNull] IComparer comparer) { if (extends == null) throw new ArgumentNullException("extends"); @@ -698,7 +710,7 @@ namespace ICD.Common.Utils.Extensions /// /// /// - public static IOrderedEnumerable Order(this IEnumerable extends) + public static IOrderedEnumerable Order([NotNull] this IEnumerable extends) { if (extends == null) throw new ArgumentNullException("extends"); @@ -713,7 +725,8 @@ namespace ICD.Common.Utils.Extensions /// /// /// - public static IOrderedEnumerable Order(this IEnumerable extends, IComparer comparer) + public static IOrderedEnumerable Order([NotNull] this IEnumerable extends, + [NotNull] IComparer comparer) { if (extends == null) throw new ArgumentNullException("extends"); @@ -731,7 +744,8 @@ namespace ICD.Common.Utils.Extensions /// /// /// - public static IOrderedEnumerable OrderDescending(this IEnumerable extends, IComparer comparer) + public static IOrderedEnumerable OrderDescending([NotNull] this IEnumerable extends, + [NotNull] IComparer comparer) { if (extends == null) throw new ArgumentNullException("extends"); @@ -749,7 +763,7 @@ namespace ICD.Common.Utils.Extensions /// /// /// - public static IEnumerable Except(this IEnumerable extends, T item) + public static IEnumerable Except([NotNull] this IEnumerable extends, T item) { if (extends == null) throw new ArgumentNullException("extends"); @@ -765,7 +779,8 @@ namespace ICD.Common.Utils.Extensions /// /// /// - public static IEnumerable Except(this IEnumerable extends, T item, IEqualityComparer comparer) + public static IEnumerable Except([NotNull] this IEnumerable extends, T item, + [NotNull] IEqualityComparer comparer) { if (extends == null) throw new ArgumentNullException("extends"); @@ -783,7 +798,7 @@ namespace ICD.Common.Utils.Extensions /// /// [PublicAPI] - public static IEnumerable ExceptNulls(this IEnumerable extends) + public static IEnumerable ExceptNulls([NotNull] this IEnumerable extends) where T : struct { if (extends == null) @@ -800,7 +815,7 @@ namespace ICD.Common.Utils.Extensions /// /// /// - public static void CopyTo(this IEnumerable extends, T[] array, int index) + public static void CopyTo([NotNull] this IEnumerable extends, [NotNull] T[] array, int index) { if (extends == null) throw new ArgumentNullException("extends"); @@ -829,7 +844,7 @@ namespace ICD.Common.Utils.Extensions /// /// /// - public static IcdHashSet ToIcdHashSet(this IEnumerable extends) + public static IcdHashSet ToIcdHashSet([NotNull] this IEnumerable extends) { if (extends == null) throw new ArgumentNullException("extends"); @@ -844,7 +859,8 @@ namespace ICD.Common.Utils.Extensions /// /// /// - public static IcdHashSet ToIcdHashSet(this IEnumerable extends, IEqualityComparer comparer) + public static IcdHashSet ToIcdHashSet([NotNull] this IEnumerable extends, + [NotNull] IEqualityComparer comparer) { if (extends == null) throw new ArgumentNullException("extends"); @@ -862,7 +878,7 @@ namespace ICD.Common.Utils.Extensions /// /// /// - public static T[] ToArray(this IEnumerable extends, int count) + public static T[] ToArray([NotNull] this IEnumerable extends, int count) { if (extends == null) throw new ArgumentNullException("extends"); @@ -903,7 +919,7 @@ namespace ICD.Common.Utils.Extensions /// /// /// - public static List ToList(this IEnumerable extends, int count) + public static List ToList([NotNull] this IEnumerable extends, int count) { if (extends == null) throw new ArgumentNullException("extends"); @@ -923,7 +939,7 @@ namespace ICD.Common.Utils.Extensions /// /// [PublicAPI] - public static Dictionary ToIndexedDictionary(this IEnumerable extends) + public static Dictionary ToIndexedDictionary([NotNull] this IEnumerable extends) { if (extends == null) throw new ArgumentNullException("extends"); @@ -940,7 +956,7 @@ namespace ICD.Common.Utils.Extensions /// /// [PublicAPI] - public static Dictionary ToIndexedDictionaryUInt(this IEnumerable extends) + public static Dictionary ToIndexedDictionaryUInt([NotNull] this IEnumerable extends) { if (extends == null) throw new ArgumentNullException("extends"); @@ -950,22 +966,6 @@ namespace ICD.Common.Utils.Extensions return output; } - /// - /// Turns an enumerable of KeyValuePairs back into a dictionary - /// - /// - /// - /// - /// - [PublicAPI] - public static Dictionary ToDictionary(this IEnumerable> extends) - { - if (extends == null) - throw new ArgumentNullException("extends"); - - return extends.ToDictionary(x => x.Key, x => x.Value); - } - /// /// Gets distinct elements from the sequence based on given property. /// @@ -975,8 +975,8 @@ namespace ICD.Common.Utils.Extensions /// /// [PublicAPI] - public static IEnumerable Distinct(this IEnumerable extends, - Func getProperty) + public static IEnumerable Distinct([NotNull] this IEnumerable extends, + [NotNull] Func getProperty) { if (extends == null) throw new ArgumentNullException("extends"); @@ -998,8 +998,9 @@ namespace ICD.Common.Utils.Extensions /// /// [PublicAPI] - public static IEnumerable Distinct(this IEnumerable extends, + public static IEnumerable Distinct([NotNull] this IEnumerable extends, Func getProperty, + [NotNull] IEqualityComparer propertyComparer) { if (extends == null) @@ -1020,7 +1021,7 @@ namespace ICD.Common.Utils.Extensions /// /// /// - public static T Random(this IEnumerable extends) + public static T Random([NotNull] this IEnumerable extends) { if (extends == null) throw new ArgumentNullException("extends"); @@ -1045,7 +1046,7 @@ namespace ICD.Common.Utils.Extensions /// /// [PublicAPI] - public static T Unanimous(this IEnumerable extends, T other) + public static T Unanimous([NotNull] this IEnumerable extends, T other) { if (extends == null) throw new ArgumentNullException("extends"); @@ -1063,7 +1064,7 @@ namespace ICD.Common.Utils.Extensions /// /// [PublicAPI] - public static bool Unanimous(this IEnumerable extends, out T result) + public static bool Unanimous([NotNull] this IEnumerable extends, out T result) { if (extends == null) throw new ArgumentNullException("extends"); @@ -1081,7 +1082,8 @@ namespace ICD.Common.Utils.Extensions /// /// [PublicAPI] - public static bool Unanimous(this IEnumerable extends, IEqualityComparer comparer, out T result) + public static bool Unanimous([NotNull] this IEnumerable extends, [NotNull] IEqualityComparer comparer, + out T result) { if (extends == null) throw new ArgumentNullException("extends"); @@ -1120,7 +1122,7 @@ namespace ICD.Common.Utils.Extensions /// /// [PublicAPI] - public static IEnumerable> Partition(this IEnumerable extends, int partitionSize) + public static IEnumerable> Partition([NotNull] this IEnumerable extends, int partitionSize) { if (extends == null) throw new ArgumentNullException("extends"); @@ -1134,7 +1136,8 @@ namespace ICD.Common.Utils.Extensions /// /// /// - private static IEnumerable> PartitionIterator(IEnumerable sequence, int partitionSize) + private static IEnumerable> PartitionIterator([NotNull] IEnumerable sequence, + int partitionSize) { using (IEnumerator enumerator = sequence.GetEnumerator()) { @@ -1143,7 +1146,7 @@ namespace ICD.Common.Utils.Extensions } } - private static IEnumerable YieldBatchElements(IEnumerator source, int partitionSize) + private static IEnumerable YieldBatchElements([NotNull] IEnumerator source, int partitionSize) { if (source == null) throw new ArgumentNullException("source"); @@ -1164,7 +1167,7 @@ namespace ICD.Common.Utils.Extensions /// Type of the object. /// The instance that will be wrapped. /// An IEnumerable<T> consisting of a single item. - public static IEnumerable Yield(this T item) + public static IEnumerable Yield([CanBeNull] this T item) { yield return item; } @@ -1175,7 +1178,7 @@ namespace ICD.Common.Utils.Extensions /// /// /// - public static IEnumerable GetAdjacentPairs(this IEnumerable extends) + public static IEnumerable GetAdjacentPairs([NotNull] this IEnumerable extends) { if (extends == null) throw new ArgumentNullException("extends"); @@ -1189,7 +1192,7 @@ namespace ICD.Common.Utils.Extensions /// /// /// - private static IEnumerable GetAdjacentPairsIterator(IEnumerable extends) + private static IEnumerable GetAdjacentPairsIterator([NotNull] IEnumerable extends) { T previous = default(T); bool first = true; @@ -1197,7 +1200,7 @@ namespace ICD.Common.Utils.Extensions foreach (T item in extends) { if (!first) - yield return new[] { previous, item }; + yield return new[] {previous, item}; first = false; previous = item; @@ -1211,7 +1214,7 @@ namespace ICD.Common.Utils.Extensions /// /// /// - public static T GetClosest(this IEnumerable extends, Func getDelta) + public static T GetClosest([NotNull] this IEnumerable extends, [NotNull] Func getDelta) { return extends.MinBy(n => Math.Abs(getDelta(n))); } @@ -1234,8 +1237,8 @@ namespace ICD.Common.Utils.Extensions /// or is null /// is empty [PublicAPI] - public static TSource MinBy(this IEnumerable source, - Func selector) + public static TSource MinBy([NotNull] this IEnumerable source, + [NotNull] Func selector) { if (source == null) throw new ArgumentNullException("source"); @@ -1265,8 +1268,9 @@ namespace ICD.Common.Utils.Extensions /// or is null /// is empty [PublicAPI] - public static TSource MinBy(this IEnumerable source, Func selector, - IComparer comparer) + public static TSource MinBy([NotNull] this IEnumerable source, + [NotNull] Func selector, + [NotNull] IComparer comparer) { if (source == null) throw new ArgumentNullException("source"); @@ -1307,7 +1311,7 @@ namespace ICD.Common.Utils.Extensions /// /// [PublicAPI] - public static byte Sum(this IEnumerable extends) + public static byte Sum([NotNull] this IEnumerable extends) { if (extends == null) throw new ArgumentNullException("extends"); @@ -1326,7 +1330,7 @@ namespace ICD.Common.Utils.Extensions /// /// [PublicAPI] - public static IEnumerable Consolidate(this IEnumerable extends) + public static IEnumerable Consolidate([NotNull] this IEnumerable extends) { if (extends == null) throw new ArgumentNullException("extends"); @@ -1346,7 +1350,8 @@ namespace ICD.Common.Utils.Extensions /// /// [PublicAPI] - public static IEnumerable Consolidate(this IEnumerable extends, IComparer comparer) + public static IEnumerable Consolidate([NotNull] this IEnumerable extends, + [NotNull] IComparer comparer) { if (extends == null) throw new ArgumentNullException("extends"); @@ -1368,7 +1373,8 @@ namespace ICD.Common.Utils.Extensions /// /// /// - private static IEnumerable ConsolidateIterator(IEnumerable sequence, IComparer comparer) + private static IEnumerable ConsolidateIterator([NotNull] IEnumerable sequence, + [NotNull] IComparer comparer) { bool first = true; T last = default(T); @@ -1394,7 +1400,7 @@ namespace ICD.Common.Utils.Extensions /// /// [PublicAPI] - public static bool AnyAndAll(this IEnumerable extends, Func predicate) + public static bool AnyAndAll([NotNull] this IEnumerable extends, [NotNull] Func predicate) { if (extends == null) throw new ArgumentNullException("extends"); @@ -1420,11 +1426,12 @@ namespace ICD.Common.Utils.Extensions /// /// /// - public static IEnumerable> Zip(this IEnumerable extends, - IEnumerable second) + public static IEnumerable> Zip( + [NotNull] this IEnumerable extends, + [NotNull] IEnumerable second) { if (extends == null) - throw new ArgumentNullException("first"); + throw new ArgumentNullException("extends"); if (second == null) throw new ArgumentNullException("second"); @@ -1441,9 +1448,9 @@ namespace ICD.Common.Utils.Extensions /// /// /// - public static void Zip(this IEnumerable extends, - IEnumerable second, - Action callback) + public static void Zip([NotNull]this IEnumerable extends, + [NotNull]IEnumerable second, + [NotNull]Action callback) { if (extends == null) throw new ArgumentNullException("first"); @@ -1474,9 +1481,9 @@ namespace ICD.Common.Utils.Extensions /// /// /// - public static IEnumerable Zip(this IEnumerable extends, - IEnumerable other, - Func callback) + public static IEnumerable Zip([NotNull]this IEnumerable extends, + [NotNull]IEnumerable other, + [NotNull]Func callback) { if (extends == null) throw new ArgumentNullException("extends"); @@ -1500,9 +1507,9 @@ namespace ICD.Common.Utils.Extensions /// /// /// - private static IEnumerable ZipIterator(IEnumerable first, - IEnumerable second, - Func callback) + private static IEnumerable ZipIterator([NotNull]IEnumerable first, + [NotNull]IEnumerable second, + [NotNull]Func callback) { using (IEnumerator enumerator1 = first.GetEnumerator()) { diff --git a/ICD.Common.Utils/Extensions/EventHandlerExtensions.cs b/ICD.Common.Utils/Extensions/EventHandlerExtensions.cs index 824a886..fcc909c 100644 --- a/ICD.Common.Utils/Extensions/EventHandlerExtensions.cs +++ b/ICD.Common.Utils/Extensions/EventHandlerExtensions.cs @@ -1,9 +1,5 @@ using System; -#if SIMPLSHARP -using Crestron.SimplSharp.Reflection; -#else -using System.Reflection; -#endif +using ICD.Common.Properties; namespace ICD.Common.Utils.Extensions { @@ -17,7 +13,7 @@ namespace ICD.Common.Utils.Extensions /// /// /// - public static void Raise(this EventHandler extends, object sender) + public static void Raise([CanBeNull] this EventHandler extends, object sender) { if (extends != null) extends(sender, EventArgs.Empty); @@ -30,7 +26,7 @@ namespace ICD.Common.Utils.Extensions /// /// /// - public static void Raise(this EventHandler extends, object sender, T args) + public static void Raise([CanBeNull]this EventHandler extends, object sender, [NotNull]T args) where T : EventArgs { if (args == null) @@ -39,22 +35,5 @@ namespace ICD.Common.Utils.Extensions if (extends != null) extends(sender, args); } - - /// - /// Cross-platform shim for getting MethodInfo for the delegate. - /// - /// - /// - public static MethodInfo GetMethodInfo(this Delegate extends) - { - if (extends == null) - throw new ArgumentNullException("extends"); - -#if SIMPLSHARP - return extends.GetMethod(); -#else - return extends.Method; -#endif - } } } diff --git a/ICD.Common.Utils/Extensions/JsonReaderExtensions.cs b/ICD.Common.Utils/Extensions/JsonReaderExtensions.cs index 263e616..a824008 100644 --- a/ICD.Common.Utils/Extensions/JsonReaderExtensions.cs +++ b/ICD.Common.Utils/Extensions/JsonReaderExtensions.cs @@ -15,7 +15,7 @@ namespace ICD.Common.Utils.Extensions /// /// /// - public static T ReadAsObject(this JsonReader extends) + public static T ReadAsObject([NotNull] this JsonReader extends) { if (extends == null) throw new ArgumentNullException("extends"); @@ -36,7 +36,7 @@ namespace ICD.Common.Utils.Extensions /// /// /// - public static T ReadAsObject(this JsonReader extends, JsonSerializer serializer) + public static T ReadAsObject([NotNull] this JsonReader extends, [NotNull] JsonSerializer serializer) { if (extends == null) throw new ArgumentNullException("extends"); @@ -53,8 +53,8 @@ namespace ICD.Common.Utils.Extensions /// /// /// - public static void ReadObject(this JsonReader extends, JsonSerializer serializer, - Action readPropertyValue) + public static void ReadObject([NotNull] this JsonReader extends, [NotNull] JsonSerializer serializer, + [NotNull] Action readPropertyValue) { if (extends == null) throw new ArgumentNullException("extends"); @@ -69,7 +69,8 @@ namespace ICD.Common.Utils.Extensions return; if (extends.TokenType != JsonToken.StartObject) - throw new FormatException(string.Format("Expected {0} got {1}", JsonToken.StartObject, extends.TokenType)); + throw new FormatException(string.Format("Expected {0} got {1}", JsonToken.StartObject, + extends.TokenType)); while (extends.Read()) { @@ -94,7 +95,7 @@ namespace ICD.Common.Utils.Extensions /// /// [PublicAPI] - public static Type GetValueAsType(this JsonReader extends) + public static Type GetValueAsType([NotNull] this JsonReader extends) { if (extends == null) throw new ArgumentNullException("extends"); @@ -109,7 +110,7 @@ namespace ICD.Common.Utils.Extensions /// /// [PublicAPI] - public static uint GetValueAsUInt(this JsonReader extends) + public static uint GetValueAsUInt([NotNull] this JsonReader extends) { if (extends == null) throw new ArgumentNullException("extends"); @@ -117,7 +118,8 @@ namespace ICD.Common.Utils.Extensions if (extends.TokenType == JsonToken.Integer) return (uint)(long)extends.Value; - string message = string.Format("Token {0} {1} is not {2}", extends.TokenType, extends.Value, JsonToken.Integer); + string message = string.Format("Token {0} {1} is not {2}", extends.TokenType, extends.Value, + JsonToken.Integer); throw new InvalidCastException(message); } @@ -127,7 +129,7 @@ namespace ICD.Common.Utils.Extensions /// /// [PublicAPI] - public static int GetValueAsInt(this JsonReader extends) + public static int GetValueAsInt([NotNull] this JsonReader extends) { if (extends == null) throw new ArgumentNullException("extends"); @@ -135,7 +137,8 @@ namespace ICD.Common.Utils.Extensions if (extends.TokenType == JsonToken.Integer) return (int)(long)extends.Value; - string message = string.Format("Token {0} {1} is not {2}", extends.TokenType, extends.Value, JsonToken.Integer); + string message = string.Format("Token {0} {1} is not {2}", extends.TokenType, extends.Value, + JsonToken.Integer); throw new InvalidCastException(message); } @@ -145,7 +148,7 @@ namespace ICD.Common.Utils.Extensions /// /// [PublicAPI] - public static string GetValueAsString(this JsonReader extends) + public static string GetValueAsString([NotNull] this JsonReader extends) { if (extends == null) throw new ArgumentNullException("extends"); @@ -168,7 +171,7 @@ namespace ICD.Common.Utils.Extensions /// /// [PublicAPI] - public static bool GetValueAsBool(this JsonReader extends) + public static bool GetValueAsBool([NotNull] this JsonReader extends) { if (extends == null) throw new ArgumentNullException("extends"); @@ -176,7 +179,8 @@ namespace ICD.Common.Utils.Extensions if (extends.TokenType == JsonToken.Boolean) return (bool)extends.Value; - string message = string.Format("Token {0} {1} is not {2}", extends.TokenType, extends.Value, JsonToken.Boolean); + string message = string.Format("Token {0} {1} is not {2}", extends.TokenType, extends.Value, + JsonToken.Boolean); throw new InvalidCastException(message); } @@ -186,7 +190,7 @@ namespace ICD.Common.Utils.Extensions /// /// [PublicAPI] - public static T GetValueAsEnum(this JsonReader extends) + public static T GetValueAsEnum([NotNull] this JsonReader extends) where T : struct, IConvertible { if (extends == null) @@ -204,7 +208,7 @@ namespace ICD.Common.Utils.Extensions /// /// [PublicAPI] - public static Guid GetValueAsGuid(this JsonReader extends) + public static Guid GetValueAsGuid([NotNull] this JsonReader extends) { if (extends == null) throw new ArgumentNullException("extends"); @@ -218,7 +222,7 @@ namespace ICD.Common.Utils.Extensions /// /// /// - public static DateTime GetValueAsDateTime(this JsonReader extends) + public static DateTime GetValueAsDateTime([NotNull] this JsonReader extends) { if (extends == null) throw new ArgumentNullException("extends"); @@ -240,11 +244,15 @@ namespace ICD.Common.Utils.Extensions /// /// /// - public static DateTime GetValueAsDateTimeExact(this JsonReader extends, string format, IFormatProvider provider) + public static DateTime GetValueAsDateTimeExact([NotNull] this JsonReader extends, [NotNull] string format, + IFormatProvider provider) { if (extends == null) throw new ArgumentNullException("extends"); + if (format == null) + throw new ArgumentNullException("format"); + #if !SIMPLSHARP // Newer NewtonSoft tries to be helpful by assuming that anything that looks like a DateTime must be a date. if (extends.DateParseHandling != DateParseHandling.None) diff --git a/ICD.Common.Utils/Extensions/JsonSerializerExtensions.cs b/ICD.Common.Utils/Extensions/JsonSerializerExtensions.cs index 5abd7f9..7fea6ab 100644 --- a/ICD.Common.Utils/Extensions/JsonSerializerExtensions.cs +++ b/ICD.Common.Utils/Extensions/JsonSerializerExtensions.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.Linq; +using ICD.Common.Properties; using Newtonsoft.Json; namespace ICD.Common.Utils.Extensions @@ -16,8 +17,16 @@ namespace ICD.Common.Utils.Extensions /// /// /// - public static IEnumerable DeserializeArray(this JsonSerializer extends, JsonReader reader) + [CanBeNull] + public static IEnumerable DeserializeArray([NotNull] this JsonSerializer extends, + [NotNull] JsonReader reader) { + if (extends == null) + throw new ArgumentNullException("extends"); + + if (reader == null) + throw new ArgumentNullException("reader"); + return extends.DeserializeArray(reader, (s, r) => extends.Deserialize(reader)); } @@ -28,8 +37,10 @@ namespace ICD.Common.Utils.Extensions /// /// /// - public static IEnumerable DeserializeArray(this JsonSerializer extends, JsonReader reader, - Func read) + [CanBeNull] + public static IEnumerable DeserializeArray([NotNull] this JsonSerializer extends, + [NotNull] JsonReader reader, + [NotNull] Func read) { if (extends == null) throw new ArgumentNullException("extends"); @@ -41,10 +52,11 @@ namespace ICD.Common.Utils.Extensions throw new ArgumentNullException("read"); if (reader.TokenType == JsonToken.Null) - return Enumerable.Empty(); + return null; if (reader.TokenType != JsonToken.StartArray) - throw new FormatException(string.Format("Expected token {0} got {1}", JsonToken.StartArray, reader.TokenType)); + throw new FormatException(string.Format("Expected token {0} got {1}", JsonToken.StartArray, + reader.TokenType)); // ToArray to ensure everything gets read before moving onto the next token return DeserializeArrayIterator(extends, reader, read).ToArray(); @@ -57,8 +69,10 @@ namespace ICD.Common.Utils.Extensions /// /// /// - private static IEnumerable DeserializeArrayIterator(JsonSerializer serializer, JsonReader reader, - Func read) + [NotNull] + private static IEnumerable DeserializeArrayIterator( + [NotNull] JsonSerializer serializer, [NotNull] JsonReader reader, + [NotNull] Func read) { // Step into the first value reader.Read(); @@ -80,8 +94,10 @@ namespace ICD.Common.Utils.Extensions /// /// /// - public static IEnumerable> DeserializeDict(this JsonSerializer extends, - JsonReader reader) + [CanBeNull] + public static IEnumerable> DeserializeDict( + [NotNull] this JsonSerializer extends, + [NotNull] JsonReader reader) { if (extends == null) throw new ArgumentNullException("extends"); @@ -103,12 +119,12 @@ namespace ICD.Common.Utils.Extensions /// /// /// - public static IEnumerable> DeserializeDict(this JsonSerializer extends, - JsonReader reader, - Func readKey, - Func readValue) + [CanBeNull] + public static IEnumerable> DeserializeDict( + [NotNull] this JsonSerializer extends, + [NotNull] JsonReader reader, + [NotNull] Func readKey, + [NotNull] Func readValue) { if (extends == null) throw new ArgumentNullException("extends"); @@ -125,11 +141,11 @@ namespace ICD.Common.Utils.Extensions return extends.DeserializeArray(reader, (s, r) => s.DeserializeKeyValuePair(r, readKey, readValue)); } - public static KeyValuePair DeserializeKeyValuePair(this JsonSerializer extends, JsonReader reader, - Func readKey, - Func readValue) + public static KeyValuePair DeserializeKeyValuePair( + [NotNull] this JsonSerializer extends, + [NotNull] JsonReader reader, + [NotNull] Func readKey, + [NotNull] Func readValue) { if (extends == null) throw new ArgumentNullException("extends"); @@ -144,7 +160,8 @@ namespace ICD.Common.Utils.Extensions throw new ArgumentNullException("readValue"); if (reader.TokenType != JsonToken.StartObject) - throw new FormatException(string.Format("Expected token {0} got {1}", JsonToken.StartObject, reader.TokenType)); + throw new FormatException(string.Format("Expected token {0} got {1}", JsonToken.StartObject, + reader.TokenType)); TKey key = default(TKey); TValue value = default(TValue); @@ -155,7 +172,8 @@ namespace ICD.Common.Utils.Extensions while (reader.TokenType != JsonToken.EndObject) { if (reader.TokenType != JsonToken.PropertyName) - throw new FormatException(string.Format("Expected token {0} got {1}", JsonToken.PropertyName, reader.TokenType)); + throw new FormatException(string.Format("Expected token {0} got {1}", JsonToken.PropertyName, + reader.TokenType)); string propertyName = (string)reader.Value; @@ -190,8 +208,15 @@ namespace ICD.Common.Utils.Extensions /// /// /// - public static void SerializeArray(this JsonSerializer extends, JsonWriter writer, IEnumerable items) + public static void SerializeArray([NotNull] this JsonSerializer extends, [NotNull] JsonWriter writer, + [CanBeNull] IEnumerable items) { + if (extends == null) + throw new ArgumentNullException("extends"); + + if (writer == null) + throw new ArgumentNullException("writer"); + extends.SerializeArray(writer, items, (s, w, item) => s.Serialize(w, item)); } @@ -203,8 +228,9 @@ namespace ICD.Common.Utils.Extensions /// /// /// - public static void SerializeArray(this JsonSerializer extends, JsonWriter writer, IEnumerable items, - Action write) + public static void SerializeArray([NotNull] this JsonSerializer extends, [NotNull] JsonWriter writer, + [CanBeNull] IEnumerable items, + [NotNull] Action write) { if (extends == null) throw new ArgumentNullException("extends"); @@ -237,8 +263,9 @@ namespace ICD.Common.Utils.Extensions /// /// /// - public static void SerializeDict(this JsonSerializer extends, JsonWriter writer, - IEnumerable> items) + public static void SerializeDict([NotNull] this JsonSerializer extends, + [NotNull] JsonWriter writer, + [CanBeNull] IEnumerable> items) { extends.SerializeDict(writer, items, (s, w, k) => s.Serialize(w, k), @@ -255,10 +282,11 @@ namespace ICD.Common.Utils.Extensions /// /// /// - public static void SerializeDict(this JsonSerializer extends, JsonWriter writer, - IEnumerable> items, - Action writeKey, - Action writeValue) + public static void SerializeDict([NotNull] this JsonSerializer extends, + [NotNull] JsonWriter writer, + [CanBeNull] IEnumerable> items, + [NotNull] Action writeKey, + [NotNull] Action writeValue) { if (extends == null) throw new ArgumentNullException("extends"); @@ -275,10 +303,11 @@ namespace ICD.Common.Utils.Extensions extends.SerializeArray(writer, items, (s, w, kvp) => s.SerializeKeyValuePair(w, kvp, writeKey, writeValue)); } - public static void SerializeKeyValuePair(this JsonSerializer extends, JsonWriter writer, - KeyValuePair kvp, - Action writeKey, - Action writeValue) + public static void SerializeKeyValuePair( + [NotNull] this JsonSerializer extends, + [NotNull] JsonWriter writer, KeyValuePair kvp, + [NotNull] Action writeKey, + [NotNull] Action writeValue) { if (extends == null) throw new ArgumentNullException("extends"); @@ -303,4 +332,4 @@ namespace ICD.Common.Utils.Extensions writer.WriteEndObject(); } } -} \ No newline at end of file +} diff --git a/ICD.Common.Utils/Extensions/JsonWriterExtensions.cs b/ICD.Common.Utils/Extensions/JsonWriterExtensions.cs index ede230a..8b5346a 100644 --- a/ICD.Common.Utils/Extensions/JsonWriterExtensions.cs +++ b/ICD.Common.Utils/Extensions/JsonWriterExtensions.cs @@ -12,7 +12,7 @@ namespace ICD.Common.Utils.Extensions /// /// [PublicAPI] - public static void WriteType(this JsonWriter extends, Type type) + public static void WriteType([NotNull]this JsonWriter extends, [CanBeNull]Type type) { if (extends == null) throw new ArgumentNullException("extends"); @@ -33,7 +33,7 @@ namespace ICD.Common.Utils.Extensions /// /// /// - public static void WriteProperty(this JsonWriter extends, string propertyName, object value) + public static void WriteProperty([NotNull]this JsonWriter extends, string propertyName, object value) { if (extends == null) throw new ArgumentNullException("extends"); @@ -48,7 +48,7 @@ namespace ICD.Common.Utils.Extensions /// /// /// - public static void WriteProperty(this JsonWriter extends, string propertyName, string value) + public static void WriteProperty([NotNull]this JsonWriter extends, string propertyName, string value) { if (extends == null) throw new ArgumentNullException("extends"); @@ -63,7 +63,7 @@ namespace ICD.Common.Utils.Extensions /// /// /// - public static void WriteProperty(this JsonWriter extends, string propertyName, DateTime value) + public static void WriteProperty([NotNull]this JsonWriter extends, string propertyName, DateTime value) { if (extends == null) throw new ArgumentNullException("extends"); @@ -78,7 +78,7 @@ namespace ICD.Common.Utils.Extensions /// /// /// - public static void WriteProperty(this JsonWriter extends, string propertyName, bool value) + public static void WriteProperty([NotNull]this JsonWriter extends, string propertyName, bool value) { if (extends == null) throw new ArgumentNullException("extends"); @@ -93,7 +93,7 @@ namespace ICD.Common.Utils.Extensions /// /// /// - public static void WriteProperty(this JsonWriter extends, string propertyName, int value) + public static void WriteProperty([NotNull]this JsonWriter extends, string propertyName, int value) { if (extends == null) throw new ArgumentNullException("extends"); @@ -108,7 +108,7 @@ namespace ICD.Common.Utils.Extensions /// /// /// - public static void WriteProperty(this JsonWriter extends, string propertyName, Guid value) + public static void WriteProperty([NotNull]this JsonWriter extends, string propertyName, Guid value) { if (extends == null) throw new ArgumentNullException("extends"); @@ -123,7 +123,7 @@ namespace ICD.Common.Utils.Extensions /// /// /// - public static void WriteProperty(this JsonWriter extends, string propertyName, Type value) + public static void WriteProperty([NotNull]this JsonWriter extends, string propertyName, Type value) { if (extends == null) throw new ArgumentNullException("extends"); diff --git a/ICD.Common.Utils/Extensions/ListExtensions.cs b/ICD.Common.Utils/Extensions/ListExtensions.cs index e66bc17..e03b053 100644 --- a/ICD.Common.Utils/Extensions/ListExtensions.cs +++ b/ICD.Common.Utils/Extensions/ListExtensions.cs @@ -21,7 +21,7 @@ namespace ICD.Common.Utils.Extensions /// /// [PublicAPI] - public static bool AddSorted(this IList extends, T item) + public static bool AddSorted([NotNull] this IList extends, T item) { if (extends == null) throw new ArgumentNullException("extends"); @@ -39,7 +39,8 @@ namespace ICD.Common.Utils.Extensions /// /// [PublicAPI] - public static bool AddSorted(this IList extends, T item, Func predicate) + public static bool AddSorted([NotNull] this IList extends, T item, + [NotNull] Func predicate) { if (extends == null) throw new ArgumentNullException("extends"); @@ -61,7 +62,7 @@ namespace ICD.Common.Utils.Extensions /// /// [PublicAPI] - public static bool AddSorted(this IList extends, T item, IComparer comparer) + public static bool AddSorted([NotNull] this IList extends, T item, [NotNull] IComparer comparer) { if (extends == null) throw new ArgumentNullException("extends"); @@ -92,7 +93,7 @@ namespace ICD.Common.Utils.Extensions /// /// [PublicAPI] - public static bool RemoveSorted(this IList extends, T item) + public static bool RemoveSorted([NotNull] this IList extends, T item) { if (extends == null) throw new ArgumentNullException("extends"); @@ -110,7 +111,8 @@ namespace ICD.Common.Utils.Extensions /// /// [PublicAPI] - public static bool RemoveSorted(this IList extends, T item, Func predicate) + public static bool RemoveSorted([NotNull] this IList extends, T item, + [NotNull] Func predicate) { if (extends == null) throw new ArgumentNullException("extends"); @@ -132,7 +134,7 @@ namespace ICD.Common.Utils.Extensions /// /// [PublicAPI] - public static bool RemoveSorted(this IList extends, T item, IComparer comparer) + public static bool RemoveSorted([NotNull] this IList extends, T item, [NotNull] IComparer comparer) { if (extends == null) throw new ArgumentNullException("extends"); @@ -160,7 +162,7 @@ namespace ICD.Common.Utils.Extensions /// /// [PublicAPI] - public static void InsertSorted(this IList extends, IEnumerable items) + public static void InsertSorted([NotNull] this IList extends, [NotNull] IEnumerable items) { if (extends == null) throw new ArgumentNullException("extends"); @@ -179,7 +181,8 @@ namespace ICD.Common.Utils.Extensions /// /// [PublicAPI] - public static void InsertSorted(this IList extends, IEnumerable items, IComparer comparer) + public static void InsertSorted([NotNull] this IList extends, [NotNull] IEnumerable items, + [NotNull] IComparer comparer) { if (extends == null) throw new ArgumentNullException("extends"); @@ -202,7 +205,8 @@ namespace ICD.Common.Utils.Extensions /// /// [PublicAPI] - public static void InsertSorted(this IList extends, IEnumerable items, Func predicate) + public static void InsertSorted([NotNull] this IList extends, [NotNull] IEnumerable items, + [NotNull] Func predicate) { if (extends == null) throw new ArgumentNullException("extends"); @@ -224,7 +228,7 @@ namespace ICD.Common.Utils.Extensions /// /// [PublicAPI] - public static int InsertSorted(this IList extends, T item) + public static int InsertSorted([NotNull] this IList extends, T item) { if (extends == null) throw new ArgumentNullException("extends"); @@ -241,7 +245,8 @@ namespace ICD.Common.Utils.Extensions /// /// [PublicAPI] - public static int InsertSorted(this IList extends, T item, Func predicate) + public static int InsertSorted([NotNull] this IList extends, T item, + [NotNull] Func predicate) { if (extends == null) throw new ArgumentNullException("extends"); @@ -261,7 +266,7 @@ namespace ICD.Common.Utils.Extensions /// /// [PublicAPI] - public static int InsertSorted(this IList extends, T item, IComparer comparer) + public static int InsertSorted([NotNull] this IList extends, T item, [NotNull] IComparer comparer) { if (extends == null) throw new ArgumentNullException("extends"); @@ -281,7 +286,7 @@ namespace ICD.Common.Utils.Extensions #endregion #region Contains Sorted - + /// /// Returns true if the sorted list contains the given item. /// @@ -290,7 +295,7 @@ namespace ICD.Common.Utils.Extensions /// /// [PublicAPI] - public static bool ContainsSorted(this IList extends, T item) + public static bool ContainsSorted([NotNull] this IList extends, T item) { if (extends == null) throw new ArgumentNullException("extends"); @@ -307,7 +312,7 @@ namespace ICD.Common.Utils.Extensions /// /// [PublicAPI] - public static bool ContainsSorted(this IList extends, T item, IComparer comparer) + public static bool ContainsSorted([NotNull] this IList extends, T item, [NotNull] IComparer comparer) { if (extends == null) throw new ArgumentNullException("extends"); @@ -331,7 +336,7 @@ namespace ICD.Common.Utils.Extensions /// /// [PublicAPI] - public static int BinarySearch(this IList extends, T item, IComparer comparer) + public static int BinarySearch([NotNull] this IList extends, T item, [NotNull] IComparer comparer) { if (extends == null) throw new ArgumentNullException("extends"); diff --git a/ICD.Common.Utils/Extensions/MethodInfoExtensions.cs b/ICD.Common.Utils/Extensions/MethodInfoExtensions.cs index 452010a..57956b2 100644 --- a/ICD.Common.Utils/Extensions/MethodInfoExtensions.cs +++ b/ICD.Common.Utils/Extensions/MethodInfoExtensions.cs @@ -1,5 +1,6 @@ using System; using System.Text; +using ICD.Common.Properties; #if SIMPLSHARP using Crestron.SimplSharp.Reflection; #else @@ -15,8 +16,11 @@ namespace ICD.Common.Utils.Extensions /// /// The Method /// Method signature - public static string GetSignature(this MethodInfo method) + public static string GetSignature([NotNull]this MethodInfo method) { + if (method == null) + throw new ArgumentNullException("method"); + return method.GetSignature(false); } @@ -26,8 +30,11 @@ namespace ICD.Common.Utils.Extensions /// The Method /// Return as a callable string(public void a(string b) would return a(b)) /// Method signature - public static string GetSignature(this MethodInfo method, bool callable) + public static string GetSignature([NotNull]this MethodInfo method, bool callable) { + if (method == null) + throw new ArgumentNullException("method"); + bool firstParam = true; StringBuilder sigBuilder = new StringBuilder(); @@ -123,5 +130,22 @@ namespace ICD.Common.Utils.Extensions return sigBuilder.ToString(); } + + /// + /// Cross-platform shim for getting MethodInfo for the delegate. + /// + /// + /// + public static MethodInfo GetMethodInfo([NotNull]this Delegate extends) + { + if (extends == null) + throw new ArgumentNullException("extends"); + +#if SIMPLSHARP + return extends.GetMethod(); +#else + return extends.Method; +#endif + } } } diff --git a/ICD.Common.Utils/Extensions/ParameterInfoExtensions.cs b/ICD.Common.Utils/Extensions/ParameterInfoExtensions.cs index d87a436..90b10ff 100644 --- a/ICD.Common.Utils/Extensions/ParameterInfoExtensions.cs +++ b/ICD.Common.Utils/Extensions/ParameterInfoExtensions.cs @@ -1,4 +1,5 @@ using System; +using ICD.Common.Properties; #if SIMPLSHARP using Crestron.SimplSharp.Reflection; #else @@ -14,7 +15,7 @@ namespace ICD.Common.Utils.Extensions /// /// /// - public static bool GetIsOut(this ParameterInfo extends) + public static bool GetIsOut([NotNull] this ParameterInfo extends) { if (extends == null) throw new ArgumentNullException("extends"); diff --git a/ICD.Common.Utils/Extensions/QueueExtensions.cs b/ICD.Common.Utils/Extensions/QueueExtensions.cs index f8f379b..ac120f1 100644 --- a/ICD.Common.Utils/Extensions/QueueExtensions.cs +++ b/ICD.Common.Utils/Extensions/QueueExtensions.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using ICD.Common.Properties; namespace ICD.Common.Utils.Extensions { @@ -11,7 +12,7 @@ namespace ICD.Common.Utils.Extensions /// /// /// - public static void EnqueueRange(this Queue extends, IEnumerable items) + public static void EnqueueRange([NotNull] this Queue extends, [NotNull] IEnumerable items) { if (extends == null) throw new ArgumentNullException("extends"); @@ -30,7 +31,7 @@ namespace ICD.Common.Utils.Extensions /// /// /// - public static bool Dequeue(this Queue extends, out T item) + public static bool Dequeue([NotNull] this Queue extends, out T item) { if (extends == null) throw new ArgumentNullException("extends"); @@ -51,7 +52,7 @@ namespace ICD.Common.Utils.Extensions /// /// /// - public static bool Peek(this Queue extends, out T item) + public static bool Peek([NotNull] this Queue extends, out T item) { if (extends == null) throw new ArgumentNullException("extends"); diff --git a/ICD.Common.Utils/Extensions/ReflectionExtensions.cs b/ICD.Common.Utils/Extensions/ReflectionExtensions.cs index 367534e..4e30516 100644 --- a/ICD.Common.Utils/Extensions/ReflectionExtensions.cs +++ b/ICD.Common.Utils/Extensions/ReflectionExtensions.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.Linq; +using ICD.Common.Properties; #if SIMPLSHARP using Crestron.SimplSharp.Reflection; #else @@ -20,7 +21,7 @@ namespace ICD.Common.Utils.Extensions /// /// /// - public static IEnumerable GetCustomAttributes(this ICustomAttributeProvider extends) + public static IEnumerable GetCustomAttributes([NotNull] this ICustomAttributeProvider extends) { if (extends == null) throw new ArgumentNullException("extends"); @@ -35,7 +36,8 @@ namespace ICD.Common.Utils.Extensions /// /// /// - public static IEnumerable GetCustomAttributes(this ICustomAttributeProvider extends, bool inherits) + public static IEnumerable GetCustomAttributes([NotNull] this ICustomAttributeProvider extends, + bool inherits) { if (extends == null) throw new ArgumentNullException("extends"); @@ -57,7 +59,7 @@ namespace ICD.Common.Utils.Extensions /// /// /// - public static T GetCustomAttribute(this ICustomAttributeProvider extends) + public static T GetCustomAttribute([NotNull] this ICustomAttributeProvider extends) { if (extends == null) throw new ArgumentNullException("extends"); @@ -72,7 +74,7 @@ namespace ICD.Common.Utils.Extensions /// /// /// - public static T GetCustomAttribute(this ICustomAttributeProvider extends, bool inherits) + public static T GetCustomAttribute([NotNull] this ICustomAttributeProvider extends, bool inherits) { if (extends == null) throw new ArgumentNullException("extends"); @@ -86,16 +88,18 @@ namespace ICD.Common.Utils.Extensions /// /// /// - /// /// - public static IEnumerable GetCustomAttributesIncludingBaseInterfaces(this Type extends) + public static IEnumerable GetCustomAttributesIncludingBaseInterfaces([NotNull] this Type extends) where T : Attribute { + if (extends == null) + throw new ArgumentNullException("extends"); + return extends.GetCustomAttributes(true) - .Union(extends.GetInterfaces() - .SelectMany(interfaceType => interfaceType - .GetCustomAttributes(true))) - .Distinct(); + .Union(extends.GetInterfaces() + .SelectMany(interfaceType => interfaceType + .GetCustomAttributes(true))) + .Distinct(); } /// @@ -103,23 +107,25 @@ namespace ICD.Common.Utils.Extensions /// /// /// - /// /// - public static IEnumerable GetCustomAttributesIncludingBaseInterfaces(this MemberInfo extends) + public static IEnumerable GetCustomAttributesIncludingBaseInterfaces([NotNull] this MemberInfo extends) where T : Attribute { + if (extends == null) + throw new ArgumentNullException("extends"); + return extends.GetCustomAttributes(true) - .Union(extends.DeclaringType? - .GetInterfaces() - .SelectMany(interfaceType => interfaceType - .GetMember( - extends.Name, - extends.MemberType, - BindingFlags.Instance) - .FirstOrDefault()? - .GetCustomAttributes(true) ?? Enumerable.Empty())? - .Except(null) ?? Enumerable.Empty()) - .Distinct(); + .Union(extends.DeclaringType? + .GetInterfaces() + .SelectMany(interfaceType => interfaceType + .GetMember(extends.Name, + extends.MemberType, + BindingFlags.Instance) + .FirstOrDefault()? + .GetCustomAttributes(true) ?? + Enumerable.Empty())? + .Except(null) ?? Enumerable.Empty()) + .Distinct(); } #endif } diff --git a/ICD.Common.Utils/Extensions/StringBuilderExtensions.cs b/ICD.Common.Utils/Extensions/StringBuilderExtensions.cs index a4e97f3..92acbf5 100644 --- a/ICD.Common.Utils/Extensions/StringBuilderExtensions.cs +++ b/ICD.Common.Utils/Extensions/StringBuilderExtensions.cs @@ -1,5 +1,6 @@ using System; using System.Text; +using ICD.Common.Properties; namespace ICD.Common.Utils.Extensions { @@ -9,7 +10,7 @@ namespace ICD.Common.Utils.Extensions /// Empties the StringBuilder. /// /// - public static void Clear(this StringBuilder extends) + public static void Clear([NotNull] this StringBuilder extends) { if (extends == null) throw new ArgumentNullException("extends"); @@ -23,7 +24,7 @@ namespace ICD.Common.Utils.Extensions /// /// /// - public static string Pop(this StringBuilder extends) + public static string Pop([NotNull] this StringBuilder extends) { if (extends == null) throw new ArgumentNullException("extends"); diff --git a/ICD.Common.Utils/Extensions/StringExtensions.cs b/ICD.Common.Utils/Extensions/StringExtensions.cs index e1be90e..066e984 100644 --- a/ICD.Common.Utils/Extensions/StringExtensions.cs +++ b/ICD.Common.Utils/Extensions/StringExtensions.cs @@ -15,7 +15,7 @@ namespace ICD.Common.Utils.Extensions /// /// [PublicAPI] - public static int IndexOf(this string extends, IEnumerable items, out string first) + public static int IndexOf([NotNull] this string extends, [NotNull] IEnumerable items, out string first) { if (extends == null) throw new ArgumentNullException("extends"); @@ -52,7 +52,7 @@ namespace ICD.Common.Utils.Extensions /// /// [PublicAPI] - public static bool StartsWith(this string extends, char character) + public static bool StartsWith([NotNull] this string extends, char character) { if (extends == null) throw new ArgumentNullException("extends"); @@ -67,7 +67,7 @@ namespace ICD.Common.Utils.Extensions /// /// [PublicAPI] - public static bool EndsWith(this string extends, char character) + public static bool EndsWith([NotNull] this string extends, char character) { if (extends == null) throw new ArgumentNullException("extends"); @@ -83,7 +83,8 @@ namespace ICD.Common.Utils.Extensions /// /// /// - public static IEnumerable Split(this string extends, char delimeter, int count) + [NotNull] + public static IEnumerable Split([NotNull] this string extends, char delimeter, int count) { if (extends == null) throw new ArgumentNullException("extends"); @@ -102,7 +103,8 @@ namespace ICD.Common.Utils.Extensions /// /// /// - private static IEnumerable SplitIterator(string value, char delimeter, int count) + [NotNull] + private static IEnumerable SplitIterator([NotNull] string value, char delimeter, int count) { while (count > 1) { @@ -125,7 +127,8 @@ namespace ICD.Common.Utils.Extensions /// /// [PublicAPI] - public static IEnumerable Split(this string extends, int chunkSize) + [NotNull] + public static IEnumerable Split([NotNull] this string extends, int chunkSize) { if (extends == null) throw new ArgumentNullException("extends"); @@ -134,7 +137,8 @@ namespace ICD.Common.Utils.Extensions throw new InvalidOperationException("chunkSize must be greater than 0"); return Enumerable.Range(0, (int)Math.Ceiling(extends.Length / (double)chunkSize)) - .Select(i => extends.Substring(i * chunkSize, Math.Min(chunkSize, extends.Length - (i * chunkSize)))); + .Select(i => extends.Substring(i * chunkSize, + Math.Min(chunkSize, extends.Length - (i * chunkSize)))); } /// @@ -143,7 +147,8 @@ namespace ICD.Common.Utils.Extensions /// /// [PublicAPI] - public static string RemoveWhitespace(this string extends) + [NotNull] + public static string RemoveWhitespace([NotNull] this string extends) { if (extends == null) throw new ArgumentNullException("extends"); @@ -152,12 +157,13 @@ namespace ICD.Common.Utils.Extensions } /// - /// Removes all occurances of the given string. + /// Removes all occurrences of the given string. /// /// /// /// - public static string Remove(this string extends, string other) + [NotNull] + public static string Remove([NotNull] this string extends, [NotNull] string other) { if (extends == null) throw new ArgumentNullException("extends"); @@ -176,19 +182,21 @@ namespace ICD.Common.Utils.Extensions } /// - /// Removes all occurances the given characters from the string. + /// Removes all occurrences the given characters from the string. /// /// /// /// [PublicAPI] - public static string Remove(this string extends, IEnumerable characters) + [NotNull] + public static string Remove([NotNull] this string extends, IEnumerable characters) { if (extends == null) throw new ArgumentNullException("extends"); if (characters == null) throw new ArgumentNullException("characters"); + var cSet = characters.ToIcdHashSet(); return new string(extends.Where(c => !cSet.Contains(c)).ToArray()); @@ -200,7 +208,7 @@ namespace ICD.Common.Utils.Extensions /// /// [PublicAPI] - public static bool IsNumeric(this string extends) + public static bool IsNumeric([NotNull] this string extends) { if (extends == null) throw new ArgumentNullException("extends"); @@ -214,7 +222,7 @@ namespace ICD.Common.Utils.Extensions /// /// /// - public static bool Contains(this string extends, char character) + public static bool Contains([NotNull] this string extends, char character) { if (extends == null) throw new ArgumentNullException("extends"); @@ -227,8 +235,11 @@ namespace ICD.Common.Utils.Extensions /// /// /// - public static int GetStableHashCode(this string extends) + public static int GetStableHashCode([NotNull] this string extends) { + if (extends == null) + throw new ArgumentNullException("extends"); + unchecked { int hash1 = 5381; diff --git a/ICD.Common.Utils/Extensions/TypeExtensions.cs b/ICD.Common.Utils/Extensions/TypeExtensions.cs index 685dd65..11e5fc9 100644 --- a/ICD.Common.Utils/Extensions/TypeExtensions.cs +++ b/ICD.Common.Utils/Extensions/TypeExtensions.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using System.Linq; using System.Text; +using ICD.Common.Properties; using ICD.Common.Utils.Collections; #if SIMPLSHARP using Crestron.SimplSharp.Reflection; @@ -83,7 +84,7 @@ namespace ICD.Common.Utils.Extensions /// /// /// - public static bool CanBeNull(this Type extends) + public static bool CanBeNull([NotNull]this Type extends) { if (extends == null) throw new ArgumentException("extends"); @@ -96,7 +97,7 @@ namespace ICD.Common.Utils.Extensions /// /// /// - public static bool IsNumeric(this Type extends) + public static bool IsNumeric([NotNull]this Type extends) { if (extends == null) throw new ArgumentException("extends"); @@ -109,7 +110,7 @@ namespace ICD.Common.Utils.Extensions /// /// /// - public static bool IsSignedNumeric(this Type extends) + public static bool IsSignedNumeric([NotNull]this Type extends) { if (extends == null) throw new ArgumentException("extends"); @@ -122,7 +123,7 @@ namespace ICD.Common.Utils.Extensions /// /// /// - public static bool IsDecimalNumeric(this Type extends) + public static bool IsDecimalNumeric([NotNull]this Type extends) { if (extends == null) throw new ArgumentException("extends"); @@ -135,7 +136,7 @@ namespace ICD.Common.Utils.Extensions /// /// /// - public static bool IsIntegerNumeric(this Type extends) + public static bool IsIntegerNumeric([NotNull]this Type extends) { if (extends == null) throw new ArgumentException("extends"); @@ -148,7 +149,8 @@ namespace ICD.Common.Utils.Extensions /// /// /// - public static Assembly GetAssembly(this Type extends) + [NotNull] + public static Assembly GetAssembly([NotNull]this Type extends) { if (extends == null) throw new ArgumentNullException("extends"); @@ -167,7 +169,7 @@ namespace ICD.Common.Utils.Extensions /// /// /// - public static bool IsAssignableTo(this Type from) + public static bool IsAssignableTo([NotNull]this Type from) { if (from == null) throw new ArgumentNullException("from"); @@ -181,7 +183,7 @@ namespace ICD.Common.Utils.Extensions /// /// /// - public static bool IsAssignableTo(this Type from, Type to) + public static bool IsAssignableTo([NotNull]this Type from, [NotNull]Type to) { if (from == null) throw new ArgumentNullException("from"); @@ -197,7 +199,8 @@ namespace ICD.Common.Utils.Extensions /// /// /// - public static IEnumerable GetAllTypes(this Type extends) + [NotNull] + public static IEnumerable GetAllTypes([NotNull]this Type extends) { if (extends == null) throw new ArgumentNullException("extends"); @@ -221,7 +224,8 @@ namespace ICD.Common.Utils.Extensions /// /// /// - public static IEnumerable GetBaseTypes(this Type extends) + [NotNull] + public static IEnumerable GetBaseTypes([NotNull]this Type extends) { if (extends == null) throw new ArgumentNullException("extends"); @@ -236,7 +240,8 @@ namespace ICD.Common.Utils.Extensions return types; } - private static IEnumerable GetBaseTypesIterator(Type type) + [NotNull] + private static IEnumerable GetBaseTypesIterator([NotNull] Type type) { do { @@ -257,7 +262,8 @@ namespace ICD.Common.Utils.Extensions /// /// /// - public static IEnumerable GetImmediateInterfaces(this Type extends) + [NotNull] + public static IEnumerable GetImmediateInterfaces([NotNull]this Type extends) { if (extends == null) throw new ArgumentNullException("extends"); @@ -286,7 +292,8 @@ namespace ICD.Common.Utils.Extensions /// /// /// - public static IEnumerable GetMinimalInterfaces(this Type extends) + [NotNull] + public static IEnumerable GetMinimalInterfaces([NotNull]this Type extends) { if (extends == null) throw new ArgumentNullException("extends"); @@ -314,7 +321,8 @@ namespace ICD.Common.Utils.Extensions /// /// /// - public static string GetNameWithoutGenericArity(this Type extends) + [NotNull] + public static string GetNameWithoutGenericArity([NotNull]this Type extends) { if (extends == null) throw new ArgumentNullException("extends"); @@ -330,7 +338,8 @@ namespace ICD.Common.Utils.Extensions /// /// /// - public static string GetMinimalName(this Type extends) + [NotNull] + public static string GetMinimalName([NotNull]this Type extends) { if (extends == null) throw new ArgumentNullException("extends"); @@ -380,7 +389,8 @@ namespace ICD.Common.Utils.Extensions /// /// /// - public static string GetNameWithoutAssemblyDetails(this Type extends) + [NotNull] + public static string GetNameWithoutAssemblyDetails([NotNull]this Type extends) { if (extends == null) throw new ArgumentNullException("extends"); @@ -401,7 +411,8 @@ namespace ICD.Common.Utils.Extensions /// /// /// - private static string RemoveAssemblyDetails(string fullyQualifiedTypeName) + [NotNull] + private static string RemoveAssemblyDetails([NotNull] string fullyQualifiedTypeName) { StringBuilder builder = new StringBuilder(); @@ -450,7 +461,8 @@ namespace ICD.Common.Utils.Extensions /// /// Type. May be generic or nullable /// Full type name, fully qualified namespaces - public static string GetSyntaxName(this Type extends) + [NotNull] + public static string GetSyntaxName([NotNull]this Type extends) { if (extends == null) throw new ArgumentNullException("extends"); diff --git a/ICD.Common.Utils/Extensions/UshortExtensions.cs b/ICD.Common.Utils/Extensions/UShortExtensions.cs similarity index 100% rename from ICD.Common.Utils/Extensions/UshortExtensions.cs rename to ICD.Common.Utils/Extensions/UShortExtensions.cs diff --git a/ICD.Common.Utils/Extensions/UriExtensions.cs b/ICD.Common.Utils/Extensions/UriExtensions.cs index 520ecbe..952dc5b 100644 --- a/ICD.Common.Utils/Extensions/UriExtensions.cs +++ b/ICD.Common.Utils/Extensions/UriExtensions.cs @@ -1,5 +1,6 @@ using System; using System.Linq; +using ICD.Common.Properties; namespace ICD.Common.Utils.Extensions { @@ -10,7 +11,8 @@ namespace ICD.Common.Utils.Extensions /// /// /// - public static string GetUserName(this Uri extends) + [NotNull] + public static string GetUserName([NotNull] this Uri extends) { if (extends == null) throw new ArgumentNullException("extends"); @@ -23,7 +25,8 @@ namespace ICD.Common.Utils.Extensions /// /// /// - public static string GetPassword(this Uri extends) + [NotNull] + public static string GetPassword([NotNull] this Uri extends) { if (extends == null) throw new ArgumentNullException("extends"); diff --git a/ICD.Common.Utils/ReflectionUtils.cs b/ICD.Common.Utils/ReflectionUtils.cs index 899dcd3..514910d 100644 --- a/ICD.Common.Utils/ReflectionUtils.cs +++ b/ICD.Common.Utils/ReflectionUtils.cs @@ -4,6 +4,7 @@ using System.Linq; using ICD.Common.Properties; using ICD.Common.Utils.Extensions; using ICD.Common.Utils.IO; +using MethodInfoExtensions = ICD.Common.Utils.Extensions.MethodInfoExtensions; #if SIMPLSHARP using Crestron.SimplSharp.CrestronIO; using Crestron.SimplSharp.Reflection; @@ -373,7 +374,7 @@ namespace ICD.Common.Utils object handler = eventHandler.Target; // ReSharper disable InvokeAsExtensionMethod - MethodInfo callback = EventHandlerExtensions.GetMethodInfo(eventHandler); + MethodInfo callback = MethodInfoExtensions.GetMethodInfo(eventHandler); // ReSharper restore InvokeAsExtensionMethod return SubscribeEvent(instance, eventInfo, handler, callback); @@ -397,7 +398,7 @@ namespace ICD.Common.Utils object handler = eventHandler.Target; // ReSharper disable InvokeAsExtensionMethod - MethodInfo callback = EventHandlerExtensions.GetMethodInfo(eventHandler); + MethodInfo callback = MethodInfoExtensions.GetMethodInfo(eventHandler); // ReSharper restore InvokeAsExtensionMethod return SubscribeEvent(instance, eventInfo, handler, callback);