From 546cabf0b4aeac118c2ea8d5e2d886e00d36c9e2 Mon Sep 17 00:00:00 2001 From: Chris Cameron Date: Fri, 17 Nov 2017 11:38:01 -0500 Subject: [PATCH 1/3] Extension methods for determining if a type is numeric --- .../Extensions/TypeExtensionsTest.cs | 145 ++++++++++++------ ICD.Common.Utils/Extensions/TypeExtensions.cs | 75 ++++++++- 2 files changed, 172 insertions(+), 48 deletions(-) diff --git a/ICD.Common.Utils.Tests/Extensions/TypeExtensionsTest.cs b/ICD.Common.Utils.Tests/Extensions/TypeExtensionsTest.cs index ee941af..cf44533 100644 --- a/ICD.Common.Utils.Tests/Extensions/TypeExtensionsTest.cs +++ b/ICD.Common.Utils.Tests/Extensions/TypeExtensionsTest.cs @@ -5,61 +5,112 @@ using NUnit.Framework; namespace ICD.Common.Utils.Tests.Extensions { - [TestFixture] - public sealed class TypeExtensionsTest - { - [Test] - public void IsAssignableToTest() - { - Assert.IsTrue(typeof(string).IsAssignableTo(typeof(object))); - Assert.IsFalse(typeof(object).IsAssignableTo(typeof(string))); - } + [TestFixture] + public sealed class TypeExtensionsTest + { + [TestCase(typeof(byte), true)] + [TestCase(typeof(decimal), true)] + [TestCase(typeof(double), true)] + [TestCase(typeof(float), true)] + [TestCase(typeof(int), true)] + [TestCase(typeof(long), true)] + [TestCase(typeof(sbyte), true)] + [TestCase(typeof(short), true)] + [TestCase(typeof(uint), true)] + [TestCase(typeof(ulong), true)] + [TestCase(typeof(ushort), true)] + [TestCase(typeof(string), false)] + public void IsNumericTest(Type value, bool expected) + { + Assert.AreEqual(expected, value.IsNumeric()); + } - [Test] - public void GetAllTypesTest() - { - Type[] allTypes = typeof(B).GetAllTypes().ToArray(); + [TestCase(typeof(byte), false)] + [TestCase(typeof(decimal), true)] + [TestCase(typeof(double), true)] + [TestCase(typeof(float), true)] + [TestCase(typeof(int), true)] + [TestCase(typeof(long), true)] + [TestCase(typeof(sbyte), true)] + [TestCase(typeof(short), true)] + [TestCase(typeof(uint), false)] + [TestCase(typeof(ulong), false)] + [TestCase(typeof(ushort), false)] + [TestCase(typeof(string), false)] + public void IsSignedNumericTest(Type value, bool expected) + { + Assert.AreEqual(expected, value.IsSignedNumeric()); + } - Assert.AreEqual(6, allTypes.Length); + [TestCase(typeof(byte), false)] + [TestCase(typeof(decimal), true)] + [TestCase(typeof(double), true)] + [TestCase(typeof(float), true)] + [TestCase(typeof(int), false)] + [TestCase(typeof(long), false)] + [TestCase(typeof(sbyte), false)] + [TestCase(typeof(short), false)] + [TestCase(typeof(uint), false)] + [TestCase(typeof(ulong), false)] + [TestCase(typeof(ushort), false)] + [TestCase(typeof(string), false)] + public void IsDecimalNumericTest(Type value, bool expected) + { + Assert.AreEqual(expected, value.IsDecimalNumeric()); + } - Assert.IsTrue(allTypes.Contains(typeof(E))); - Assert.IsTrue(allTypes.Contains(typeof(D))); - Assert.IsTrue(allTypes.Contains(typeof(C))); - Assert.IsTrue(allTypes.Contains(typeof(B))); - Assert.IsTrue(allTypes.Contains(typeof(A))); - Assert.IsTrue(allTypes.Contains(typeof(object))); - } + [TestCase(typeof(string), typeof(object), true)] + [TestCase(typeof(object), typeof(string), false)] + public void IsAssignableToTest(Type a, Type b, bool expected) + { + Assert.AreEqual(expected, a.IsAssignableTo(b)); + } - [Test] - public void GetBaseTypesTest() - { - Type[] baseTypes = typeof(B).GetBaseTypes().ToArray(); + [Test] + public void GetAllTypesTest() + { + Type[] allTypes = typeof(B).GetAllTypes().ToArray(); - Assert.AreEqual(2, baseTypes.Length); + Assert.AreEqual(6, allTypes.Length); - Assert.IsFalse(baseTypes.Contains(typeof(B))); - Assert.IsTrue(baseTypes.Contains(typeof(A))); - Assert.IsTrue(baseTypes.Contains(typeof(object))); - } + Assert.IsTrue(allTypes.Contains(typeof(E))); + Assert.IsTrue(allTypes.Contains(typeof(D))); + Assert.IsTrue(allTypes.Contains(typeof(C))); + Assert.IsTrue(allTypes.Contains(typeof(B))); + Assert.IsTrue(allTypes.Contains(typeof(A))); + Assert.IsTrue(allTypes.Contains(typeof(object))); + } - private interface C - { - } + [Test] + public void GetBaseTypesTest() + { + Type[] baseTypes = typeof(B).GetBaseTypes().ToArray(); - private interface D - { - } + Assert.AreEqual(2, baseTypes.Length); - private interface E : C, D - { - } + Assert.IsFalse(baseTypes.Contains(typeof(B))); + Assert.IsTrue(baseTypes.Contains(typeof(A))); + Assert.IsTrue(baseTypes.Contains(typeof(object))); + } - private class A - { - } + private interface C + { + } - private class B : A, E - { - } - } -} \ No newline at end of file + private interface D + { + } + + private interface E : C, D + { + } + + private class A + { + } + + private class B : A, E + { + } + } +} diff --git a/ICD.Common.Utils/Extensions/TypeExtensions.cs b/ICD.Common.Utils/Extensions/TypeExtensions.cs index 72f5632..beb6f83 100644 --- a/ICD.Common.Utils/Extensions/TypeExtensions.cs +++ b/ICD.Common.Utils/Extensions/TypeExtensions.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using ICD.Common.Utils.Collections; #if SIMPLSHARP using Crestron.SimplSharp.Reflection; #else @@ -10,6 +11,78 @@ namespace ICD.Common.Utils.Extensions { public static class TypeExtensions { + private static readonly IcdHashSet s_NumericTypes = new IcdHashSet + { + typeof(byte), + typeof(decimal), + typeof(double), + typeof(float), + typeof(int), + typeof(long), + typeof(sbyte), + typeof(short), + typeof(uint), + typeof(ulong), + typeof(ushort) + }; + + private static readonly IcdHashSet s_SignedNumericTypes = new IcdHashSet + { + typeof(decimal), + typeof(double), + typeof(float), + typeof(int), + typeof(long), + typeof(sbyte), + typeof(short), + }; + + private static readonly IcdHashSet s_DecimalNumericTypes = new IcdHashSet + { + typeof(decimal), + typeof(double), + typeof(float), + }; + + /// + /// Returns true if the given type is a numeric type. + /// + /// + /// + public static bool IsNumeric(this Type extends) + { + if (extends == null) + throw new ArgumentException("extends"); + + return s_NumericTypes.Contains(extends); + } + + /// + /// Returns true if the given type is a signed numeric type. + /// + /// + /// + public static bool IsSignedNumeric(this Type extends) + { + if (extends == null) + throw new ArgumentException("extends"); + + return s_SignedNumericTypes.Contains(extends); + } + + /// + /// Returns true if the given type is a non-integer numeric type. + /// + /// + /// + public static bool IsDecimalNumeric(this Type extends) + { + if (extends == null) + throw new ArgumentException("extends"); + + return s_DecimalNumericTypes.Contains(extends); + } + public static Assembly GetAssembly(this Type extends) { if (extends == null) @@ -68,7 +141,7 @@ namespace ICD.Common.Utils.Extensions { extends = extends #if !SIMPLSHARP - .GetTypeInfo() + .GetTypeInfo() #endif .BaseType; From 772a87bab6692cb1422c69c2bf2fd819fb38be5d Mon Sep 17 00:00:00 2001 From: Chris Cameron Date: Fri, 17 Nov 2017 12:20:25 -0500 Subject: [PATCH 2/3] Extension method for getting the creation time of an assembly --- .../Extensions/AssemblyExtensions.cs | 16 +++++++++++++++- ICD.Common.Utils/IO/IcdFile.cs | 6 ++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/ICD.Common.Utils/Extensions/AssemblyExtensions.cs b/ICD.Common.Utils/Extensions/AssemblyExtensions.cs index 6e260da..3a67c37 100644 --- a/ICD.Common.Utils/Extensions/AssemblyExtensions.cs +++ b/ICD.Common.Utils/Extensions/AssemblyExtensions.cs @@ -20,7 +20,7 @@ namespace ICD.Common.Utils.Extensions public static string GetPath(this Assembly extends) { if (extends == null) - throw new ArgumentNullException(); + throw new ArgumentNullException("extends"); string path = extends #if SIMPLSHARP @@ -43,5 +43,19 @@ namespace ICD.Common.Utils.Extensions return IcdFile.Exists(path) ? path : null; } + + /// + /// Gets the creation date of the given assembly. + /// + /// + /// + public static DateTime GetCreationTime(this Assembly extends) + { + if (extends == null) + throw new ArgumentNullException("extends"); + + string path = extends.GetPath(); + return path == null ? DateTime.MinValue : IcdFile.GetCreationTime(path); + } } } diff --git a/ICD.Common.Utils/IO/IcdFile.cs b/ICD.Common.Utils/IO/IcdFile.cs index 32e35d5..7c8bdc0 100644 --- a/ICD.Common.Utils/IO/IcdFile.cs +++ b/ICD.Common.Utils/IO/IcdFile.cs @@ -77,5 +77,11 @@ namespace ICD.Common.Utils.IO { return new IcdFileStream(File.Open(path, mode)); } + + [PublicAPI] + public static DateTime GetCreationTime(string path) + { + return File.GetCreationTime(path); + } } } From 3e143c07a8614fb782fba5ce04a5523e820fd52e Mon Sep 17 00:00:00 2001 From: Chris Cameron Date: Fri, 17 Nov 2017 13:33:34 -0500 Subject: [PATCH 3/3] Test stub --- ICD.Common.Utils.Tests/Extensions/AssemblyExtensionsTest.cs | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/ICD.Common.Utils.Tests/Extensions/AssemblyExtensionsTest.cs b/ICD.Common.Utils.Tests/Extensions/AssemblyExtensionsTest.cs index 02122f9..72d2f67 100644 --- a/ICD.Common.Utils.Tests/Extensions/AssemblyExtensionsTest.cs +++ b/ICD.Common.Utils.Tests/Extensions/AssemblyExtensionsTest.cs @@ -7,6 +7,12 @@ namespace ICD.Common.Utils.Tests.Extensions { [Test] public void GetPathTest() + { + Assert.Inconclusive(); + } + + [Test] + public void GetCreationTimeTest() { Assert.Inconclusive(); }