Skip to content

Commit

Permalink
All exceptions are ignored correcly now.
Browse files Browse the repository at this point in the history
Types classes use IList<T> (read-only) instead of T[] in public field.
Refactoring.
Version 0.0.3.
  • Loading branch information
Konard committed Jul 26, 2019
1 parent 9785e49 commit 8fd15b2
Show file tree
Hide file tree
Showing 14 changed files with 85 additions and 93 deletions.
3 changes: 2 additions & 1 deletion AssemblyExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Concurrent;
using System.Reflection;
using Platform.Exceptions;
using Platform.Collections.Lists;

namespace Platform.Reflection
Expand All @@ -14,7 +15,7 @@ public static class AssemblyExtensions
/// </remarks>
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();
Expand Down
23 changes: 6 additions & 17 deletions CachedTypeInfo.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
using System;
using System.Runtime.InteropServices;
using Platform.Exceptions;

// ReSharper disable AssignmentInConditionalExpression

// ReSharper disable BuiltInTypeReferenceStyle
// ReSharper disable StaticFieldInGenericType

Expand All @@ -13,43 +13,31 @@ public class CachedTypeInfo<T>
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;
Expand All @@ -61,8 +49,9 @@ static CachedTypeInfo()
SignedVersion = signedVersion;
UnsignedVersion = unsignedVersion;
}
catch (Exception)
catch (Exception exception)
{
exception.Ignore();
}
}

Expand Down
2 changes: 2 additions & 0 deletions DynamicExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ public static bool HasProperty(this object @object, string propertyName)
{
var type = @object.GetType();
if (type == typeof(ExpandoObject))
{
return ((IDictionary<string, object>)@object).ContainsKey(propertyName);
}
return type.GetProperty(propertyName) != null;
}
}
Expand Down
6 changes: 5 additions & 1 deletion Platform.Reflection.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<Description>LinksPlatform's Platform.Reflection Class Library</Description>
<Copyright>Konstantin Diachenko</Copyright>
<AssemblyTitle>Platform.Reflection</AssemblyTitle>
<VersionPrefix>0.0.2</VersionPrefix>
<VersionPrefix>0.0.3</VersionPrefix>
<Authors>Konstantin Diachenko</Authors>
<TargetFramework>netstandard2.0</TargetFramework>
<AssemblyName>Platform.Reflection</AssemblyName>
Expand All @@ -21,6 +21,10 @@
<GenerateAssemblyProductAttribute>false</GenerateAssemblyProductAttribute>
<IncludeSymbols>true</IncludeSymbols>
<SymbolPackageFormat>snupkg</SymbolPackageFormat>
<PackageReleaseNotes>Platform.Collections updated to 0.0.3 version.
All exceptions are ignored correcly now.
Types classes use IList&lt;T&gt; (read-only) instead of T[] in public field.
Refactoring.</PackageReleaseNotes>
</PropertyGroup>

<ItemGroup>
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
57 changes: 24 additions & 33 deletions TypeExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using Platform.Collections;

namespace Platform.Reflection
{
Expand All @@ -12,19 +13,32 @@ public static class TypeExtensions
static private readonly HashSet<Type> IsNumericTypes;
static private readonly HashSet<Type> IsSignedTypes;
static private readonly HashSet<Type> IsFloatPointTypes;
static private readonly Dictionary<Type, Type> UnsignedVersionsOfSignedTypes;
static private readonly Dictionary<Type, Type> SignedVersionsOfUnsignedTypes;

static TypeExtensions()
{
CanBeNumericTypes = new HashSet<Type>() { typeof(bool), typeof(char), typeof(DateTime), typeof(TimeSpan) };
IsNumericTypes = new HashSet<Type>() { typeof(byte), typeof(ushort), typeof(uint), typeof(ulong) };
IsSignedTypes = new HashSet<Type>() { typeof(sbyte), typeof(short), typeof(int), typeof(long) };
IsFloatPointTypes = new HashSet<Type>() { typeof(decimal), typeof(double), typeof(float) };

CanBeNumericTypes = new HashSet<Type> { typeof(bool), typeof(char), typeof(DateTime), typeof(TimeSpan) };
IsNumericTypes = new HashSet<Type> { typeof(byte), typeof(ushort), typeof(uint), typeof(ulong) };
IsSignedTypes = new HashSet<Type> { typeof(sbyte), typeof(short), typeof(int), typeof(long) };
IsFloatPointTypes = new HashSet<Type> { typeof(decimal), typeof(double), typeof(float) };
UnsignedVersionsOfSignedTypes = new Dictionary<Type, Type>
{
{ typeof(sbyte), typeof(byte) },
{ typeof(short), typeof(ushort) },
{ typeof(int), typeof(uint) },
{ typeof(long), typeof(ulong) },
};
SignedVersionsOfUnsignedTypes = new Dictionary<Type, Type>
{
{ 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);
Expand All @@ -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;
}
Expand All @@ -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);

Expand Down
55 changes: 30 additions & 25 deletions Types.cs
Original file line number Diff line number Diff line change
@@ -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<Type, Type[]> Cache = new ConcurrentDictionary<Type, Type[]>();
private static readonly ConcurrentDictionary<Type, IList<Type>> Cache = new ConcurrentDictionary<Type, IList<Type>>();

protected Type[] ToArray()
protected IList<Type> ToReadOnlyList()
{
var array = GetType().GetGenericArguments();

var list = new List<Type>();
AppendTypes(list, array);
return list.ToArray();
var types = GetType().GetGenericArguments();
var result = new List<Type>();
AppendTypes(result, types);
return new ReadOnlyCollection<Type>(result);;
}

private void AppendTypes(List<Type> list, Type[] array)
private void AppendTypes(List<Type> container, IList<Type> 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<Type[]>());
else
list.Add(element);
var element = types[i];
if (element != typeof(Types))
{
if (element.IsSubclassOf(typeof(Types)))
{
AppendTypes(container, element.GetFirstField().GetStaticValue<IList<Type>>());
}
else
{
container.Add(element);
}
}
}
}

public static Type[] Get<T>()
public static IList<Type> Get<T>()
{
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<Type[]>();
return new[] { type };
{
return type.GetFirstField().GetStaticValue<IList<Type>>();
}
return Array.AsReadOnly(new[] { type });
});
}
}
Expand Down
4 changes: 2 additions & 2 deletions Types[T1, T2, T3, T4, T5, T6, T7].cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
using System;
using System.Collections.Generic;

namespace Platform.Reflection
{
public class Types<T1, T2, T3, T4, T5, T6, T7> : Types
{
public static readonly Type[] Array = new Types<T1, T2, T3, T4, T5, T6, T7>().ToArray();

public static readonly IList<Type> List = new Types<T1, T2, T3, T4, T5, T6, T7>().ToReadOnlyList();
private Types() { }
}
}
4 changes: 2 additions & 2 deletions Types[T1, T2, T3, T4, T5, T6].cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
using System;
using System.Collections.Generic;

namespace Platform.Reflection
{
public class Types<T1, T2, T3, T4, T5, T6> : Types
{
public static readonly Type[] Array = new Types<T1, T2, T3, T4, T5, T6>().ToArray();

public static readonly IList<Type> List = new Types<T1, T2, T3, T4, T5, T6>().ToReadOnlyList();
private Types() { }
}
}
4 changes: 2 additions & 2 deletions Types[T1, T2, T3, T4, T5].cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
using System;
using System.Collections.Generic;

namespace Platform.Reflection
{
public class Types<T1, T2, T3, T4, T5> : Types
{
public static readonly Type[] Array = new Types<T1, T2, T3, T4, T5>().ToArray();

public static readonly IList<Type> List = new Types<T1, T2, T3, T4, T5>().ToReadOnlyList();
private Types() { }
}
}
4 changes: 2 additions & 2 deletions Types[T1, T2, T3, T4].cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
using System;
using System.Collections.Generic;

namespace Platform.Reflection
{
public class Types<T1, T2, T3, T4> : Types
{
public static readonly Type[] Array = new Types<T1, T2, T3, T4>().ToArray();

public static readonly IList<Type> List = new Types<T1, T2, T3, T4>().ToReadOnlyList();
private Types() { }
}
}
4 changes: 2 additions & 2 deletions Types[T1, T2, T3].cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
using System;
using System.Collections.Generic;

namespace Platform.Reflection
{
public class Types<T1, T2, T3> : Types
{
public static readonly Type[] Array = new Types<T1, T2, T3>().ToArray();

public static readonly IList<Type> List = new Types<T1, T2, T3>().ToReadOnlyList();
private Types() { }
}
}
4 changes: 2 additions & 2 deletions Types[T1, T2].cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
using System;
using System.Collections.Generic;

namespace Platform.Reflection
{
public class Types<T1, T2> : Types
{
public static readonly Type[] Array = new Types<T1, T2>().ToArray();

public static readonly IList<Type> List = new Types<T1, T2>().ToReadOnlyList();
private Types() { }
}
}
4 changes: 2 additions & 2 deletions Types[T].cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
using System;
using System.Collections.Generic;

namespace Platform.Reflection
{
public class Types<T> : Types
{
public static readonly Type[] Array = new Types<T>().ToArray();

public static readonly IList<Type> Array = new Types<T>().ToReadOnlyList();
private Types() { }
}
}

0 comments on commit 8fd15b2

Please sign in to comment.