diff --git a/AssemblyExtensions.cs b/AssemblyExtensions.cs index 0a88894..cb2c6d5 100644 --- a/AssemblyExtensions.cs +++ b/AssemblyExtensions.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Concurrent; using System.Reflection; +using Platform.Exceptions; using Platform.Collections.Lists; namespace Platform.Reflection @@ -14,7 +15,7 @@ public static class AssemblyExtensions /// public static Type[] GetLoadableTypes(this Assembly assembly) { - if (assembly == null) throw new ArgumentNullException(nameof(assembly)); + Ensure.Always.ArgumentNotNull(assembly, nameof(assembly)); try { return assembly.GetTypes(); diff --git a/CachedTypeInfo.cs b/CachedTypeInfo.cs index cfa64d4..ba9d14a 100644 --- a/CachedTypeInfo.cs +++ b/CachedTypeInfo.cs @@ -1,8 +1,8 @@ using System; using System.Runtime.InteropServices; +using Platform.Exceptions; // ReSharper disable AssignmentInConditionalExpression - // ReSharper disable BuiltInTypeReferenceStyle // ReSharper disable StaticFieldInGenericType @@ -13,43 +13,31 @@ public class CachedTypeInfo public static readonly bool IsSupported; public static readonly Type Type; public static readonly Type UnderlyingType; - public static readonly Type SignedVersion; public static readonly Type UnsignedVersion; - public static readonly bool IsFloatPoint; public static readonly bool IsNumeric; public static readonly bool IsSigned; public static readonly bool CanBeNumeric; - public static readonly bool IsNullable; - public static readonly int BitsLength; public static readonly T MinValue; public static readonly T MaxValue; static CachedTypeInfo() { - Type = typeof(T); - - if (IsNullable = Type.IsNullable()) //-V3055 - UnderlyingType = Nullable.GetUnderlyingType(Type); - else - UnderlyingType = Type; - try { + Type = typeof(T); + IsNullable = Type.IsNullable(); + UnderlyingType = IsNullable ? Nullable.GetUnderlyingType(Type) : Type; var canBeNumeric = UnderlyingType.CanBeNumeric(); var isNumeric = UnderlyingType.IsNumeric(); var isSigned = UnderlyingType.IsSigned(); var isFloatPoint = UnderlyingType.IsFloatPoint(); - var bitsLength = Marshal.SizeOf(UnderlyingType) * 8; - GetMinAndMaxValues(UnderlyingType, out T minValue, out T maxValue); - GetSignedAndUnsignedVersions(UnderlyingType, isSigned, out Type signedVersion, out Type unsignedVersion); - IsSupported = true; CanBeNumeric = canBeNumeric; IsNumeric = isNumeric; @@ -61,8 +49,9 @@ static CachedTypeInfo() SignedVersion = signedVersion; UnsignedVersion = unsignedVersion; } - catch (Exception) + catch (Exception exception) { + exception.Ignore(); } } diff --git a/DynamicExtensions.cs b/DynamicExtensions.cs index 8c361dc..0f66aff 100644 --- a/DynamicExtensions.cs +++ b/DynamicExtensions.cs @@ -9,7 +9,9 @@ public static bool HasProperty(this object @object, string propertyName) { var type = @object.GetType(); if (type == typeof(ExpandoObject)) + { return ((IDictionary)@object).ContainsKey(propertyName); + } return type.GetProperty(propertyName) != null; } } diff --git a/Platform.Reflection.csproj b/Platform.Reflection.csproj index c4e09d2..b9cbf89 100644 --- a/Platform.Reflection.csproj +++ b/Platform.Reflection.csproj @@ -4,7 +4,7 @@ LinksPlatform's Platform.Reflection Class Library Konstantin Diachenko Platform.Reflection - 0.0.2 + 0.0.3 Konstantin Diachenko netstandard2.0 Platform.Reflection @@ -21,6 +21,10 @@ false true snupkg + Platform.Collections updated to 0.0.3 version. +All exceptions are ignored correcly now. +Types classes use IList<T> (read-only) instead of T[] in public field. +Refactoring. diff --git a/README.md b/README.md index 3b04dfd..edb9b6f 100644 --- a/README.md +++ b/README.md @@ -6,6 +6,6 @@ LinksPlatform's Platform.Reflection Class Library. Namespace: Platform.Reflection -Forked from: https://github.com/Konard/LinksPlatform/tree/822c3c283cab152489d49e6a1727ca76f8595ce2/Platform/Platform.Helpers/Reflection +Forked from: [Konard/LinksPlatform/Platform/Platform.Helpers/Reflection](https://github.com/Konard/LinksPlatform/tree/822c3c283cab152489d49e6a1727ca76f8595ce2/Platform/Platform.Helpers/Reflection) -NuGet package: https://www.nuget.org/packages/Platform.Reflection +NuGet package: [Platform.Reflection](https://www.nuget.org/packages/Platform.Reflection) \ No newline at end of file diff --git a/TypeExtensions.cs b/TypeExtensions.cs index 5a398c4..e2ba2cc 100644 --- a/TypeExtensions.cs +++ b/TypeExtensions.cs @@ -3,6 +3,7 @@ using System.Linq; using System.Reflection; using System.Runtime.CompilerServices; +using Platform.Collections; namespace Platform.Reflection { @@ -12,19 +13,32 @@ public static class TypeExtensions static private readonly HashSet IsNumericTypes; static private readonly HashSet IsSignedTypes; static private readonly HashSet IsFloatPointTypes; + static private readonly Dictionary UnsignedVersionsOfSignedTypes; + static private readonly Dictionary SignedVersionsOfUnsignedTypes; static TypeExtensions() { - CanBeNumericTypes = new HashSet() { typeof(bool), typeof(char), typeof(DateTime), typeof(TimeSpan) }; - IsNumericTypes = new HashSet() { typeof(byte), typeof(ushort), typeof(uint), typeof(ulong) }; - IsSignedTypes = new HashSet() { typeof(sbyte), typeof(short), typeof(int), typeof(long) }; - IsFloatPointTypes = new HashSet() { typeof(decimal), typeof(double), typeof(float) }; - + CanBeNumericTypes = new HashSet { typeof(bool), typeof(char), typeof(DateTime), typeof(TimeSpan) }; + IsNumericTypes = new HashSet { typeof(byte), typeof(ushort), typeof(uint), typeof(ulong) }; + IsSignedTypes = new HashSet { typeof(sbyte), typeof(short), typeof(int), typeof(long) }; + IsFloatPointTypes = new HashSet { typeof(decimal), typeof(double), typeof(float) }; + UnsignedVersionsOfSignedTypes = new Dictionary + { + { typeof(sbyte), typeof(byte) }, + { typeof(short), typeof(ushort) }, + { typeof(int), typeof(uint) }, + { typeof(long), typeof(ulong) }, + }; + SignedVersionsOfUnsignedTypes = new Dictionary + { + { typeof(byte), typeof(sbyte)}, + { typeof(ushort), typeof(short) }, + { typeof(uint), typeof(int) }, + { typeof(ulong), typeof(long) }, + }; CanBeNumericTypes.UnionWith(IsNumericTypes); - CanBeNumericTypes.UnionWith(IsSignedTypes); IsNumericTypes.UnionWith(IsSignedTypes); - CanBeNumericTypes.UnionWith(IsFloatPointTypes); IsNumericTypes.UnionWith(IsFloatPointTypes); IsSignedTypes.UnionWith(IsFloatPointTypes); @@ -41,13 +55,12 @@ public static MethodInfo GetGenericMethod(this Type type, string name, Type[] ge { var methods = from m in type.GetMethods() where m.Name == name - && m.IsGenericMethodDefinition + && m.IsGenericMethodDefinition let typeParams = m.GetGenericArguments() let normalParams = m.GetParameters().Select(x => x.ParameterType) where typeParams.SequenceEqual(genericParameterTypes) && normalParams.SequenceEqual(argumentTypes) select m; - var method = methods.Single(); return method; } @@ -73,31 +86,9 @@ where typeParams.SequenceEqual(genericParameterTypes) [MethodImpl(MethodImplOptions.AggressiveInlining)] public static bool IsNullable(this Type type) => type.IsGeneric(typeof(Nullable<>)); - public static Type GetUnsignedVersionOrNull(this Type signedType) - { - if (signedType == typeof(sbyte)) - return typeof(byte); - if (signedType == typeof(short)) - return typeof(ushort); - if (signedType == typeof(int)) - return typeof(uint); - if (signedType == typeof(long)) - return typeof(ulong); - return null; - } + public static Type GetUnsignedVersionOrNull(this Type signedType) => UnsignedVersionsOfSignedTypes.GetOrDefault(signedType); - public static Type GetSignedVersionOrNull(this Type unsignedType) - { - if (unsignedType == typeof(byte)) - return typeof(sbyte); - if (unsignedType == typeof(ushort)) - return typeof(short); - if (unsignedType == typeof(uint)) - return typeof(int); - if (unsignedType == typeof(ulong)) - return typeof(long); - return null; - } + public static Type GetSignedVersionOrNull(this Type unsignedType) => SignedVersionsOfUnsignedTypes.GetOrDefault(unsignedType); public static bool CanBeNumeric(this Type type) => CanBeNumericTypes.Contains(type); diff --git a/Types.cs b/Types.cs index 1835f2b..506bab4 100644 --- a/Types.cs +++ b/Types.cs @@ -1,49 +1,54 @@ using System; using System.Collections.Concurrent; using System.Collections.Generic; +using System.Collections.ObjectModel; namespace Platform.Reflection { public abstract class Types { - private static readonly ConcurrentDictionary Cache = new ConcurrentDictionary(); + private static readonly ConcurrentDictionary> Cache = new ConcurrentDictionary>(); - protected Type[] ToArray() + protected IList ToReadOnlyList() { - var array = GetType().GetGenericArguments(); - - var list = new List(); - AppendTypes(list, array); - return list.ToArray(); + var types = GetType().GetGenericArguments(); + var result = new List(); + AppendTypes(result, types); + return new ReadOnlyCollection(result);; } - private void AppendTypes(List list, Type[] array) + private void AppendTypes(List container, IList types) { - for (var i = 0; i < array.Length; i++) + for (var i = 0; i < types.Count; i++) { - var element = array[i]; - - if (element == typeof(Types)) - continue; - - if (element.IsSubclassOf(typeof(Types))) - AppendTypes(list, element.GetFirstField().GetStaticValue()); - else - list.Add(element); + var element = types[i]; + if (element != typeof(Types)) + { + if (element.IsSubclassOf(typeof(Types))) + { + AppendTypes(container, element.GetFirstField().GetStaticValue>()); + } + else + { + container.Add(element); + } + } } } - public static Type[] Get() + public static IList Get() { - var type = typeof(T); - - return Cache.GetOrAdd(type, t => + return Cache.GetOrAdd(typeof(T), type => { if (type == typeof(Types)) - return new Type[0]; + { + return Array.AsReadOnly(new Type[0]); + } if (type.IsSubclassOf(typeof(Types))) - return type.GetFirstField().GetStaticValue(); - return new[] { type }; + { + return type.GetFirstField().GetStaticValue>(); + } + return Array.AsReadOnly(new[] { type }); }); } } diff --git a/Types[T1, T2, T3, T4, T5, T6, T7].cs b/Types[T1, T2, T3, T4, T5, T6, T7].cs index efe4fbd..6c239cf 100644 --- a/Types[T1, T2, T3, T4, T5, T6, T7].cs +++ b/Types[T1, T2, T3, T4, T5, T6, T7].cs @@ -1,11 +1,11 @@ using System; +using System.Collections.Generic; namespace Platform.Reflection { public class Types : Types { - public static readonly Type[] Array = new Types().ToArray(); - + public static readonly IList List = new Types().ToReadOnlyList(); private Types() { } } } diff --git a/Types[T1, T2, T3, T4, T5, T6].cs b/Types[T1, T2, T3, T4, T5, T6].cs index 080a659..7d4136f 100644 --- a/Types[T1, T2, T3, T4, T5, T6].cs +++ b/Types[T1, T2, T3, T4, T5, T6].cs @@ -1,11 +1,11 @@ using System; +using System.Collections.Generic; namespace Platform.Reflection { public class Types : Types { - public static readonly Type[] Array = new Types().ToArray(); - + public static readonly IList List = new Types().ToReadOnlyList(); private Types() { } } } diff --git a/Types[T1, T2, T3, T4, T5].cs b/Types[T1, T2, T3, T4, T5].cs index f93c45a..4773b9c 100644 --- a/Types[T1, T2, T3, T4, T5].cs +++ b/Types[T1, T2, T3, T4, T5].cs @@ -1,11 +1,11 @@ using System; +using System.Collections.Generic; namespace Platform.Reflection { public class Types : Types { - public static readonly Type[] Array = new Types().ToArray(); - + public static readonly IList List = new Types().ToReadOnlyList(); private Types() { } } } diff --git a/Types[T1, T2, T3, T4].cs b/Types[T1, T2, T3, T4].cs index 99f6352..66b7296 100644 --- a/Types[T1, T2, T3, T4].cs +++ b/Types[T1, T2, T3, T4].cs @@ -1,11 +1,11 @@ using System; +using System.Collections.Generic; namespace Platform.Reflection { public class Types : Types { - public static readonly Type[] Array = new Types().ToArray(); - + public static readonly IList List = new Types().ToReadOnlyList(); private Types() { } } } diff --git a/Types[T1, T2, T3].cs b/Types[T1, T2, T3].cs index b60e8d0..ca4d0a0 100644 --- a/Types[T1, T2, T3].cs +++ b/Types[T1, T2, T3].cs @@ -1,11 +1,11 @@ using System; +using System.Collections.Generic; namespace Platform.Reflection { public class Types : Types { - public static readonly Type[] Array = new Types().ToArray(); - + public static readonly IList List = new Types().ToReadOnlyList(); private Types() { } } } diff --git a/Types[T1, T2].cs b/Types[T1, T2].cs index 03d04c7..0e043d2 100644 --- a/Types[T1, T2].cs +++ b/Types[T1, T2].cs @@ -1,11 +1,11 @@ using System; +using System.Collections.Generic; namespace Platform.Reflection { public class Types : Types { - public static readonly Type[] Array = new Types().ToArray(); - + public static readonly IList List = new Types().ToReadOnlyList(); private Types() { } } } diff --git a/Types[T].cs b/Types[T].cs index 2e4e0d8..c0d1344 100644 --- a/Types[T].cs +++ b/Types[T].cs @@ -1,11 +1,11 @@ using System; +using System.Collections.Generic; namespace Platform.Reflection { public class Types : Types { - public static readonly Type[] Array = new Types().ToArray(); - + public static readonly IList Array = new Types().ToReadOnlyList(); private Types() { } } }