coding styles nullable

This commit is contained in:
BlubbFish 2019-12-08 19:54:52 +01:00
parent c78348324a
commit aa9fcd4a36
109 changed files with 12952 additions and 14462 deletions

View File

@ -3,50 +3,47 @@ using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
namespace Swan.Collections
{
namespace Swan.Collections {
/// <summary>
/// A thread-safe collection cache repository for types.
/// </summary>
/// <typeparam name="TValue">The type of member to cache.</typeparam>
public class CollectionCacheRepository<TValue> {
private readonly Lazy<ConcurrentDictionary<Type, IEnumerable<TValue>>> _data = new Lazy<ConcurrentDictionary<Type, IEnumerable<TValue>>>(() => new ConcurrentDictionary<Type, IEnumerable<TValue>>(), true);
/// <summary>
/// A thread-safe collection cache repository for types.
/// Determines whether the cache contains the specified key.
/// </summary>
/// <typeparam name="TValue">The type of member to cache.</typeparam>
public class CollectionCacheRepository<TValue>
{
private readonly Lazy<ConcurrentDictionary<Type, IEnumerable<TValue>>> _data =
new Lazy<ConcurrentDictionary<Type, IEnumerable<TValue>>>(() =>
new ConcurrentDictionary<Type, IEnumerable<TValue>>(), true);
/// <summary>
/// Determines whether the cache contains the specified key.
/// </summary>
/// <param name="key">The key.</param>
/// <returns><c>true</c> if the cache contains the key, otherwise <c>false</c>.</returns>
public bool ContainsKey(Type key) => _data.Value.ContainsKey(key);
/// <summary>
/// Retrieves the properties stored for the specified type.
/// If the properties are not available, it calls the factory method to retrieve them
/// and returns them as an array of PropertyInfo.
/// </summary>
/// <param name="key">The key.</param>
/// <param name="factory">The factory.</param>
/// <returns>
/// An array of the properties stored for the specified type.
/// </returns>
/// <exception cref="ArgumentNullException">
/// key
/// or
/// factory.
/// </exception>
/// <exception cref="System.ArgumentNullException">type.</exception>
public IEnumerable<TValue> Retrieve(Type key, Func<Type, IEnumerable<TValue>> factory)
{
if (key == null)
throw new ArgumentNullException(nameof(key));
if (factory == null)
throw new ArgumentNullException(nameof(factory));
return _data.Value.GetOrAdd(key, k => factory.Invoke(k).Where(item => item != null));
}
}
/// <param name="key">The key.</param>
/// <returns><c>true</c> if the cache contains the key, otherwise <c>false</c>.</returns>
public Boolean ContainsKey(Type key) => this._data.Value.ContainsKey(key);
/// <summary>
/// Retrieves the properties stored for the specified type.
/// If the properties are not available, it calls the factory method to retrieve them
/// and returns them as an array of PropertyInfo.
/// </summary>
/// <param name="key">The key.</param>
/// <param name="factory">The factory.</param>
/// <returns>
/// An array of the properties stored for the specified type.
/// </returns>
/// <exception cref="ArgumentNullException">
/// key
/// or
/// factory.
/// </exception>
/// <exception cref="System.ArgumentNullException">type.</exception>
public IEnumerable<TValue> Retrieve(Type key, Func<Type, IEnumerable<TValue>> factory) {
if(key == null) {
throw new ArgumentNullException(nameof(key));
}
if(factory == null) {
throw new ArgumentNullException(nameof(factory));
}
return this._data.Value.GetOrAdd(key, k => factory.Invoke(k).Where(item => item != null));
}
}
}

View File

@ -3,74 +3,75 @@ using System.Collections;
using System.Collections.Generic;
using Swan.Configuration;
namespace Swan.Collections
{
namespace Swan.Collections {
/// <summary>
/// <para>Implements a collection of components.</para>
/// <para>Each component in the collection may be given a unique name for later retrieval.</para>
/// </summary>
/// <typeparam name="T">The type of components in the collection.</typeparam>
/// <seealso cref="IComponentCollection{T}" />
public class ComponentCollection<T> : ConfiguredObject, IComponentCollection<T> {
private readonly List<T> _components = new List<T>();
private readonly List<(String, T)> _componentsWithSafeNames = new List<(String, T)>();
private readonly Dictionary<String, T> _namedComponents = new Dictionary<String, T>();
/// <inheritdoc />
public Int32 Count => this._components.Count;
/// <inheritdoc />
public IReadOnlyDictionary<String, T> Named => this._namedComponents;
/// <inheritdoc />
public IReadOnlyList<(String SafeName, T Component)> WithSafeNames => this._componentsWithSafeNames;
/// <inheritdoc />
public T this[Int32 index] => this._components[index];
/// <inheritdoc />
public T this[String key] => this._namedComponents[key];
/// <inheritdoc />
public IEnumerator<T> GetEnumerator() => this._components.GetEnumerator();
/// <inheritdoc />
IEnumerator IEnumerable.GetEnumerator() => ((IEnumerable)this._components).GetEnumerator();
/// <inheritdoc />
/// <exception cref="InvalidOperationException">The collection is locked.</exception>
public void Add(String name, T component) {
this.EnsureConfigurationNotLocked();
if(name != null) {
if(name.Length == 0) {
throw new ArgumentException("Component name is empty.", nameof(name));
}
if(this._namedComponents.ContainsKey(name)) {
throw new ArgumentException("Duplicate component name.", nameof(name));
}
}
if(component == null) {
throw new ArgumentNullException(nameof(component));
}
if(this._components.Contains(component)) {
throw new ArgumentException("Component has already been added.", nameof(component));
}
this._components.Add(component);
this._componentsWithSafeNames.Add((name ?? $"<{component.GetType().Name}>", component));
if(name != null) {
this._namedComponents.Add(name, component);
}
}
/// <summary>
/// <para>Implements a collection of components.</para>
/// <para>Each component in the collection may be given a unique name for later retrieval.</para>
/// Locks the collection, preventing further additions.
/// </summary>
/// <typeparam name="T">The type of components in the collection.</typeparam>
/// <seealso cref="IComponentCollection{T}" />
public class ComponentCollection<T> : ConfiguredObject, IComponentCollection<T>
{
private readonly List<T> _components = new List<T>();
private readonly List<(string, T)> _componentsWithSafeNames = new List<(string, T)>();
private readonly Dictionary<string, T> _namedComponents = new Dictionary<string, T>();
/// <inheritdoc />
public int Count => _components.Count;
/// <inheritdoc />
public IReadOnlyDictionary<string, T> Named => _namedComponents;
/// <inheritdoc />
public IReadOnlyList<(string SafeName, T Component)> WithSafeNames => _componentsWithSafeNames;
/// <inheritdoc />
public T this[int index] => _components[index];
/// <inheritdoc />
public T this[string key] => _namedComponents[key];
/// <inheritdoc />
public IEnumerator<T> GetEnumerator() => _components.GetEnumerator();
/// <inheritdoc />
IEnumerator IEnumerable.GetEnumerator() => ((IEnumerable)_components).GetEnumerator();
/// <inheritdoc />
/// <exception cref="InvalidOperationException">The collection is locked.</exception>
public void Add(string name, T component)
{
EnsureConfigurationNotLocked();
if (name != null)
{
if (name.Length == 0)
throw new ArgumentException("Component name is empty.", nameof(name));
if (_namedComponents.ContainsKey(name))
throw new ArgumentException("Duplicate component name.", nameof(name));
}
if (component == null)
throw new ArgumentNullException(nameof(component));
if (_components.Contains(component))
throw new ArgumentException("Component has already been added.", nameof(component));
_components.Add(component);
_componentsWithSafeNames.Add((name ?? $"<{component.GetType().Name}>", component));
if (name != null)
_namedComponents.Add(name, component);
}
/// <summary>
/// Locks the collection, preventing further additions.
/// </summary>
public void Lock() => LockConfiguration();
}
public void Lock() => this.LockConfiguration();
}
}

View File

@ -4,301 +4,266 @@ using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
namespace Swan.Collections
{
namespace Swan.Collections {
/// <summary>
/// Represents a thread-safe collection of key/value pairs that does not store null values
/// and can be accessed by multiple threads concurrently.
/// </summary>
/// <typeparam name="TKey">The type of keys in the dictionary. This must be a reference type.</typeparam>
/// <typeparam name="TValue">The type of values in the dictionary. This must be a reference type.</typeparam>
/// <seealso cref="IDataDictionary{TKey,TValue}"/>
public sealed class ConcurrentDataDictionary<TKey, TValue> : IDataDictionary<TKey, TValue> where TKey : class where TValue : class {
#region Private data
private readonly ConcurrentDictionary<TKey, TValue> _dictionary;
#endregion
#region Instance management
/// <summary>
/// Represents a thread-safe collection of key/value pairs that does not store null values
/// and can be accessed by multiple threads concurrently.
/// Initializes a new instance of the <see cref="ConcurrentDataDictionary{TKey,TValue}"/> class
/// that is empty, has the default concurrency level, has the default initial capacity,
/// and uses the default comparer for <typeparamref name="TKey"/>.
/// </summary>
/// <typeparam name="TKey">The type of keys in the dictionary. This must be a reference type.</typeparam>
/// <typeparam name="TValue">The type of values in the dictionary. This must be a reference type.</typeparam>
/// <seealso cref="IDataDictionary{TKey,TValue}"/>
public sealed class ConcurrentDataDictionary<TKey, TValue> : IDataDictionary<TKey, TValue>
where TKey : class
where TValue : class
{
#region Private data
private readonly ConcurrentDictionary<TKey, TValue> _dictionary;
#endregion
#region Instance management
/// <summary>
/// Initializes a new instance of the <see cref="ConcurrentDataDictionary{TKey,TValue}"/> class
/// that is empty, has the default concurrency level, has the default initial capacity,
/// and uses the default comparer for <typeparamref name="TKey"/>.
/// </summary>
/// <see cref="ConcurrentDictionary{TKey,TValue}()"/>
public ConcurrentDataDictionary()
{
_dictionary = new ConcurrentDictionary<TKey, TValue>();
}
/// <summary>
/// Initializes a new instance of the <see cref="ConcurrentDataDictionary{TKey,TValue}"/> class
/// that contains elements copied from the specified <see cref="IEnumerable{T}"/>, has the default concurrency level,
/// has the default initial capacity, and uses the default comparer for <typeparamref name="TKey"/>.
/// </summary>
/// <param name="collection">The <see cref="IEnumerable{T}"/> whose elements are copied
/// to the new <see cref="ConcurrentDataDictionary{TKey,TValue}"/>.</param>
/// <exception cref="ArgumentNullException"><paramref name="collection"/> is <see langword="null"/>.</exception>
/// <remarks>
/// <para>Since <see cref="ConcurrentDataDictionary{TKey,TValue}"/> does not store null values,
/// key/value pairs whose value is <see langword="null"/> will not be copied from <paramref name="collection"/>.</para>
/// </remarks>
/// <see cref="ConcurrentDictionary{TKey,TValue}(IEnumerable{KeyValuePair{TKey,TValue}})"/>
public ConcurrentDataDictionary(IEnumerable<KeyValuePair<TKey, TValue>> collection)
{
if (collection == null)
throw new ArgumentNullException(nameof(collection));
_dictionary = new ConcurrentDictionary<TKey, TValue>(collection.Where(pair => pair.Value != null));
}
/// <summary>
/// Initializes a new instance of the <see cref="ConcurrentDataDictionary{TKey,TValue}"/> class
/// that is empty, has the default concurrency level and capacity, and uses the specified <see cref="IEqualityComparer{T}"/>.
/// </summary>
/// <param name="comparer">The equality comparison implementation to use when comparing keys.</param>
/// <exception cref="ArgumentNullException"><paramref name="comparer"/> is <see langword="null"/>.</exception>
/// <see cref="ConcurrentDictionary{TKey,TValue}(IEqualityComparer{TKey})"/>
public ConcurrentDataDictionary(IEqualityComparer<TKey> comparer)
{
_dictionary = new ConcurrentDictionary<TKey, TValue>(comparer);
}
/// <summary>
/// Initializes a new instance of the <see cref="ConcurrentDataDictionary{TKey, TValue}"/> class
/// that contains elements copied from the specified <see cref="IEnumerable{T}"/>, has the default concurrency level,
/// has the default initial capacity, and uses the specified <see cref="IEqualityComparer{T}"/>.
/// </summary>
/// <param name="collection">The <see cref="IEnumerable{T}"/> whose elements are copied
/// to the new <see cref="ConcurrentDataDictionary{TKey,TValue}"/>.</param>
/// <param name="comparer">The equality comparison implementation to use when comparing keys.</param>
/// <remarks>
/// <para>Since <see cref="ConcurrentDataDictionary{TKey,TValue}"/> does not store null values,
/// key/value pairs whose value is <see langword="null"/> will not be copied from <paramref name="collection"/>.</para>
/// </remarks>
/// <exception cref="ArgumentNullException">
/// <para><paramref name="collection"/> is <see langword="null"/>.</para>
/// <para>- or -.</para>
/// <para><paramref name="comparer"/> is <see langword="null"/>.</para>
/// </exception>
/// <see cref="ConcurrentDictionary{TKey,TValue}(IEnumerable{KeyValuePair{TKey,TValue}},IEqualityComparer{TKey})"/>
public ConcurrentDataDictionary(IEnumerable<KeyValuePair<TKey, TValue>> collection, IEqualityComparer<TKey> comparer)
{
if (collection == null)
throw new ArgumentNullException(nameof(collection));
_dictionary = new ConcurrentDictionary<TKey, TValue>(collection.Where(pair => pair.Value != null), comparer);
}
/// <summary>
/// Initializes a new instance of the <see cref="ConcurrentDataDictionary{TKey, TValue}"/> class
/// that is empty, has the specified concurrency level and capacity, and uses the default comparer for the key type.
/// </summary>
/// <param name="concurrencyLevel">The estimated number of threads that will update
/// the <see cref="ConcurrentDataDictionary{TKey, TValue}"/> concurrently.</param>
/// <param name="capacity">The initial number of elements that the <see cref="ConcurrentDataDictionary{TKey, TValue}"/> can contain.</param>
/// <exception cref="ArgumentOutOfRangeException">
/// <para><paramref name="concurrencyLevel"/> is less than 1.</para>
/// <para>- or -.</para>
/// <para><paramref name="capacity"/> is less than 0.</para>
/// </exception>
/// <see cref="ConcurrentDictionary{TKey,TValue}(int,int)"/>
public ConcurrentDataDictionary(int concurrencyLevel, int capacity)
{
_dictionary = new ConcurrentDictionary<TKey, TValue>(concurrencyLevel, capacity);
}
/// <summary>
/// Initializes a new instance of the <see cref="ConcurrentDataDictionary{TKey, TValue}"/> class
/// that contains elements copied from the specified <see cref="IEnumerable{T}"/>, has the specified concurrency level,
/// has the default initial capacity, and uses the specified <see cref="IEqualityComparer{T}"/>.
/// </summary>
/// <param name="concurrencyLevel">The estimated number of threads that will update
/// the <see cref="ConcurrentDataDictionary{TKey, TValue}"/> concurrently.</param>
/// <param name="collection">The <see cref="IEnumerable{T}"/> whose elements are copied
/// to the new <see cref="ConcurrentDataDictionary{TKey,TValue}"/>.</param>
/// <param name="comparer">The equality comparison implementation to use when comparing keys.</param>
/// <remarks>
/// <para>Since <see cref="ConcurrentDataDictionary{TKey,TValue}"/> does not store null values,
/// key/value pairs whose value is <see langword="null"/> will not be copied from <paramref name="collection"/>.</para>
/// </remarks>
/// <exception cref="ArgumentNullException">
/// <para><paramref name="collection"/> is <see langword="null"/>.</para>
/// <para>- or -.</para>
/// <para><paramref name="comparer"/> is <see langword="null"/>.</para>
/// </exception>
/// <exception cref="ArgumentOutOfRangeException"><paramref name="concurrencyLevel"/> is less than 1.</exception>
/// <see cref="ConcurrentDictionary{TKey,TValue}(int,IEnumerable{KeyValuePair{TKey,TValue}},IEqualityComparer{TKey})"/>
public ConcurrentDataDictionary(int concurrencyLevel, IEnumerable<KeyValuePair<TKey, TValue>> collection, IEqualityComparer<TKey> comparer)
{
if (collection == null)
throw new ArgumentNullException(nameof(collection));
_dictionary = new ConcurrentDictionary<TKey, TValue>(
concurrencyLevel,
collection.Where(pair => pair.Value != null),
comparer);
}
#endregion
#region Public APIs
/// <inheritdoc cref="IDataDictionary{TKey,TValue}.Count"/>
public int Count => _dictionary.Count;
/// <inheritdoc cref="IDataDictionary{TKey,TValue}.IsEmpty"/>
public bool IsEmpty => _dictionary.IsEmpty;
/// <inheritdoc cref="IDictionary{TKey,TValue}.Keys"/>
public ICollection<TKey> Keys => _dictionary.Keys;
/// <inheritdoc cref="IDictionary{TKey,TValue}.Values"/>
public ICollection<TValue> Values => _dictionary.Values;
/// <inheritdoc cref="IDataDictionary{TKey,TValue}.this"/>
public TValue? this[TKey key]
{
get => _dictionary.TryGetValue(key ?? throw new ArgumentNullException(nameof(key)), out var value) ? value : null;
set
{
if (value != null)
{
_dictionary[key] = value;
}
else
{
_dictionary.TryRemove(key, out _);
}
}
}
/// <inheritdoc cref="IDataDictionary{TKey,TValue}.Clear"/>
public void Clear() => _dictionary.Clear();
/// <inheritdoc cref="IDataDictionary{TKey,TValue}.ContainsKey"/>
public bool ContainsKey(TKey key) => _dictionary.ContainsKey(key);
/// <inheritdoc cref="ConcurrentDictionary{TKey,TValue}.GetOrAdd(TKey,TValue)"/>
public TValue? GetOrAdd(TKey key, TValue value)
{
if (key == null)
throw new ArgumentNullException(nameof(key));
if (value != null)
return _dictionary.GetOrAdd(key, value);
return _dictionary.TryGetValue(key, out var retrievedValue) ? retrievedValue : null;
}
/// <inheritdoc cref="IDictionary{TKey,TValue}.Remove(TKey)"/>
public bool Remove(TKey key) => _dictionary.TryRemove(key, out _);
/// <inheritdoc cref="ConcurrentDictionary{TKey,TValue}.TryAdd"/>
public bool TryAdd(TKey key, TValue value)
{
if (key == null)
throw new ArgumentNullException(nameof(key));
return value == null || _dictionary.TryAdd(key, value);
}
/// <inheritdoc cref="IDataDictionary{TKey,TValue}.TryGetValue"/>
public bool TryGetValue(TKey key, out TValue value) => _dictionary.TryGetValue(key, out value);
/// <inheritdoc cref="IDataDictionary{TKey,TValue}.TryRemove"/>
public bool TryRemove(TKey key, out TValue value) => _dictionary.TryRemove(key, out value);
/// <inheritdoc cref="ConcurrentDictionary{TKey,TValue}.TryUpdate"/>
public bool TryUpdate(TKey key, TValue newValue, TValue comparisonValue)
{
if (key == null)
throw new ArgumentNullException(nameof(key));
return newValue != null && comparisonValue != null && _dictionary.TryUpdate(key, newValue, comparisonValue);
}
#endregion
#region Implementation of IDictionary<TKey, TValue>
/// <inheritdoc cref="IDictionary{TKey,TValue}.Add(TKey,TValue)"/>
void IDictionary<TKey, TValue>.Add(TKey key, TValue value)
{
if (value != null)
{
((IDictionary<TKey, TValue>)_dictionary).Add(key, value);
}
else
{
_dictionary.TryRemove(key, out _);
}
}
#endregion
#region Implementation of IReadOnlyDictionary<TKey, TValue>
/// <inheritdoc cref="IReadOnlyDictionary{TKey,TValue}.Keys"/>
IEnumerable<TKey> IReadOnlyDictionary<TKey, TValue>.Keys => _dictionary.Keys;
/// <inheritdoc cref="IReadOnlyDictionary{TKey,TValue}.Values"/>
IEnumerable<TValue> IReadOnlyDictionary<TKey, TValue>.Values => _dictionary.Values;
#endregion
#region Implementation of ICollection<KeyValuePair<TKey, TValue>>
/// <inheritdoc cref="ICollection{T}.IsReadOnly"/>
/// <remarks>
/// <para>This property is always <see langword="false"/> for a <see cref="ConcurrentDataDictionary{TKey,TValue}"/>.</para>
/// </remarks>
bool ICollection<KeyValuePair<TKey, TValue>>.IsReadOnly => false;
/// <inheritdoc cref="ICollection{T}.Add"/>
void ICollection<KeyValuePair<TKey, TValue>>.Add(KeyValuePair<TKey, TValue> item)
{
if (item.Value != null)
{
((ICollection<KeyValuePair<TKey, TValue>>)_dictionary).Add(item);
}
else
{
_dictionary.TryRemove(item.Key, out _);
}
}
/// <inheritdoc cref="ICollection{T}.Contains"/>
bool ICollection<KeyValuePair<TKey, TValue>>.Contains(KeyValuePair<TKey, TValue> item)
=> ((ICollection<KeyValuePair<TKey, TValue>>)_dictionary).Contains(item);
/// <inheritdoc cref="ICollection{T}.CopyTo"/>
void ICollection<KeyValuePair<TKey, TValue>>.CopyTo(KeyValuePair<TKey, TValue>[] array, int arrayIndex)
=> ((ICollection<KeyValuePair<TKey, TValue>>)_dictionary).CopyTo(array, arrayIndex);
/// <inheritdoc cref="ICollection{T}.Remove"/>
bool ICollection<KeyValuePair<TKey, TValue>>.Remove(KeyValuePair<TKey, TValue> item)
=> ((ICollection<KeyValuePair<TKey, TValue>>)_dictionary).Remove(item);
#endregion
#region Implementation of IEnumerable<KeyValuePair<TKey, TValue>>
/// <inheritdoc cref="IEnumerable{T}.GetEnumerator"/>
IEnumerator<KeyValuePair<TKey, TValue>> IEnumerable<KeyValuePair<TKey, TValue>>.GetEnumerator() => _dictionary.GetEnumerator();
#endregion
#region Implementation of IEnumerable
/// <inheritdoc cref="IEnumerable.GetEnumerator"/>
IEnumerator IEnumerable.GetEnumerator() => ((IEnumerable)_dictionary).GetEnumerator();
#endregion
}
/// <see cref="ConcurrentDictionary{TKey,TValue}()"/>
public ConcurrentDataDictionary() => this._dictionary = new ConcurrentDictionary<TKey, TValue>();
/// <summary>
/// Initializes a new instance of the <see cref="ConcurrentDataDictionary{TKey,TValue}"/> class
/// that contains elements copied from the specified <see cref="IEnumerable{T}"/>, has the default concurrency level,
/// has the default initial capacity, and uses the default comparer for <typeparamref name="TKey"/>.
/// </summary>
/// <param name="collection">The <see cref="IEnumerable{T}"/> whose elements are copied
/// to the new <see cref="ConcurrentDataDictionary{TKey,TValue}"/>.</param>
/// <exception cref="ArgumentNullException"><paramref name="collection"/> is <see langword="null"/>.</exception>
/// <remarks>
/// <para>Since <see cref="ConcurrentDataDictionary{TKey,TValue}"/> does not store null values,
/// key/value pairs whose value is <see langword="null"/> will not be copied from <paramref name="collection"/>.</para>
/// </remarks>
/// <see cref="ConcurrentDictionary{TKey,TValue}(IEnumerable{KeyValuePair{TKey,TValue}})"/>
public ConcurrentDataDictionary(IEnumerable<KeyValuePair<TKey, TValue>> collection) {
if(collection == null) {
throw new ArgumentNullException(nameof(collection));
}
this._dictionary = new ConcurrentDictionary<TKey, TValue>(collection.Where(pair => pair.Value != null));
}
/// <summary>
/// Initializes a new instance of the <see cref="ConcurrentDataDictionary{TKey,TValue}"/> class
/// that is empty, has the default concurrency level and capacity, and uses the specified <see cref="IEqualityComparer{T}"/>.
/// </summary>
/// <param name="comparer">The equality comparison implementation to use when comparing keys.</param>
/// <exception cref="ArgumentNullException"><paramref name="comparer"/> is <see langword="null"/>.</exception>
/// <see cref="ConcurrentDictionary{TKey,TValue}(IEqualityComparer{TKey})"/>
public ConcurrentDataDictionary(IEqualityComparer<TKey> comparer) => this._dictionary = new ConcurrentDictionary<TKey, TValue>(comparer);
/// <summary>
/// Initializes a new instance of the <see cref="ConcurrentDataDictionary{TKey, TValue}"/> class
/// that contains elements copied from the specified <see cref="IEnumerable{T}"/>, has the default concurrency level,
/// has the default initial capacity, and uses the specified <see cref="IEqualityComparer{T}"/>.
/// </summary>
/// <param name="collection">The <see cref="IEnumerable{T}"/> whose elements are copied
/// to the new <see cref="ConcurrentDataDictionary{TKey,TValue}"/>.</param>
/// <param name="comparer">The equality comparison implementation to use when comparing keys.</param>
/// <remarks>
/// <para>Since <see cref="ConcurrentDataDictionary{TKey,TValue}"/> does not store null values,
/// key/value pairs whose value is <see langword="null"/> will not be copied from <paramref name="collection"/>.</para>
/// </remarks>
/// <exception cref="ArgumentNullException">
/// <para><paramref name="collection"/> is <see langword="null"/>.</para>
/// <para>- or -.</para>
/// <para><paramref name="comparer"/> is <see langword="null"/>.</para>
/// </exception>
/// <see cref="ConcurrentDictionary{TKey,TValue}(IEnumerable{KeyValuePair{TKey,TValue}},IEqualityComparer{TKey})"/>
public ConcurrentDataDictionary(IEnumerable<KeyValuePair<TKey, TValue>> collection, IEqualityComparer<TKey> comparer) {
if(collection == null) {
throw new ArgumentNullException(nameof(collection));
}
this._dictionary = new ConcurrentDictionary<TKey, TValue>(collection.Where(pair => pair.Value != null), comparer);
}
/// <summary>
/// Initializes a new instance of the <see cref="ConcurrentDataDictionary{TKey, TValue}"/> class
/// that is empty, has the specified concurrency level and capacity, and uses the default comparer for the key type.
/// </summary>
/// <param name="concurrencyLevel">The estimated number of threads that will update
/// the <see cref="ConcurrentDataDictionary{TKey, TValue}"/> concurrently.</param>
/// <param name="capacity">The initial number of elements that the <see cref="ConcurrentDataDictionary{TKey, TValue}"/> can contain.</param>
/// <exception cref="ArgumentOutOfRangeException">
/// <para><paramref name="concurrencyLevel"/> is less than 1.</para>
/// <para>- or -.</para>
/// <para><paramref name="capacity"/> is less than 0.</para>
/// </exception>
/// <see cref="ConcurrentDictionary{TKey,TValue}(Int32,Int32)"/>
public ConcurrentDataDictionary(Int32 concurrencyLevel, Int32 capacity) => this._dictionary = new ConcurrentDictionary<TKey, TValue>(concurrencyLevel, capacity);
/// <summary>
/// Initializes a new instance of the <see cref="ConcurrentDataDictionary{TKey, TValue}"/> class
/// that contains elements copied from the specified <see cref="IEnumerable{T}"/>, has the specified concurrency level,
/// has the default initial capacity, and uses the specified <see cref="IEqualityComparer{T}"/>.
/// </summary>
/// <param name="concurrencyLevel">The estimated number of threads that will update
/// the <see cref="ConcurrentDataDictionary{TKey, TValue}"/> concurrently.</param>
/// <param name="collection">The <see cref="IEnumerable{T}"/> whose elements are copied
/// to the new <see cref="ConcurrentDataDictionary{TKey,TValue}"/>.</param>
/// <param name="comparer">The equality comparison implementation to use when comparing keys.</param>
/// <remarks>
/// <para>Since <see cref="ConcurrentDataDictionary{TKey,TValue}"/> does not store null values,
/// key/value pairs whose value is <see langword="null"/> will not be copied from <paramref name="collection"/>.</para>
/// </remarks>
/// <exception cref="ArgumentNullException">
/// <para><paramref name="collection"/> is <see langword="null"/>.</para>
/// <para>- or -.</para>
/// <para><paramref name="comparer"/> is <see langword="null"/>.</para>
/// </exception>
/// <exception cref="ArgumentOutOfRangeException"><paramref name="concurrencyLevel"/> is less than 1.</exception>
/// <see cref="ConcurrentDictionary{TKey,TValue}(Int32,IEnumerable{KeyValuePair{TKey,TValue}},IEqualityComparer{TKey})"/>
public ConcurrentDataDictionary(Int32 concurrencyLevel, IEnumerable<KeyValuePair<TKey, TValue>> collection, IEqualityComparer<TKey> comparer) {
if(collection == null) {
throw new ArgumentNullException(nameof(collection));
}
this._dictionary = new ConcurrentDictionary<TKey, TValue>(concurrencyLevel, collection.Where(pair => pair.Value != null), comparer);
}
#endregion
#region Public APIs
/// <inheritdoc cref="IDataDictionary{TKey,TValue}.Count"/>
public Int32 Count => this._dictionary.Count;
/// <inheritdoc cref="IDataDictionary{TKey,TValue}.IsEmpty"/>
public Boolean IsEmpty => this._dictionary.IsEmpty;
/// <inheritdoc cref="IDictionary{TKey,TValue}.Keys"/>
public ICollection<TKey> Keys => this._dictionary.Keys;
/// <inheritdoc cref="IDictionary{TKey,TValue}.Values"/>
public ICollection<TValue> Values => this._dictionary.Values;
/// <inheritdoc cref="IDataDictionary{TKey,TValue}.this"/>
public TValue this[TKey key] {
get => this._dictionary.TryGetValue(key ?? throw new ArgumentNullException(nameof(key)), out TValue value) ? value : null;
set {
if(value != null) {
this._dictionary[key] = value;
} else {
_ = this._dictionary.TryRemove(key, out _);
}
}
}
/// <inheritdoc cref="IDataDictionary{TKey,TValue}.Clear"/>
public void Clear() => this._dictionary.Clear();
/// <inheritdoc cref="IDataDictionary{TKey,TValue}.ContainsKey"/>
public Boolean ContainsKey(TKey key) => this._dictionary.ContainsKey(key);
/// <inheritdoc cref="ConcurrentDictionary{TKey,TValue}.GetOrAdd(TKey,TValue)"/>
public TValue GetOrAdd(TKey key, TValue value) {
if(key == null) {
throw new ArgumentNullException(nameof(key));
}
return value != null ? this._dictionary.GetOrAdd(key, value) : this._dictionary.TryGetValue(key, out TValue retrievedValue) ? retrievedValue : null;
}
/// <inheritdoc cref="IDictionary{TKey,TValue}.Remove(TKey)"/>
public Boolean Remove(TKey key) => this._dictionary.TryRemove(key, out _);
/// <inheritdoc cref="ConcurrentDictionary{TKey,TValue}.TryAdd"/>
public Boolean TryAdd(TKey key, TValue value) {
if(key == null) {
throw new ArgumentNullException(nameof(key));
}
return value == null || this._dictionary.TryAdd(key, value);
}
/// <inheritdoc cref="IDataDictionary{TKey,TValue}.TryGetValue"/>
public Boolean TryGetValue(TKey key, out TValue value) => this._dictionary.TryGetValue(key, out value);
/// <inheritdoc cref="IDataDictionary{TKey,TValue}.TryRemove"/>
public Boolean TryRemove(TKey key, out TValue value) => this._dictionary.TryRemove(key, out value);
/// <inheritdoc cref="ConcurrentDictionary{TKey,TValue}.TryUpdate"/>
public Boolean TryUpdate(TKey key, TValue newValue, TValue comparisonValue) {
if(key == null) {
throw new ArgumentNullException(nameof(key));
}
return newValue != null && comparisonValue != null && this._dictionary.TryUpdate(key, newValue, comparisonValue);
}
#endregion
#region Implementation of IDictionary<TKey, TValue>
/// <inheritdoc cref="IDictionary{TKey,TValue}.Add(TKey,TValue)"/>
void IDictionary<TKey, TValue>.Add(TKey key, TValue value) {
if(value != null) {
((IDictionary<TKey, TValue>)this._dictionary).Add(key, value);
} else {
_ = this._dictionary.TryRemove(key, out _);
}
}
#endregion
#region Implementation of IReadOnlyDictionary<TKey, TValue>
/// <inheritdoc cref="IReadOnlyDictionary{TKey,TValue}.Keys"/>
IEnumerable<TKey> IReadOnlyDictionary<TKey, TValue>.Keys => this._dictionary.Keys;
/// <inheritdoc cref="IReadOnlyDictionary{TKey,TValue}.Values"/>
IEnumerable<TValue> IReadOnlyDictionary<TKey, TValue>.Values => this._dictionary.Values;
#endregion
#region Implementation of ICollection<KeyValuePair<TKey, TValue>>
/// <inheritdoc cref="ICollection{T}.IsReadOnly"/>
/// <remarks>
/// <para>This property is always <see langword="false"/> for a <see cref="ConcurrentDataDictionary{TKey,TValue}"/>.</para>
/// </remarks>
Boolean ICollection<KeyValuePair<TKey, TValue>>.IsReadOnly => false;
/// <inheritdoc cref="ICollection{T}.Add"/>
void ICollection<KeyValuePair<TKey, TValue>>.Add(KeyValuePair<TKey, TValue> item) {
if(item.Value != null) {
((ICollection<KeyValuePair<TKey, TValue>>)this._dictionary).Add(item);
} else {
_ = this._dictionary.TryRemove(item.Key, out _);
}
}
/// <inheritdoc cref="ICollection{T}.Contains"/>
Boolean ICollection<KeyValuePair<TKey, TValue>>.Contains(KeyValuePair<TKey, TValue> item) => ((ICollection<KeyValuePair<TKey, TValue>>)this._dictionary).Contains(item);
/// <inheritdoc cref="ICollection{T}.CopyTo"/>
void ICollection<KeyValuePair<TKey, TValue>>.CopyTo(KeyValuePair<TKey, TValue>[] array, Int32 arrayIndex) => ((ICollection<KeyValuePair<TKey, TValue>>)this._dictionary).CopyTo(array, arrayIndex);
/// <inheritdoc cref="ICollection{T}.Remove"/>
Boolean ICollection<KeyValuePair<TKey, TValue>>.Remove(KeyValuePair<TKey, TValue> item) => ((ICollection<KeyValuePair<TKey, TValue>>)this._dictionary).Remove(item);
#endregion
#region Implementation of IEnumerable<KeyValuePair<TKey, TValue>>
/// <inheritdoc cref="IEnumerable{T}.GetEnumerator"/>
IEnumerator<KeyValuePair<TKey, TValue>> IEnumerable<KeyValuePair<TKey, TValue>>.GetEnumerator() => this._dictionary.GetEnumerator();
#endregion
#region Implementation of IEnumerable
/// <inheritdoc cref="IEnumerable.GetEnumerator"/>
IEnumerator IEnumerable.GetEnumerator() => ((IEnumerable)this._dictionary).GetEnumerator();
#endregion
}
}

View File

@ -1,341 +1,312 @@
using System;

using System;
using System.Collections;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
namespace Swan.Collections
{
namespace Swan.Collections {
/// <summary>
/// Represents a non-thread-safe collection of key/value pairs that does not store null values.
/// </summary>
/// <typeparam name="TKey">The type of keys in the dictionary. This must be a reference type.</typeparam>
/// <typeparam name="TValue">The type of values in the dictionary. This must be a reference type.</typeparam>
/// <seealso cref="IDataDictionary{TKey,TValue}"/>
public sealed class DataDictionary<TKey, TValue> : IDataDictionary<TKey, TValue>
where TKey : class
where TValue : class {
#region Private data
private readonly Dictionary<TKey, TValue> _dictionary;
#endregion
#region Instance management
/// <summary>
/// Represents a non-thread-safe collection of key/value pairs that does not store null values.
/// Initializes a new instance of the <see cref="DataDictionary{TKey,TValue}"/> class
/// that is empty, has the default initial capacity,
/// and uses the default comparer for <typeparamref name="TKey"/>.
/// </summary>
/// <typeparam name="TKey">The type of keys in the dictionary. This must be a reference type.</typeparam>
/// <typeparam name="TValue">The type of values in the dictionary. This must be a reference type.</typeparam>
/// <seealso cref="IDataDictionary{TKey,TValue}"/>
public sealed class DataDictionary<TKey, TValue> : IDataDictionary<TKey, TValue>
where TKey : class
where TValue : class
{
#region Private data
private readonly Dictionary<TKey, TValue> _dictionary;
#endregion
#region Instance management
/// <summary>
/// Initializes a new instance of the <see cref="DataDictionary{TKey,TValue}"/> class
/// that is empty, has the default initial capacity,
/// and uses the default comparer for <typeparamref name="TKey"/>.
/// </summary>
/// <see cref="Dictionary{TKey,TValue}()"/>
public DataDictionary()
{
_dictionary = new Dictionary<TKey, TValue>();
}
/// <summary>
/// Initializes a new instance of the <see cref="DataDictionary{TKey,TValue}"/> class
/// that contains elements copied from the specified <see cref="IEnumerable{T}"/>,
/// has the default initial capacity, and uses the default comparer for <typeparamref name="TKey"/>.
/// </summary>
/// <param name="collection">The <see cref="IEnumerable{T}"/> whose elements are copied
/// to the new <see cref="DataDictionary{TKey,TValue}"/>.</param>
/// <exception cref="ArgumentNullException"><paramref name="collection"/> is <see langword="null"/>.</exception>
/// <remarks>
/// <para>Since <see cref="DataDictionary{TKey,TValue}"/> does not store null values,
/// key/value pairs whose value is <see langword="null"/> will not be copied from <paramref name="collection"/>.</para>
/// </remarks>
/// <see cref="Dictionary{TKey,TValue}()"/>
public DataDictionary(IEnumerable<KeyValuePair<TKey, TValue>> collection)
{
if (collection == null)
throw new ArgumentNullException(nameof(collection));
_dictionary = new Dictionary<TKey, TValue>();
foreach (var pair in collection.Where(pair => pair.Value != null))
{
_dictionary.Add(pair.Key, pair.Value);
}
}
/// <summary>
/// Initializes a new instance of the <see cref="DataDictionary{TKey,TValue}"/> class
/// that is empty, has the default capacity, and uses the specified <see cref="IEqualityComparer{T}"/>.
/// </summary>
/// <param name="comparer">The equality comparison implementation to use when comparing keys.</param>
/// <exception cref="ArgumentNullException"><paramref name="comparer"/> is <see langword="null"/>.</exception>
/// <see cref="Dictionary{TKey,TValue}(IEqualityComparer{TKey})"/>
public DataDictionary(IEqualityComparer<TKey> comparer)
{
_dictionary = new Dictionary<TKey, TValue>(comparer);
}
/// <summary>
/// Initializes a new instance of the <see cref="DataDictionary{TKey, TValue}"/> class
/// that contains elements copied from the specified <see cref="IEnumerable{T}"/>,
/// has the default initial capacity, and uses the specified <see cref="IEqualityComparer{T}"/>.
/// </summary>
/// <param name="collection">The <see cref="IEnumerable{T}"/> whose elements are copied
/// to the new <see cref="DataDictionary{TKey,TValue}"/>.</param>
/// <param name="comparer">The equality comparison implementation to use when comparing keys.</param>
/// <remarks>
/// <para>Since <see cref="DataDictionary{TKey,TValue}"/> does not store null values,
/// key/value pairs whose value is <see langword="null"/> will not be copied from <paramref name="collection"/>.</para>
/// </remarks>
/// <exception cref="ArgumentNullException">
/// <para><paramref name="collection"/> is <see langword="null"/>.</para>
/// <para>- or -.</para>
/// <para><paramref name="comparer"/> is <see langword="null"/>.</para>
/// </exception>
/// <see cref="Dictionary{TKey,TValue}(IEqualityComparer{TKey})"/>
public DataDictionary(IEnumerable<KeyValuePair<TKey, TValue>> collection, IEqualityComparer<TKey> comparer)
{
if (collection == null)
throw new ArgumentNullException(nameof(collection));
_dictionary = new Dictionary<TKey, TValue>(comparer);
foreach (var pair in collection.Where(pair => pair.Value != null))
{
_dictionary.Add(pair.Key, pair.Value);
}
}
/// <summary>
/// Initializes a new instance of the <see cref="DataDictionary{TKey, TValue}"/> class
/// that is empty, has the specified capacity, and uses the default comparer for the key type.
/// </summary>
/// <param name="capacity">The initial number of elements that the <see cref="DataDictionary{TKey, TValue}"/> can contain.</param>
/// <exception cref="ArgumentOutOfRangeException"><paramref name="capacity"/> is less than 0.</exception>
/// <see cref="Dictionary{TKey,TValue}(int)"/>
public DataDictionary(int capacity)
{
_dictionary = new Dictionary<TKey, TValue>(capacity);
}
/// <summary>
/// Initializes a new instance of the <see cref="DataDictionary{TKey, TValue}"/> class
/// that contains elements copied from the specified <see cref="IEnumerable{T}"/>,
/// has the specified capacity, and uses the specified <see cref="IEqualityComparer{T}"/>.
/// </summary>
/// <param name="capacity">The initial number of elements that the <see cref="DataDictionary{TKey, TValue}"/> can contain.</param>
/// <param name="collection">The <see cref="IEnumerable{T}"/> whose elements are copied
/// to the new <see cref="ConcurrentDataDictionary{TKey,TValue}"/>.</param>
/// <param name="comparer">The equality comparison implementation to use when comparing keys.</param>
/// <remarks>
/// <para>Since <see cref="ConcurrentDataDictionary{TKey,TValue}"/> does not store null values,
/// key/value pairs whose value is <see langword="null"/> will not be copied from <paramref name="collection"/>.</para>
/// </remarks>
/// <exception cref="ArgumentNullException">
/// <para><paramref name="collection"/> is <see langword="null"/>.</para>
/// <para>- or -.</para>
/// <para><paramref name="comparer"/> is <see langword="null"/>.</para>
/// </exception>
/// <exception cref="ArgumentOutOfRangeException"><paramref name="capacity"/> is less than 0.</exception>
/// <see cref="Dictionary{TKey,TValue}(int,IEqualityComparer{TKey})"/>
public DataDictionary(int capacity, IEnumerable<KeyValuePair<TKey, TValue>> collection, IEqualityComparer<TKey> comparer)
{
if (collection == null)
throw new ArgumentNullException(nameof(collection));
_dictionary = new Dictionary<TKey, TValue>(capacity, comparer);
foreach (var pair in collection.Where(pair => pair.Value != null))
{
_dictionary.Add(pair.Key, pair.Value);
}
}
#endregion
#region Public APIs
/// <inheritdoc cref="IDataDictionary{TKey,TValue}.Count"/>
public int Count => _dictionary.Count;
/// <inheritdoc cref="IDataDictionary{TKey,TValue}.IsEmpty"/>
public bool IsEmpty => _dictionary.Count == 0;
/// <inheritdoc cref="IDictionary{TKey,TValue}.Keys"/>
public ICollection<TKey> Keys => _dictionary.Keys;
/// <inheritdoc cref="IDictionary{TKey,TValue}.Values"/>
public ICollection<TValue> Values => _dictionary.Values;
/// <inheritdoc cref="IDataDictionary{TKey,TValue}.this"/>
public TValue? this[TKey key]
{
get => _dictionary.TryGetValue(key ?? throw new ArgumentNullException(nameof(key)), out var value) ? value : null;
set
{
if (value != null)
{
_dictionary[key] = value;
}
else
{
_dictionary.Remove(key);
}
}
}
/// <inheritdoc cref="IDataDictionary{TKey,TValue}.Clear"/>
public void Clear() => _dictionary.Clear();
/// <inheritdoc cref="IDataDictionary{TKey,TValue}.ContainsKey"/>
public bool ContainsKey(TKey key)
{
// _dictionary.ContainsKey will take care of throwing on a null key.
return _dictionary.ContainsKey(key);
}
/// <inheritdoc cref="ConcurrentDictionary{TKey,TValue}.GetOrAdd(TKey,TValue)"/>
public TValue? GetOrAdd(TKey key, TValue value)
{
// _dictionary.TryGetValue will take care of throwing on a null key.
if (_dictionary.TryGetValue(key, out var result))
return result;
if (value == null)
return null;
_dictionary.Add(key, value);
return value;
}
/// <inheritdoc cref="IDictionary{TKey,TValue}.Remove(TKey)"/>
public bool Remove(TKey key)
{
// _dictionary.Remove will take care of throwing on a null key.
return _dictionary.Remove(key);
}
/// <inheritdoc cref="ConcurrentDictionary{TKey,TValue}.TryAdd"/>
public bool TryAdd(TKey key, TValue value)
{
// _dictionary.ContainsKey will take care of throwing on a null key.
if (_dictionary.ContainsKey(key))
return false;
if (value != null)
_dictionary.Add(key, value);
return true;
}
/// <inheritdoc cref="IDataDictionary{TKey,TValue}.TryGetValue"/>
public bool TryGetValue(TKey key, out TValue value) => _dictionary.TryGetValue(key, out value);
/// <inheritdoc cref="IDataDictionary{TKey,TValue}.TryRemove"/>
public bool TryRemove(TKey key, out TValue value)
{
// TryGetValue will take care of throwing on a null key.
if (!_dictionary.TryGetValue(key, out value))
return false;
_dictionary.Remove(key);
return true;
}
/// <inheritdoc cref="ConcurrentDictionary{TKey,TValue}.TryUpdate"/>
public bool TryUpdate(TKey key, TValue newValue, TValue comparisonValue)
{
// TryGetValue will take care of throwing on a null key.
if (!_dictionary.TryGetValue(key, out var value))
return false;
if (value != comparisonValue)
return false;
_dictionary[key] = newValue;
return true;
}
#endregion
#region Implementation of IDictionary<TKey, TValue>
/// <inheritdoc cref="IDictionary{TKey,TValue}.Add(TKey,TValue)"/>
void IDictionary<TKey, TValue>.Add(TKey key, TValue value)
{
// Validating the key seems redundant, because both Add and Remove
// will throw on a null key.
// This way, though, the code path on null key does not depend on value.
// Without this validation, there should be two unit tests for null key,
// one with a null value and one with a non-null value,
// which makes no sense.
if (key == null)
throw new ArgumentNullException(nameof(key));
if (value != null)
{
_dictionary.Add(key, value);
}
else
{
_dictionary.Remove(key);
}
}
#endregion
#region Implementation of IReadOnlyDictionary<TKey, TValue>
/// <inheritdoc cref="IReadOnlyDictionary{TKey,TValue}.Keys"/>
IEnumerable<TKey> IReadOnlyDictionary<TKey, TValue>.Keys => _dictionary.Keys;
/// <inheritdoc cref="IReadOnlyDictionary{TKey,TValue}.Values"/>
IEnumerable<TValue> IReadOnlyDictionary<TKey, TValue>.Values => _dictionary.Values;
#endregion
#region Implementation of ICollection<KeyValuePair<TKey, TValue>>
/// <inheritdoc cref="ICollection{T}.IsReadOnly"/>
/// <remarks>
/// <para>This property is always <see langword="false"/> for a <see cref="DataDictionary{TKey,TValue}"/>.</para>
/// </remarks>
bool ICollection<KeyValuePair<TKey, TValue>>.IsReadOnly => false;
/// <inheritdoc cref="ICollection{T}.Add"/>
void ICollection<KeyValuePair<TKey, TValue>>.Add(KeyValuePair<TKey, TValue> item)
{
if (item.Value != null)
{
((ICollection<KeyValuePair<TKey, TValue>>)_dictionary).Add(item);
}
else
{
_dictionary.Remove(item.Key);
}
}
/// <inheritdoc cref="ICollection{T}.Contains"/>
bool ICollection<KeyValuePair<TKey, TValue>>.Contains(KeyValuePair<TKey, TValue> item)
=> ((ICollection<KeyValuePair<TKey, TValue>>)_dictionary).Contains(item);
/// <inheritdoc cref="ICollection{T}.CopyTo"/>
void ICollection<KeyValuePair<TKey, TValue>>.CopyTo(KeyValuePair<TKey, TValue>[] array, int arrayIndex)
=> ((ICollection<KeyValuePair<TKey, TValue>>)_dictionary).CopyTo(array, arrayIndex);
/// <inheritdoc cref="ICollection{T}.Remove"/>
bool ICollection<KeyValuePair<TKey, TValue>>.Remove(KeyValuePair<TKey, TValue> item)
=> ((ICollection<KeyValuePair<TKey, TValue>>) _dictionary).Remove(item);
#endregion
#region Implementation of IEnumerable<KeyValuePair<TKey, TValue>>
/// <inheritdoc cref="IEnumerable{T}.GetEnumerator"/>
IEnumerator<KeyValuePair<TKey, TValue>> IEnumerable<KeyValuePair<TKey, TValue>>.GetEnumerator() => _dictionary.GetEnumerator();
#endregion
#region Implementation of IEnumerable
/// <inheritdoc cref="IEnumerable.GetEnumerator"/>
IEnumerator IEnumerable.GetEnumerator() => ((IEnumerable)_dictionary).GetEnumerator();
#endregion
}
/// <see cref="Dictionary{TKey,TValue}()"/>
public DataDictionary() => this._dictionary = new Dictionary<TKey, TValue>();
/// <summary>
/// Initializes a new instance of the <see cref="DataDictionary{TKey,TValue}"/> class
/// that contains elements copied from the specified <see cref="IEnumerable{T}"/>,
/// has the default initial capacity, and uses the default comparer for <typeparamref name="TKey"/>.
/// </summary>
/// <param name="collection">The <see cref="IEnumerable{T}"/> whose elements are copied
/// to the new <see cref="DataDictionary{TKey,TValue}"/>.</param>
/// <exception cref="ArgumentNullException"><paramref name="collection"/> is <see langword="null"/>.</exception>
/// <remarks>
/// <para>Since <see cref="DataDictionary{TKey,TValue}"/> does not store null values,
/// key/value pairs whose value is <see langword="null"/> will not be copied from <paramref name="collection"/>.</para>
/// </remarks>
/// <see cref="Dictionary{TKey,TValue}()"/>
public DataDictionary(IEnumerable<KeyValuePair<TKey, TValue>> collection) {
if(collection == null) {
throw new ArgumentNullException(nameof(collection));
}
this._dictionary = new Dictionary<TKey, TValue>();
foreach(KeyValuePair<TKey, TValue> pair in collection.Where(pair => pair.Value != null)) {
this._dictionary.Add(pair.Key, pair.Value);
}
}
/// <summary>
/// Initializes a new instance of the <see cref="DataDictionary{TKey,TValue}"/> class
/// that is empty, has the default capacity, and uses the specified <see cref="IEqualityComparer{T}"/>.
/// </summary>
/// <param name="comparer">The equality comparison implementation to use when comparing keys.</param>
/// <exception cref="ArgumentNullException"><paramref name="comparer"/> is <see langword="null"/>.</exception>
/// <see cref="Dictionary{TKey,TValue}(IEqualityComparer{TKey})"/>
public DataDictionary(IEqualityComparer<TKey> comparer) => this._dictionary = new Dictionary<TKey, TValue>(comparer);
/// <summary>
/// Initializes a new instance of the <see cref="DataDictionary{TKey, TValue}"/> class
/// that contains elements copied from the specified <see cref="IEnumerable{T}"/>,
/// has the default initial capacity, and uses the specified <see cref="IEqualityComparer{T}"/>.
/// </summary>
/// <param name="collection">The <see cref="IEnumerable{T}"/> whose elements are copied
/// to the new <see cref="DataDictionary{TKey,TValue}"/>.</param>
/// <param name="comparer">The equality comparison implementation to use when comparing keys.</param>
/// <remarks>
/// <para>Since <see cref="DataDictionary{TKey,TValue}"/> does not store null values,
/// key/value pairs whose value is <see langword="null"/> will not be copied from <paramref name="collection"/>.</para>
/// </remarks>
/// <exception cref="ArgumentNullException">
/// <para><paramref name="collection"/> is <see langword="null"/>.</para>
/// <para>- or -.</para>
/// <para><paramref name="comparer"/> is <see langword="null"/>.</para>
/// </exception>
/// <see cref="Dictionary{TKey,TValue}(IEqualityComparer{TKey})"/>
public DataDictionary(IEnumerable<KeyValuePair<TKey, TValue>> collection, IEqualityComparer<TKey> comparer) {
if(collection == null) {
throw new ArgumentNullException(nameof(collection));
}
this._dictionary = new Dictionary<TKey, TValue>(comparer);
foreach(KeyValuePair<TKey, TValue> pair in collection.Where(pair => pair.Value != null)) {
this._dictionary.Add(pair.Key, pair.Value);
}
}
/// <summary>
/// Initializes a new instance of the <see cref="DataDictionary{TKey, TValue}"/> class
/// that is empty, has the specified capacity, and uses the default comparer for the key type.
/// </summary>
/// <param name="capacity">The initial number of elements that the <see cref="DataDictionary{TKey, TValue}"/> can contain.</param>
/// <exception cref="ArgumentOutOfRangeException"><paramref name="capacity"/> is less than 0.</exception>
/// <see cref="Dictionary{TKey,TValue}(Int32)"/>
public DataDictionary(Int32 capacity) => this._dictionary = new Dictionary<TKey, TValue>(capacity);
/// <summary>
/// Initializes a new instance of the <see cref="DataDictionary{TKey, TValue}"/> class
/// that contains elements copied from the specified <see cref="IEnumerable{T}"/>,
/// has the specified capacity, and uses the specified <see cref="IEqualityComparer{T}"/>.
/// </summary>
/// <param name="capacity">The initial number of elements that the <see cref="DataDictionary{TKey, TValue}"/> can contain.</param>
/// <param name="collection">The <see cref="IEnumerable{T}"/> whose elements are copied
/// to the new <see cref="ConcurrentDataDictionary{TKey,TValue}"/>.</param>
/// <param name="comparer">The equality comparison implementation to use when comparing keys.</param>
/// <remarks>
/// <para>Since <see cref="ConcurrentDataDictionary{TKey,TValue}"/> does not store null values,
/// key/value pairs whose value is <see langword="null"/> will not be copied from <paramref name="collection"/>.</para>
/// </remarks>
/// <exception cref="ArgumentNullException">
/// <para><paramref name="collection"/> is <see langword="null"/>.</para>
/// <para>- or -.</para>
/// <para><paramref name="comparer"/> is <see langword="null"/>.</para>
/// </exception>
/// <exception cref="ArgumentOutOfRangeException"><paramref name="capacity"/> is less than 0.</exception>
/// <see cref="Dictionary{TKey,TValue}(Int32,IEqualityComparer{TKey})"/>
public DataDictionary(Int32 capacity, IEnumerable<KeyValuePair<TKey, TValue>> collection, IEqualityComparer<TKey> comparer) {
if(collection == null) {
throw new ArgumentNullException(nameof(collection));
}
this._dictionary = new Dictionary<TKey, TValue>(capacity, comparer);
foreach(KeyValuePair<TKey, TValue> pair in collection.Where(pair => pair.Value != null)) {
this._dictionary.Add(pair.Key, pair.Value);
}
}
#endregion
#region Public APIs
/// <inheritdoc cref="IDataDictionary{TKey,TValue}.Count"/>
public Int32 Count => this._dictionary.Count;
/// <inheritdoc cref="IDataDictionary{TKey,TValue}.IsEmpty"/>
public Boolean IsEmpty => this._dictionary.Count == 0;
/// <inheritdoc cref="IDictionary{TKey,TValue}.Keys"/>
public ICollection<TKey> Keys => this._dictionary.Keys;
/// <inheritdoc cref="IDictionary{TKey,TValue}.Values"/>
public ICollection<TValue> Values => this._dictionary.Values;
/// <inheritdoc cref="IDataDictionary{TKey,TValue}.this"/>
public TValue this[TKey key] {
get => this._dictionary.TryGetValue(key ?? throw new ArgumentNullException(nameof(key)), out TValue value) ? value : null;
set {
if(value != null) {
this._dictionary[key] = value;
} else {
_ = this._dictionary.Remove(key);
}
}
}
/// <inheritdoc cref="IDataDictionary{TKey,TValue}.Clear"/>
public void Clear() => this._dictionary.Clear();
/// <inheritdoc cref="IDataDictionary{TKey,TValue}.ContainsKey"/>
public Boolean ContainsKey(TKey key) =>
// _dictionary.ContainsKey will take care of throwing on a null key.
this._dictionary.ContainsKey(key);
/// <inheritdoc cref="ConcurrentDictionary{TKey,TValue}.GetOrAdd(TKey,TValue)"/>
public TValue GetOrAdd(TKey key, TValue value) {
// _dictionary.TryGetValue will take care of throwing on a null key.
if(this._dictionary.TryGetValue(key, out TValue result)) {
return result;
}
if(value == null) {
return null;
}
this._dictionary.Add(key, value);
return value;
}
/// <inheritdoc cref="IDictionary{TKey,TValue}.Remove(TKey)"/>
public Boolean Remove(TKey key) =>
// _dictionary.Remove will take care of throwing on a null key.
this._dictionary.Remove(key);
/// <inheritdoc cref="ConcurrentDictionary{TKey,TValue}.TryAdd"/>
public Boolean TryAdd(TKey key, TValue value) {
// _dictionary.ContainsKey will take care of throwing on a null key.
if(this._dictionary.ContainsKey(key)) {
return false;
}
if(value != null) {
this._dictionary.Add(key, value);
}
return true;
}
/// <inheritdoc cref="IDataDictionary{TKey,TValue}.TryGetValue"/>
public Boolean TryGetValue(TKey key, out TValue value) => this._dictionary.TryGetValue(key, out value);
/// <inheritdoc cref="IDataDictionary{TKey,TValue}.TryRemove"/>
public Boolean TryRemove(TKey key, out TValue value) {
// TryGetValue will take care of throwing on a null key.
if(!this._dictionary.TryGetValue(key, out value)) {
return false;
}
_ = this._dictionary.Remove(key);
return true;
}
/// <inheritdoc cref="ConcurrentDictionary{TKey,TValue}.TryUpdate"/>
public Boolean TryUpdate(TKey key, TValue newValue, TValue comparisonValue) {
// TryGetValue will take care of throwing on a null key.
if(!this._dictionary.TryGetValue(key, out TValue value)) {
return false;
}
if(value != comparisonValue) {
return false;
}
this._dictionary[key] = newValue;
return true;
}
#endregion
#region Implementation of IDictionary<TKey, TValue>
/// <inheritdoc cref="IDictionary{TKey,TValue}.Add(TKey,TValue)"/>
void IDictionary<TKey, TValue>.Add(TKey key, TValue value) {
// Validating the key seems redundant, because both Add and Remove
// will throw on a null key.
// This way, though, the code path on null key does not depend on value.
// Without this validation, there should be two unit tests for null key,
// one with a null value and one with a non-null value,
// which makes no sense.
if(key == null) {
throw new ArgumentNullException(nameof(key));
}
if(value != null) {
this._dictionary.Add(key, value);
} else {
_ = this._dictionary.Remove(key);
}
}
#endregion
#region Implementation of IReadOnlyDictionary<TKey, TValue>
/// <inheritdoc cref="IReadOnlyDictionary{TKey,TValue}.Keys"/>
IEnumerable<TKey> IReadOnlyDictionary<TKey, TValue>.Keys => this._dictionary.Keys;
/// <inheritdoc cref="IReadOnlyDictionary{TKey,TValue}.Values"/>
IEnumerable<TValue> IReadOnlyDictionary<TKey, TValue>.Values => this._dictionary.Values;
#endregion
#region Implementation of ICollection<KeyValuePair<TKey, TValue>>
/// <inheritdoc cref="ICollection{T}.IsReadOnly"/>
/// <remarks>
/// <para>This property is always <see langword="false"/> for a <see cref="DataDictionary{TKey,TValue}"/>.</para>
/// </remarks>
Boolean ICollection<KeyValuePair<TKey, TValue>>.IsReadOnly => false;
/// <inheritdoc cref="ICollection{T}.Add"/>
void ICollection<KeyValuePair<TKey, TValue>>.Add(KeyValuePair<TKey, TValue> item) {
if(item.Value != null) {
((ICollection<KeyValuePair<TKey, TValue>>)this._dictionary).Add(item);
} else {
_ = this._dictionary.Remove(item.Key);
}
}
/// <inheritdoc cref="ICollection{T}.Contains"/>
Boolean ICollection<KeyValuePair<TKey, TValue>>.Contains(KeyValuePair<TKey, TValue> item) => ((ICollection<KeyValuePair<TKey, TValue>>)this._dictionary).Contains(item);
/// <inheritdoc cref="ICollection{T}.CopyTo"/>
void ICollection<KeyValuePair<TKey, TValue>>.CopyTo(KeyValuePair<TKey, TValue>[] array, Int32 arrayIndex) => ((ICollection<KeyValuePair<TKey, TValue>>)this._dictionary).CopyTo(array, arrayIndex);
/// <inheritdoc cref="ICollection{T}.Remove"/>
Boolean ICollection<KeyValuePair<TKey, TValue>>.Remove(KeyValuePair<TKey, TValue> item) => ((ICollection<KeyValuePair<TKey, TValue>>)this._dictionary).Remove(item);
#endregion
#region Implementation of IEnumerable<KeyValuePair<TKey, TValue>>
/// <inheritdoc cref="IEnumerable{T}.GetEnumerator"/>
IEnumerator<KeyValuePair<TKey, TValue>> IEnumerable<KeyValuePair<TKey, TValue>>.GetEnumerator() => this._dictionary.GetEnumerator();
#endregion
#region Implementation of IEnumerable
/// <inheritdoc cref="IEnumerable.GetEnumerator"/>
IEnumerator IEnumerable.GetEnumerator() => ((IEnumerable)this._dictionary).GetEnumerator();
#endregion
}
}

View File

@ -1,49 +1,46 @@
using System;
namespace Swan.Collections
{
namespace Swan.Collections {
/// <summary>
/// <para>Implements a collection of components that automatically disposes each component
/// implementing <see cref="IDisposable"/>.</para>
/// <para>Each component in the collection may be given a unique name for later retrieval.</para>
/// </summary>
/// <typeparam name="T">The type of components in the collection.</typeparam>
/// <seealso cref="ComponentCollection{T}" />
/// <seealso cref="IComponentCollection{T}" />
public class DisposableComponentCollection<T> : ComponentCollection<T>, IDisposable {
/// <summary>
/// <para>Implements a collection of components that automatically disposes each component
/// implementing <see cref="IDisposable"/>.</para>
/// <para>Each component in the collection may be given a unique name for later retrieval.</para>
/// Finalizes an instance of the <see cref="DisposableComponentCollection{T}"/> class.
/// </summary>
/// <typeparam name="T">The type of components in the collection.</typeparam>
/// <seealso cref="ComponentCollection{T}" />
/// <seealso cref="IComponentCollection{T}" />
public class DisposableComponentCollection<T> : ComponentCollection<T>, IDisposable
{
/// <summary>
/// Finalizes an instance of the <see cref="DisposableComponentCollection{T}"/> class.
/// </summary>
~DisposableComponentCollection()
{
Dispose(false);
}
/// <summary>
/// Releases unmanaged and - optionally - managed resources.
/// </summary>
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
/// <summary>
/// Releases unmanaged and - optionally - managed resources.
/// </summary>
/// <param name="disposing">
/// <see langword="true"/> to release both managed and unmanaged resources; <see langword="true"/> to release only unmanaged resources.
/// </param>
protected virtual void Dispose(bool disposing)
{
if (!disposing) return;
foreach (var component in this)
{
if (component is IDisposable disposable)
disposable.Dispose();
}
}
}
~DisposableComponentCollection() {
this.Dispose(false);
}
/// <summary>
/// Releases unmanaged and - optionally - managed resources.
/// </summary>
public void Dispose() {
this.Dispose(true);
GC.SuppressFinalize(this);
}
/// <summary>
/// Releases unmanaged and - optionally - managed resources.
/// </summary>
/// <param name="disposing">
/// <see langword="true"/> to release both managed and unmanaged resources; <see langword="true"/> to release only unmanaged resources.
/// </param>
protected virtual void Dispose(Boolean disposing) {
if(!disposing) {
return;
}
foreach(T component in this) {
if(component is IDisposable disposable) {
disposable.Dispose();
}
}
}
}
}

View File

@ -1,54 +1,56 @@
using System;
using System.Collections.Generic;
namespace Swan.Collections
{
namespace Swan.Collections {
/// <summary>
/// <para>Represents a collection of components.</para>
/// <para>Each component in the collection may be given a unique name for later retrieval.</para>
/// </summary>
/// <typeparam name="T">The type of components in the collection.</typeparam>
public interface IComponentCollection<T> : IReadOnlyList<T> {
/// <summary>
/// <para>Represents a collection of components.</para>
/// <para>Each component in the collection may be given a unique name for later retrieval.</para>
/// Gets an <see cref="IReadOnlyDictionary{TKey,TValue}"/> interface representing the named components.
/// </summary>
/// <typeparam name="T">The type of components in the collection.</typeparam>
public interface IComponentCollection<T> : IReadOnlyList<T>
{
/// <summary>
/// Gets an <see cref="IReadOnlyDictionary{TKey,TValue}"/> interface representing the named components.
/// </summary>
/// <value>
/// The named components.
/// </value>
IReadOnlyDictionary<string, T> Named { get; }
/// <summary>
/// <para>Gets an <see cref="IReadOnlyList{T}"/> interface representing all components
/// associated with safe names.</para>
/// <para>The safe name of a component is never <see langword="null"/>.
/// If a component's unique name if <see langword="null"/>, its safe name
/// will be some non-<see langword="null"/> string somehow identifying it.</para>
/// <para>Note that safe names are not necessarily unique.</para>
/// </summary>
/// <value>
/// A list of <see cref="ValueTuple{T1,T2}"/>s, each containing a safe name and a component.
/// </value>
IReadOnlyList<(string SafeName, T Component)> WithSafeNames { get; }
/// <summary>
/// Gets the component with the specified name.
/// </summary>
/// <value>
/// The component.
/// </value>
/// <param name="name">The name.</param>
/// <returns>The component with the specified <paramref name="name"/>.</returns>
/// <exception cref="ArgumentNullException"><paramref name="name"/> is null.</exception>
/// <exception cref="KeyNotFoundException">The property is retrieved and <paramref name="name"/> is not found.</exception>
T this[string name] { get; }
/// <summary>
/// Adds a component to the collection,
/// giving it the specified <paramref name="name"/> if it is not <see langword="null"/>.
/// </summary>
/// <param name="name">The name given to the module, or <see langword="null"/>.</param>
/// <param name="component">The component.</param>
void Add(string name, T component);
}
/// <value>
/// The named components.
/// </value>
IReadOnlyDictionary<String, T> Named {
get;
}
/// <summary>
/// <para>Gets an <see cref="IReadOnlyList{T}"/> interface representing all components
/// associated with safe names.</para>
/// <para>The safe name of a component is never <see langword="null"/>.
/// If a component's unique name if <see langword="null"/>, its safe name
/// will be some non-<see langword="null"/> string somehow identifying it.</para>
/// <para>Note that safe names are not necessarily unique.</para>
/// </summary>
/// <value>
/// A list of <see cref="ValueTuple{T1,T2}"/>s, each containing a safe name and a component.
/// </value>
IReadOnlyList<(String SafeName, T Component)> WithSafeNames {
get;
}
/// <summary>
/// Gets the component with the specified name.
/// </summary>
/// <value>
/// The component.
/// </value>
/// <param name="name">The name.</param>
/// <returns>The component with the specified <paramref name="name"/>.</returns>
/// <exception cref="ArgumentNullException"><paramref name="name"/> is null.</exception>
/// <exception cref="KeyNotFoundException">The property is retrieved and <paramref name="name"/> is not found.</exception>
T this[String name] { get; }
/// <summary>
/// Adds a component to the collection,
/// giving it the specified <paramref name="name"/> if it is not <see langword="null"/>.
/// </summary>
/// <param name="name">The name given to the module, or <see langword="null"/>.</param>
/// <param name="component">The component.</param>
void Add(String name, T component);
}
}

View File

@ -1,34 +1,32 @@
using System;
using System.Collections.Generic;
namespace Swan.Collections
{
namespace Swan.Collections {
/// <summary>
/// Represents a generic collection of key/value pairs that does not store
/// null values.
/// </summary>
/// <typeparam name="TKey">The type of keys in the dictionary. This must be a reference type.</typeparam>
/// <typeparam name="TValue">The type of values in the dictionary. This must be a reference type.</typeparam>
public interface IDataDictionary<TKey, TValue> : IDictionary<TKey, TValue>, IReadOnlyDictionary<TKey, TValue> where TKey : class where TValue : class {
/// <summary>
/// Represents a generic collection of key/value pairs that does not store
/// null values.
/// Gets a value that indicates whether the <see cref="IDataDictionary{TKey,TValue}"/> is empty.
/// </summary>
/// <typeparam name="TKey">The type of keys in the dictionary. This must be a reference type.</typeparam>
/// <typeparam name="TValue">The type of values in the dictionary. This must be a reference type.</typeparam>
public interface IDataDictionary<TKey, TValue> : IDictionary<TKey, TValue>, IReadOnlyDictionary<TKey, TValue>
where TKey : class
where TValue : class
{
/// <summary>
/// Gets a value that indicates whether the <see cref="IDataDictionary{TKey,TValue}"/> is empty.
/// </summary>
/// <value>
/// <see langword="true"/> if the <see cref="IDataDictionary{TKey,TValue}"/> is empty; otherwise, <see langword="false"/>.
/// </value>
bool IsEmpty { get; }
/// <summary>
/// Attempts to remove and return the value that has the specified key from the <see cref="IDataDictionary{TKey,TValue}"/>.
/// </summary>
/// <param name="key">The key of the element to remove and return.</param>
/// <param name="value">When this method returns, the value removed from the <see cref="IDataDictionary{TKey,TValue}"/>,
/// if the key is found; otherwise, <see langword="null"/>. This parameter is passed uninitialized.</param>
/// <returns><see langword="true"/> if the value was removed successfully; otherwise, <see langword="false"/>.</returns>
/// <exception cref="ArgumentNullException"><paramref name="key"/> is <see langword="null"/>.</exception>
bool TryRemove(TKey key, out TValue value);
}
/// <value>
/// <see langword="true"/> if the <see cref="IDataDictionary{TKey,TValue}"/> is empty; otherwise, <see langword="false"/>.
/// </value>
Boolean IsEmpty {
get;
}
/// <summary>
/// Attempts to remove and return the value that has the specified key from the <see cref="IDataDictionary{TKey,TValue}"/>.
/// </summary>
/// <param name="key">The key of the element to remove and return.</param>
/// <param name="value">When this method returns, the value removed from the <see cref="IDataDictionary{TKey,TValue}"/>,
/// if the key is found; otherwise, <see langword="null"/>. This parameter is passed uninitialized.</param>
/// <returns><see langword="true"/> if the value was removed successfully; otherwise, <see langword="false"/>.</returns>
/// <exception cref="ArgumentNullException"><paramref name="key"/> is <see langword="null"/>.</exception>
Boolean TryRemove(TKey key, out TValue value);
}
}

View File

@ -1,77 +1,70 @@
using System;
namespace Swan.Configuration
{
namespace Swan.Configuration {
/// <summary>
/// Base class for objects whose configuration may be locked,
/// thus becoming read-only, at a certain moment in their lifetime.
/// </summary>
public abstract class ConfiguredObject {
private readonly Object _syncRoot = new Object();
private Boolean _configurationLocked;
/// <summary>
/// Base class for objects whose configuration may be locked,
/// thus becoming read-only, at a certain moment in their lifetime.
/// Gets a value indicating whether s configuration has already been locked
/// and has therefore become read-only.
/// </summary>
public abstract class ConfiguredObject
{
private readonly object _syncRoot = new object();
private bool _configurationLocked;
/// <summary>
/// Gets a value indicating whether s configuration has already been locked
/// and has therefore become read-only.
/// </summary>
/// <value>
/// <see langword="true"/> if the configuration is locked; otherwise, <see langword="false"/>.
/// </value>
/// <seealso cref="EnsureConfigurationNotLocked"/>
protected bool ConfigurationLocked
{
get
{
lock (_syncRoot)
{
return _configurationLocked;
}
}
}
/// <summary>
/// <para>Locks this instance's configuration, preventing further modifications.</para>
/// </summary>
/// <remarks>
/// <para>Configuration locking must be enforced by derived classes
/// by calling <see cref="EnsureConfigurationNotLocked"/> at the start
/// of methods and property setters that could change the object's
/// configuration.</para>
/// <para>Immediately before locking the configuration, this method calls <see cref="OnBeforeLockConfiguration"/>
/// as a last chance to validate configuration data, and to lock the configuration of contained objects.</para>
/// </remarks>
/// <seealso cref="OnBeforeLockConfiguration"/>
protected void LockConfiguration()
{
lock (_syncRoot)
{
if (_configurationLocked)
return;
OnBeforeLockConfiguration();
_configurationLocked = true;
}
}
/// <summary>
/// Called immediately before locking the configuration.
/// </summary>
/// <seealso cref="LockConfiguration"/>
protected virtual void OnBeforeLockConfiguration()
{
}
/// <summary>
/// Checks whether a module's configuration has become read-only
/// and, if so, throws an <see cref="InvalidOperationException"/>.
/// </summary>
/// <exception cref="InvalidOperationException">The configuration is locked.</exception>
/// <seealso cref="ConfigurationLocked"/>
protected void EnsureConfigurationNotLocked()
{
if (ConfigurationLocked)
throw new InvalidOperationException($"Configuration of this {GetType().Name} instance is locked.");
}
}
/// <value>
/// <see langword="true"/> if the configuration is locked; otherwise, <see langword="false"/>.
/// </value>
/// <seealso cref="EnsureConfigurationNotLocked"/>
protected Boolean ConfigurationLocked {
get {
lock(this._syncRoot) {
return this._configurationLocked;
}
}
}
/// <summary>
/// <para>Locks this instance's configuration, preventing further modifications.</para>
/// </summary>
/// <remarks>
/// <para>Configuration locking must be enforced by derived classes
/// by calling <see cref="EnsureConfigurationNotLocked"/> at the start
/// of methods and property setters that could change the object's
/// configuration.</para>
/// <para>Immediately before locking the configuration, this method calls <see cref="OnBeforeLockConfiguration"/>
/// as a last chance to validate configuration data, and to lock the configuration of contained objects.</para>
/// </remarks>
/// <seealso cref="OnBeforeLockConfiguration"/>
protected void LockConfiguration() {
lock(this._syncRoot) {
if(this._configurationLocked) {
return;
}
this.OnBeforeLockConfiguration();
this._configurationLocked = true;
}
}
/// <summary>
/// Called immediately before locking the configuration.
/// </summary>
/// <seealso cref="LockConfiguration"/>
protected virtual void OnBeforeLockConfiguration() {
}
/// <summary>
/// Checks whether a module's configuration has become read-only
/// and, if so, throws an <see cref="InvalidOperationException"/>.
/// </summary>
/// <exception cref="InvalidOperationException">The configuration is locked.</exception>
/// <seealso cref="ConfigurationLocked"/>
protected void EnsureConfigurationNotLocked() {
if(this.ConfigurationLocked) {
throw new InvalidOperationException($"Configuration of this {this.GetType().Name} instance is locked.");
}
}
}
}

View File

@ -1,54 +1,62 @@
using System;
namespace Swan.Configuration
{
namespace Swan.Configuration {
/// <summary>
/// An attribute used to include additional information to a Property for serialization.
///
/// Previously we used DisplayAttribute from DataAnnotation.
/// </summary>
/// <seealso cref="System.Attribute" />
[AttributeUsage(AttributeTargets.Property)]
public sealed class PropertyDisplayAttribute : Attribute {
/// <summary>
/// An attribute used to include additional information to a Property for serialization.
///
/// Previously we used DisplayAttribute from DataAnnotation.
/// Gets or sets the name.
/// </summary>
/// <seealso cref="System.Attribute" />
[AttributeUsage(AttributeTargets.Property)]
public sealed class PropertyDisplayAttribute : Attribute
{
/// <summary>
/// Gets or sets the name.
/// </summary>
/// <value>
/// The name.
/// </value>
public string Name { get; set; }
/// <summary>
/// Gets or sets the description.
/// </summary>
/// <value>
/// The description.
/// </value>
public string Description { get; set; }
/// <summary>
/// Gets or sets the name of the group.
/// </summary>
/// <value>
/// The name of the group.
/// </value>
public string GroupName { get; set; }
/// <summary>
/// Gets or sets the default value.
/// </summary>
/// <value>
/// The default value.
/// </value>
public object DefaultValue { get; set; }
/// <summary>
/// Gets or sets the format string to call with method <c>ToString</c>.
/// </summary>
/// <value>
/// The format.
/// </value>
public string Format { get; set; }
}
/// <value>
/// The name.
/// </value>
public String Name {
get; set;
}
/// <summary>
/// Gets or sets the description.
/// </summary>
/// <value>
/// The description.
/// </value>
public String Description {
get; set;
}
/// <summary>
/// Gets or sets the name of the group.
/// </summary>
/// <value>
/// The name of the group.
/// </value>
public String GroupName {
get; set;
}
/// <summary>
/// Gets or sets the default value.
/// </summary>
/// <value>
/// The default value.
/// </value>
public Object DefaultValue {
get; set;
}
/// <summary>
/// Gets or sets the format string to call with method <c>ToString</c>.
/// </summary>
/// <value>
/// The format.
/// </value>
public String Format {
get; set;
}
}
}

View File

@ -41,10 +41,9 @@ namespace Swan.Configuration
/// </code>
/// </example>
/// <typeparam name="T">The type of settings model.</typeparam>
public sealed class SettingsProvider<T>
: SingletonBase<SettingsProvider<T>>
public sealed class SettingsProvider<T> : SingletonBase<SettingsProvider<T>>
{
private readonly object _syncRoot = new object();
private readonly Object _syncRoot = new Object();
private T _global;
@ -55,8 +54,7 @@ namespace Swan.Configuration
/// <value>
/// The configuration file path.
/// </value>
public string ConfigurationFilePath { get; set; } =
Path.Combine(SwanRuntime.EntryAssemblyDirectory, "appsettings.json");
public String ConfigurationFilePath { get; set; } = Path.Combine(SwanRuntime.EntryAssemblyDirectory, "appsettings.json");
/// <summary>
/// Gets the global settings object.
@ -68,12 +66,13 @@ namespace Swan.Configuration
{
get
{
lock (_syncRoot)
lock (this._syncRoot)
{
if (Equals(_global, default(T)))
ReloadGlobalSettings();
return _global;
if (Equals(this._global, default(T)!)) {
this.ReloadGlobalSettings();
}
return this._global;
}
}
}
@ -83,20 +82,21 @@ namespace Swan.Configuration
/// </summary>
public void ReloadGlobalSettings()
{
if (File.Exists(ConfigurationFilePath) == false || File.ReadAllText(ConfigurationFilePath).Length == 0)
{
ResetGlobalSettings();
if (File.Exists(this.ConfigurationFilePath) == false || File.ReadAllText(this.ConfigurationFilePath).Length == 0)
{
this.ResetGlobalSettings();
return;
}
lock (_syncRoot)
_global = Json.Deserialize<T>(File.ReadAllText(ConfigurationFilePath));
}
lock (this._syncRoot) {
this._global = Json.Deserialize<T>(File.ReadAllText(this.ConfigurationFilePath));
}
}
/// <summary>
/// Persists the global settings.
/// </summary>
public void PersistGlobalSettings() => File.WriteAllText(ConfigurationFilePath, Json.Serialize(Global, true));
public void PersistGlobalSettings() => File.WriteAllText(this.ConfigurationFilePath, Json.Serialize(this.Global, true));
/// <summary>
/// Updates settings from list.
@ -106,29 +106,34 @@ namespace Swan.Configuration
/// A list of settings of type ref="ExtendedPropertyInfo".
/// </returns>
/// <exception cref="ArgumentNullException">propertyList.</exception>
public List<string> RefreshFromList(List<ExtendedPropertyInfo<T>> propertyList)
public List<String> RefreshFromList(List<ExtendedPropertyInfo<T>> propertyList)
{
if (propertyList == null)
throw new ArgumentNullException(nameof(propertyList));
if (propertyList == null) {
throw new ArgumentNullException(nameof(propertyList));
}
List<String> changedSettings = new List<global::System.String>();
IEnumerable<PropertyInfo> globalProps = PropertyTypeCache.DefaultCache.Value.RetrieveAllProperties<T>();
var changedSettings = new List<string>();
var globalProps = PropertyTypeCache.DefaultCache.Value.RetrieveAllProperties<T>();
foreach (ExtendedPropertyInfo<T> property in propertyList)
{
PropertyInfo propertyInfo = globalProps.FirstOrDefault(x => x.Name == property.Property);
foreach (var property in propertyList)
{
var propertyInfo = globalProps.FirstOrDefault(x => x.Name == property.Property);
if (propertyInfo == null) {
continue;
}
Object originalValue = propertyInfo.GetValue(this.Global);
Boolean isChanged = propertyInfo.PropertyType.IsArray
? property.Value is IEnumerable enumerable && propertyInfo.TrySetArray(enumerable.Cast<Object>(), this.Global)
: this.SetValue(property.Value, originalValue, propertyInfo);
if (propertyInfo == null) continue;
var originalValue = propertyInfo.GetValue(Global);
var isChanged = propertyInfo.PropertyType.IsArray
? property.Value is IEnumerable enumerable && propertyInfo.TrySetArray(enumerable.Cast<object>(), Global)
: SetValue(property.Value, originalValue, propertyInfo);
if (!isChanged) continue;
changedSettings.Add(property.Property);
PersistGlobalSettings();
if (!isChanged) {
continue;
}
changedSettings.Add(property.Property);
this.PersistGlobalSettings();
}
return changedSettings;
@ -138,9 +143,9 @@ namespace Swan.Configuration
/// Gets the list.
/// </summary>
/// <returns>A List of ExtendedPropertyInfo of the type T.</returns>
public List<ExtendedPropertyInfo<T>>? GetList()
{
var jsonData = Json.Deserialize(Json.Serialize(Global)) as Dictionary<string, object>;
public List<ExtendedPropertyInfo<T>> GetList()
{
Dictionary<String, Object> jsonData = Json.Deserialize(Json.Serialize(this.Global)) as Dictionary<global::System.String, global::System.Object>;
return jsonData?.Keys
.Select(p => new ExtendedPropertyInfo<T>(p) { Value = jsonData[p] })
@ -152,26 +157,27 @@ namespace Swan.Configuration
/// </summary>
public void ResetGlobalSettings()
{
lock (_syncRoot)
_global = Activator.CreateInstance<T>();
PersistGlobalSettings();
lock (this._syncRoot) {
this._global = Activator.CreateInstance<T>();
}
this.PersistGlobalSettings();
}
private bool SetValue(object property, object originalValue, PropertyInfo propertyInfo)
private Boolean SetValue(Object property, Object originalValue, PropertyInfo propertyInfo)
{
switch (property)
{
case null when originalValue == null:
break;
case null:
propertyInfo.SetValue(Global, null);
propertyInfo.SetValue(this.Global, null);
return true;
default:
if (propertyInfo.PropertyType.TryParseBasicType(property, out var propertyValue) &&
if (propertyInfo.PropertyType.TryParseBasicType(property, out Object propertyValue) &&
!propertyValue.Equals(originalValue))
{
propertyInfo.SetValue(Global, propertyValue);
propertyInfo.SetValue(this.Global, propertyValue);
return true;
}

View File

@ -3,122 +3,123 @@ using System.IO;
using System.Security.Cryptography;
using System.Text;
namespace Swan.Cryptography
{
namespace Swan.Cryptography {
/// <summary>
/// Use this class to compute a hash in MD4, SHA1, SHA256 or SHA512.
/// </summary>
public static class Hasher {
private static readonly Lazy<MD5> Md5Hasher = new Lazy<MD5>(MD5.Create, true);
private static readonly Lazy<SHA1> SHA1Hasher = new Lazy<SHA1>(SHA1.Create, true);
private static readonly Lazy<SHA256> SHA256Hasher = new Lazy<SHA256>(SHA256.Create, true);
private static readonly Lazy<SHA512> SHA512Hasher = new Lazy<SHA512>(SHA512.Create, true);
/// <summary>
/// Use this class to compute a hash in MD4, SHA1, SHA256 or SHA512.
/// Computes the MD5 hash of the given stream.
/// Do not use for large streams as this reads ALL bytes at once.
/// </summary>
public static class Hasher
{
private static readonly Lazy<MD5> Md5Hasher = new Lazy<MD5>(MD5.Create, true);
private static readonly Lazy<SHA1> SHA1Hasher = new Lazy<SHA1>(SHA1.Create, true);
private static readonly Lazy<SHA256> SHA256Hasher = new Lazy<SHA256>(SHA256.Create, true);
private static readonly Lazy<SHA512> SHA512Hasher = new Lazy<SHA512>(SHA512.Create, true);
/// <summary>
/// Computes the MD5 hash of the given stream.
/// Do not use for large streams as this reads ALL bytes at once.
/// </summary>
/// <param name="this">The stream.</param>
/// <param name="createHasher">if set to <c>true</c> [create hasher].</param>
/// <returns>
/// The computed hash code.
/// </returns>
/// <exception cref="ArgumentNullException">stream.</exception>
[Obsolete("Use a better hasher.")]
public static byte[] ComputeMD5(Stream @this, bool createHasher = false)
{
if (@this == null)
throw new ArgumentNullException(nameof(@this));
var md5 = MD5.Create();
const int bufferSize = 4096;
var readAheadBuffer = new byte[bufferSize];
var readAheadBytesRead = @this.Read(readAheadBuffer, 0, readAheadBuffer.Length);
do
{
var bytesRead = readAheadBytesRead;
var buffer = readAheadBuffer;
readAheadBuffer = new byte[bufferSize];
readAheadBytesRead = @this.Read(readAheadBuffer, 0, readAheadBuffer.Length);
if (readAheadBytesRead == 0)
md5.TransformFinalBlock(buffer, 0, bytesRead);
else
md5.TransformBlock(buffer, 0, bytesRead, buffer, 0);
}
while (readAheadBytesRead != 0);
return md5.Hash;
}
/// <summary>
/// Computes the MD5 hash of the given string using UTF8 byte encoding.
/// </summary>
/// <param name="value">The input string.</param>
/// <param name="createHasher">if set to <c>true</c> [create hasher].</param>
/// <returns>The computed hash code.</returns>
[Obsolete("Use a better hasher.")]
public static byte[] ComputeMD5(string value, bool createHasher = false) =>
ComputeMD5(Encoding.UTF8.GetBytes(value), createHasher);
/// <summary>
/// Computes the MD5 hash of the given byte array.
/// </summary>
/// <param name="data">The data.</param>
/// <param name="createHasher">if set to <c>true</c> [create hasher].</param>
/// <returns>The computed hash code.</returns>
[Obsolete("Use a better hasher.")]
public static byte[] ComputeMD5(byte[] data, bool createHasher = false) =>
(createHasher ? MD5.Create() : Md5Hasher.Value).ComputeHash(data);
/// <summary>
/// Computes the SHA-1 hash of the given string using UTF8 byte encoding.
/// </summary>
/// <param name="this">The input string.</param>
/// <param name="createHasher">if set to <c>true</c> [create hasher].</param>
/// <returns>
/// The computes a Hash-based Message Authentication Code (HMAC)
/// using the SHA1 hash function.
/// </returns>
[Obsolete("Use a better hasher.")]
public static byte[] ComputeSha1(string @this, bool createHasher = false)
{
var inputBytes = Encoding.UTF8.GetBytes(@this);
return (createHasher ? SHA1.Create() : SHA1Hasher.Value).ComputeHash(inputBytes);
}
/// <summary>
/// Computes the SHA-256 hash of the given string using UTF8 byte encoding.
/// </summary>
/// <param name="value">The input string.</param>
/// <param name="createHasher">if set to <c>true</c> [create hasher].</param>
/// <returns>
/// The computes a Hash-based Message Authentication Code (HMAC)
/// by using the SHA256 hash function.
/// </returns>
public static byte[] ComputeSha256(string value, bool createHasher = false)
{
var inputBytes = Encoding.UTF8.GetBytes(value);
return (createHasher ? SHA256.Create() : SHA256Hasher.Value).ComputeHash(inputBytes);
}
/// <summary>
/// Computes the SHA-512 hash of the given string using UTF8 byte encoding.
/// </summary>
/// <param name="value">The input string.</param>
/// <param name="createHasher">if set to <c>true</c> [create hasher].</param>
/// <returns>
/// The computes a Hash-based Message Authentication Code (HMAC)
/// using the SHA512 hash function.
/// </returns>
public static byte[] ComputeSha512(string value, bool createHasher = false)
{
var inputBytes = Encoding.UTF8.GetBytes(value);
return (createHasher ? SHA512.Create() : SHA512Hasher.Value).ComputeHash(inputBytes);
}
}
/// <param name="this">The stream.</param>
/// <param name="createHasher">if set to <c>true</c> [create hasher].</param>
/// <returns>
/// The computed hash code.
/// </returns>
/// <exception cref="ArgumentNullException">stream.</exception>
[Obsolete("Use a better hasher.")]
[System.Diagnostics.CodeAnalysis.SuppressMessage("Codequalität", "IDE0067:Objekte verwerfen, bevor Bereich verloren geht", Justification = "<Ausstehend>")]
[System.Diagnostics.CodeAnalysis.SuppressMessage("Stil", "IDE0045:In bedingten Ausdruck konvertieren", Justification = "<Ausstehend>")]
public static Byte[] ComputeMD5(Stream @this, Boolean createHasher = false) {
if(@this == null) {
throw new ArgumentNullException(nameof(@this));
}
MD5 md5 = MD5.Create();
const Int32 bufferSize = 4096;
Byte[] readAheadBuffer = new Byte[bufferSize];
Int32 readAheadBytesRead = @this.Read(readAheadBuffer, 0, readAheadBuffer.Length);
do {
Int32 bytesRead = readAheadBytesRead;
Byte[] buffer = readAheadBuffer;
readAheadBuffer = new Byte[bufferSize];
readAheadBytesRead = @this.Read(readAheadBuffer, 0, readAheadBuffer.Length);
if(readAheadBytesRead == 0) {
_ = md5.TransformFinalBlock(buffer, 0, bytesRead);
} else {
_ = md5.TransformBlock(buffer, 0, bytesRead, buffer, 0);
}
}
while(readAheadBytesRead != 0);
return md5.Hash;
}
/// <summary>
/// Computes the MD5 hash of the given string using UTF8 byte encoding.
/// </summary>
/// <param name="value">The input string.</param>
/// <param name="createHasher">if set to <c>true</c> [create hasher].</param>
/// <returns>The computed hash code.</returns>
[Obsolete("Use a better hasher.")]
public static Byte[] ComputeMD5(String value, Boolean createHasher = false) => ComputeMD5(Encoding.UTF8.GetBytes(value), createHasher);
/// <summary>
/// Computes the MD5 hash of the given byte array.
/// </summary>
/// <param name="data">The data.</param>
/// <param name="createHasher">if set to <c>true</c> [create hasher].</param>
/// <returns>The computed hash code.</returns>
[Obsolete("Use a better hasher.")]
[System.Diagnostics.CodeAnalysis.SuppressMessage("Codequalität", "IDE0067:Objekte verwerfen, bevor Bereich verloren geht", Justification = "<Ausstehend>")]
public static Byte[] ComputeMD5(Byte[] data, Boolean createHasher = false) => (createHasher ? MD5.Create() : Md5Hasher.Value).ComputeHash(data);
/// <summary>
/// Computes the SHA-1 hash of the given string using UTF8 byte encoding.
/// </summary>
/// <param name="this">The input string.</param>
/// <param name="createHasher">if set to <c>true</c> [create hasher].</param>
/// <returns>
/// The computes a Hash-based Message Authentication Code (HMAC)
/// using the SHA1 hash function.
/// </returns>
[Obsolete("Use a better hasher.")]
[System.Diagnostics.CodeAnalysis.SuppressMessage("Codequalität", "IDE0067:Objekte verwerfen, bevor Bereich verloren geht", Justification = "<Ausstehend>")]
public static Byte[] ComputeSha1(String @this, Boolean createHasher = false) {
Byte[] inputBytes = Encoding.UTF8.GetBytes(@this);
return (createHasher ? SHA1.Create() : SHA1Hasher.Value).ComputeHash(inputBytes);
}
/// <summary>
/// Computes the SHA-256 hash of the given string using UTF8 byte encoding.
/// </summary>
/// <param name="value">The input string.</param>
/// <param name="createHasher">if set to <c>true</c> [create hasher].</param>
/// <returns>
/// The computes a Hash-based Message Authentication Code (HMAC)
/// by using the SHA256 hash function.
/// </returns>
[System.Diagnostics.CodeAnalysis.SuppressMessage("Codequalität", "IDE0067:Objekte verwerfen, bevor Bereich verloren geht", Justification = "<Ausstehend>")]
public static Byte[] ComputeSha256(String value, Boolean createHasher = false) {
Byte[] inputBytes = Encoding.UTF8.GetBytes(value);
return (createHasher ? SHA256.Create() : SHA256Hasher.Value).ComputeHash(inputBytes);
}
/// <summary>
/// Computes the SHA-512 hash of the given string using UTF8 byte encoding.
/// </summary>
/// <param name="value">The input string.</param>
/// <param name="createHasher">if set to <c>true</c> [create hasher].</param>
/// <returns>
/// The computes a Hash-based Message Authentication Code (HMAC)
/// using the SHA512 hash function.
/// </returns>
[System.Diagnostics.CodeAnalysis.SuppressMessage("Codequalität", "IDE0067:Objekte verwerfen, bevor Bereich verloren geht", Justification = "<Ausstehend>")]
public static Byte[] ComputeSha512(String value, Boolean createHasher = false) {
Byte[] inputBytes = Encoding.UTF8.GetBytes(value);
return (createHasher ? SHA512.Create() : SHA512Hasher.Value).ComputeHash(inputBytes);
}
}
}

View File

@ -1,174 +1,165 @@
using System;
namespace Swan
{
namespace Swan {
/// <summary>
/// Represents a struct of DateTimeSpan to compare dates and get in
/// separate fields the amount of time between those dates.
///
/// Based on https://stackoverflow.com/a/9216404/1096693.
/// </summary>
public struct DateTimeSpan {
/// <summary>
/// Represents a struct of DateTimeSpan to compare dates and get in
/// separate fields the amount of time between those dates.
///
/// Based on https://stackoverflow.com/a/9216404/1096693.
/// Initializes a new instance of the <see cref="DateTimeSpan"/> struct.
/// </summary>
public struct DateTimeSpan
{
/// <summary>
/// Initializes a new instance of the <see cref="DateTimeSpan"/> struct.
/// </summary>
/// <param name="years">The years.</param>
/// <param name="months">The months.</param>
/// <param name="days">The days.</param>
/// <param name="hours">The hours.</param>
/// <param name="minutes">The minutes.</param>
/// <param name="seconds">The seconds.</param>
/// <param name="milliseconds">The milliseconds.</param>
public DateTimeSpan(int years, int months, int days, int hours, int minutes, int seconds, int milliseconds)
{
Years = years;
Months = months;
Days = days;
Hours = hours;
Minutes = minutes;
Seconds = seconds;
Milliseconds = milliseconds;
}
/// <summary>
/// Gets the years.
/// </summary>
/// <value>
/// The years.
/// </value>
public int Years { get; }
/// <summary>
/// Gets the months.
/// </summary>
/// <value>
/// The months.
/// </value>
public int Months { get; }
/// <summary>
/// Gets the days.
/// </summary>
/// <value>
/// The days.
/// </value>
public int Days { get; }
/// <summary>
/// Gets the hours.
/// </summary>
/// <value>
/// The hours.
/// </value>
public int Hours { get; }
/// <summary>
/// Gets the minutes.
/// </summary>
/// <value>
/// The minutes.
/// </value>
public int Minutes { get; }
/// <summary>
/// Gets the seconds.
/// </summary>
/// <value>
/// The seconds.
/// </value>
public int Seconds { get; }
/// <summary>
/// Gets the milliseconds.
/// </summary>
/// <value>
/// The milliseconds.
/// </value>
public int Milliseconds { get; }
internal static DateTimeSpan CompareDates(DateTime date1, DateTime date2)
{
if (date2 < date1)
{
var sub = date1;
date1 = date2;
date2 = sub;
}
var current = date1;
var years = 0;
var months = 0;
var days = 0;
var phase = Phase.Years;
var span = new DateTimeSpan();
var officialDay = current.Day;
while (phase != Phase.Done)
{
switch (phase)
{
case Phase.Years:
if (current.AddYears(years + 1) > date2)
{
phase = Phase.Months;
current = current.AddYears(years);
}
else
{
years++;
}
break;
case Phase.Months:
if (current.AddMonths(months + 1) > date2)
{
phase = Phase.Days;
current = current.AddMonths(months);
if (current.Day < officialDay &&
officialDay <= DateTime.DaysInMonth(current.Year, current.Month))
current = current.AddDays(officialDay - current.Day);
}
else
{
months++;
}
break;
case Phase.Days:
if (current.AddDays(days + 1) > date2)
{
current = current.AddDays(days);
var timespan = date2 - current;
span = new DateTimeSpan(
years,
months,
days,
timespan.Hours,
timespan.Minutes,
timespan.Seconds,
timespan.Milliseconds);
phase = Phase.Done;
}
else
{
days++;
}
break;
}
}
return span;
}
private enum Phase
{
Years,
Months,
Days,
Done,
}
}
/// <param name="years">The years.</param>
/// <param name="months">The months.</param>
/// <param name="days">The days.</param>
/// <param name="hours">The hours.</param>
/// <param name="minutes">The minutes.</param>
/// <param name="seconds">The seconds.</param>
/// <param name="milliseconds">The milliseconds.</param>
public DateTimeSpan(Int32 years, Int32 months, Int32 days, Int32 hours, Int32 minutes, Int32 seconds, Int32 milliseconds) {
this.Years = years;
this.Months = months;
this.Days = days;
this.Hours = hours;
this.Minutes = minutes;
this.Seconds = seconds;
this.Milliseconds = milliseconds;
}
/// <summary>
/// Gets the years.
/// </summary>
/// <value>
/// The years.
/// </value>
public Int32 Years {
get;
}
/// <summary>
/// Gets the months.
/// </summary>
/// <value>
/// The months.
/// </value>
public Int32 Months {
get;
}
/// <summary>
/// Gets the days.
/// </summary>
/// <value>
/// The days.
/// </value>
public Int32 Days {
get;
}
/// <summary>
/// Gets the hours.
/// </summary>
/// <value>
/// The hours.
/// </value>
public Int32 Hours {
get;
}
/// <summary>
/// Gets the minutes.
/// </summary>
/// <value>
/// The minutes.
/// </value>
public Int32 Minutes {
get;
}
/// <summary>
/// Gets the seconds.
/// </summary>
/// <value>
/// The seconds.
/// </value>
public Int32 Seconds {
get;
}
/// <summary>
/// Gets the milliseconds.
/// </summary>
/// <value>
/// The milliseconds.
/// </value>
public Int32 Milliseconds {
get;
}
internal static DateTimeSpan CompareDates(DateTime date1, DateTime date2) {
if(date2 < date1) {
DateTime sub = date1;
date1 = date2;
date2 = sub;
}
DateTime current = date1;
Int32 years = 0;
Int32 months = 0;
Int32 days = 0;
Phase phase = Phase.Years;
DateTimeSpan span = new DateTimeSpan();
Int32 officialDay = current.Day;
while(phase != Phase.Done) {
switch(phase) {
case Phase.Years:
if(current.AddYears(years + 1) > date2) {
phase = Phase.Months;
current = current.AddYears(years);
} else {
years++;
}
break;
case Phase.Months:
if(current.AddMonths(months + 1) > date2) {
phase = Phase.Days;
current = current.AddMonths(months);
if(current.Day < officialDay &&
officialDay <= DateTime.DaysInMonth(current.Year, current.Month)) {
current = current.AddDays(officialDay - current.Day);
}
} else {
months++;
}
break;
case Phase.Days:
if(current.AddDays(days + 1) > date2) {
current = current.AddDays(days);
TimeSpan timespan = date2 - current;
span = new DateTimeSpan(years, months, days, timespan.Hours, timespan.Minutes, timespan.Seconds, timespan.Milliseconds);
phase = Phase.Done;
} else {
days++;
}
break;
}
}
return span;
}
private enum Phase {
Years,
Months,
Days,
Done,
}
}
}

View File

@ -3,136 +3,123 @@ using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Net;
using Swan.Reflection;
namespace Swan
{
namespace Swan {
/// <summary>
/// Contains useful constants and definitions.
/// </summary>
public static partial class Definitions {
#region Main Dictionary Definition
/// <summary>
/// Contains useful constants and definitions.
/// The basic types information.
/// </summary>
public static partial class Definitions
{
#region Main Dictionary Definition
/// <summary>
/// The basic types information.
/// </summary>
public static readonly Lazy<Dictionary<Type, ExtendedTypeInfo>> BasicTypesInfo = new Lazy<Dictionary<Type, ExtendedTypeInfo>>(() =>
new Dictionary<Type, ExtendedTypeInfo>
{
// Non-Nullables
{typeof(DateTime), new ExtendedTypeInfo<DateTime>()},
{typeof(byte), new ExtendedTypeInfo<byte>()},
{typeof(sbyte), new ExtendedTypeInfo<sbyte>()},
{typeof(int), new ExtendedTypeInfo<int>()},
{typeof(uint), new ExtendedTypeInfo<uint>()},
{typeof(short), new ExtendedTypeInfo<short>()},
{typeof(ushort), new ExtendedTypeInfo<ushort>()},
{typeof(long), new ExtendedTypeInfo<long>()},
{typeof(ulong), new ExtendedTypeInfo<ulong>()},
{typeof(float), new ExtendedTypeInfo<float>()},
{typeof(double), new ExtendedTypeInfo<double>()},
{typeof(char), new ExtendedTypeInfo<char>()},
{typeof(bool), new ExtendedTypeInfo<bool>()},
{typeof(decimal), new ExtendedTypeInfo<decimal>()},
{typeof(Guid), new ExtendedTypeInfo<Guid>()},
public static readonly Lazy<Dictionary<Type, ExtendedTypeInfo>> BasicTypesInfo = new Lazy<Dictionary<Type, ExtendedTypeInfo>>(() => new Dictionary<Type, ExtendedTypeInfo> {
// Non-Nullables
{typeof(DateTime), new ExtendedTypeInfo<DateTime>()},
{typeof(Byte), new ExtendedTypeInfo<Byte>()},
{typeof(SByte), new ExtendedTypeInfo<SByte>()},
{typeof(Int32), new ExtendedTypeInfo<Int32>()},
{typeof(UInt32), new ExtendedTypeInfo<UInt32>()},
{typeof(Int16), new ExtendedTypeInfo<Int16>()},
{typeof(UInt16), new ExtendedTypeInfo<UInt16>()},
{typeof(Int64), new ExtendedTypeInfo<Int64>()},
{typeof(UInt64), new ExtendedTypeInfo<UInt64>()},
{typeof(Single), new ExtendedTypeInfo<Single>()},
{typeof(Double), new ExtendedTypeInfo<Double>()},
{typeof(Char), new ExtendedTypeInfo<Char>()},
{typeof(Boolean), new ExtendedTypeInfo<Boolean>()},
{typeof(Decimal), new ExtendedTypeInfo<Decimal>()},
{typeof(Guid), new ExtendedTypeInfo<Guid>()},
// Strings is also considered a basic type (it's the only basic reference type)
{typeof(string), new ExtendedTypeInfo<string>()},
// Strings is also considered a basic type (it's the only basic reference type)
{typeof(String), new ExtendedTypeInfo<String>()},
// Nullables
{typeof(DateTime?), new ExtendedTypeInfo<DateTime?>()},
{typeof(byte?), new ExtendedTypeInfo<byte?>()},
{typeof(sbyte?), new ExtendedTypeInfo<sbyte?>()},
{typeof(int?), new ExtendedTypeInfo<int?>()},
{typeof(uint?), new ExtendedTypeInfo<uint?>()},
{typeof(short?), new ExtendedTypeInfo<short?>()},
{typeof(ushort?), new ExtendedTypeInfo<ushort?>()},
{typeof(long?), new ExtendedTypeInfo<long?>()},
{typeof(ulong?), new ExtendedTypeInfo<ulong?>()},
{typeof(float?), new ExtendedTypeInfo<float?>()},
{typeof(double?), new ExtendedTypeInfo<double?>()},
{typeof(char?), new ExtendedTypeInfo<char?>()},
{typeof(bool?), new ExtendedTypeInfo<bool?>()},
{typeof(decimal?), new ExtendedTypeInfo<decimal?>()},
{typeof(Guid?), new ExtendedTypeInfo<Guid?>()},
// Nullables
{typeof(DateTime?), new ExtendedTypeInfo<DateTime?>()},
{typeof(Byte?), new ExtendedTypeInfo<Byte?>()},
{typeof(SByte?), new ExtendedTypeInfo<SByte?>()},
{typeof(Int32?), new ExtendedTypeInfo<Int32?>()},
{typeof(UInt32?), new ExtendedTypeInfo<UInt32?>()},
{typeof(Int16?), new ExtendedTypeInfo<Int16?>()},
{typeof(UInt16?), new ExtendedTypeInfo<UInt16?>()},
{typeof(Int64?), new ExtendedTypeInfo<Int64?>()},
{typeof(UInt64?), new ExtendedTypeInfo<UInt64?>()},
{typeof(Single?), new ExtendedTypeInfo<Single?>()},
{typeof(Double?), new ExtendedTypeInfo<Double?>()},
{typeof(Char?), new ExtendedTypeInfo<Char?>()},
{typeof(Boolean?), new ExtendedTypeInfo<Boolean?>()},
{typeof(Decimal?), new ExtendedTypeInfo<Decimal?>()},
{typeof(Guid?), new ExtendedTypeInfo<Guid?>()},
// Additional Types
{typeof(TimeSpan), new ExtendedTypeInfo<TimeSpan>()},
{typeof(TimeSpan?), new ExtendedTypeInfo<TimeSpan?>()},
{typeof(IPAddress), new ExtendedTypeInfo<IPAddress>()},
});
#endregion
/// <summary>
/// Contains all basic types, including string, date time, and all of their nullable counterparts.
/// </summary>
/// <value>
/// All basic types.
/// </value>
public static IReadOnlyCollection<Type> AllBasicTypes { get; } = new ReadOnlyCollection<Type>(BasicTypesInfo.Value.Keys.ToArray());
/// <summary>
/// Gets all numeric types including their nullable counterparts.
/// Note that Booleans and Guids are not considered numeric types.
/// </summary>
/// <value>
/// All numeric types.
/// </value>
public static IReadOnlyCollection<Type> AllNumericTypes { get; } = new ReadOnlyCollection<Type>(
BasicTypesInfo
.Value
.Where(kvp => kvp.Value.IsNumeric)
.Select(kvp => kvp.Key).ToArray());
/// <summary>
/// Gets all numeric types without their nullable counterparts.
/// Note that Booleans and Guids are not considered numeric types.
/// </summary>
/// <value>
/// All numeric value types.
/// </value>
public static IReadOnlyCollection<Type> AllNumericValueTypes { get; } = new ReadOnlyCollection<Type>(
BasicTypesInfo
.Value
.Where(kvp => kvp.Value.IsNumeric && !kvp.Value.IsNullableValueType)
.Select(kvp => kvp.Key).ToArray());
/// <summary>
/// Contains all basic value types. i.e. excludes string and nullables.
/// </summary>
/// <value>
/// All basic value types.
/// </value>
public static IReadOnlyCollection<Type> AllBasicValueTypes { get; } = new ReadOnlyCollection<Type>(
BasicTypesInfo
.Value
.Where(kvp => kvp.Value.IsValueType)
.Select(kvp => kvp.Key).ToArray());
/// <summary>
/// Contains all basic value types including the string type. i.e. excludes nullables.
/// </summary>
/// <value>
/// All basic value and string types.
/// </value>
public static IReadOnlyCollection<Type> AllBasicValueAndStringTypes { get; } = new ReadOnlyCollection<Type>(
BasicTypesInfo
.Value
.Where(kvp => kvp.Value.IsValueType || kvp.Key == typeof(string))
.Select(kvp => kvp.Key).ToArray());
/// <summary>
/// Gets all nullable value types. i.e. excludes string and all basic value types.
/// </summary>
/// <value>
/// All basic nullable value types.
/// </value>
public static IReadOnlyCollection<Type> AllBasicNullableValueTypes { get; } = new ReadOnlyCollection<Type>(
BasicTypesInfo
.Value
.Where(kvp => kvp.Value.IsNullableValueType)
.Select(kvp => kvp.Key).ToArray());
}
// Additional Types
{typeof(TimeSpan), new ExtendedTypeInfo<TimeSpan>()},
{typeof(TimeSpan?), new ExtendedTypeInfo<TimeSpan?>()},
{typeof(IPAddress), new ExtendedTypeInfo<IPAddress>()},
});
#endregion
/// <summary>
/// Contains all basic types, including string, date time, and all of their nullable counterparts.
/// </summary>
/// <value>
/// All basic types.
/// </value>
public static IReadOnlyCollection<Type> AllBasicTypes { get; } = new ReadOnlyCollection<Type>(BasicTypesInfo.Value.Keys.ToArray());
/// <summary>
/// Gets all numeric types including their nullable counterparts.
/// Note that Booleans and Guids are not considered numeric types.
/// </summary>
/// <value>
/// All numeric types.
/// </value>
public static IReadOnlyCollection<Type> AllNumericTypes {
get;
} = new ReadOnlyCollection<Type>(BasicTypesInfo.Value.Where(kvp => kvp.Value.IsNumeric).Select(kvp => kvp.Key).ToArray());
/// <summary>
/// Gets all numeric types without their nullable counterparts.
/// Note that Booleans and Guids are not considered numeric types.
/// </summary>
/// <value>
/// All numeric value types.
/// </value>
public static IReadOnlyCollection<Type> AllNumericValueTypes {
get;
} = new ReadOnlyCollection<Type>(BasicTypesInfo.Value.Where(kvp => kvp.Value.IsNumeric && !kvp.Value.IsNullableValueType).Select(kvp => kvp.Key).ToArray());
/// <summary>
/// Contains all basic value types. i.e. excludes string and nullables.
/// </summary>
/// <value>
/// All basic value types.
/// </value>
public static IReadOnlyCollection<Type> AllBasicValueTypes {
get;
} = new ReadOnlyCollection<Type>(BasicTypesInfo.Value.Where(kvp => kvp.Value.IsValueType).Select(kvp => kvp.Key).ToArray());
/// <summary>
/// Contains all basic value types including the string type. i.e. excludes nullables.
/// </summary>
/// <value>
/// All basic value and string types.
/// </value>
public static IReadOnlyCollection<Type> AllBasicValueAndStringTypes {
get;
} = new ReadOnlyCollection<Type>(BasicTypesInfo.Value.Where(kvp => kvp.Value.IsValueType || kvp.Key == typeof(String)).Select(kvp => kvp.Key).ToArray());
/// <summary>
/// Gets all nullable value types. i.e. excludes string and all basic value types.
/// </summary>
/// <value>
/// All basic nullable value types.
/// </value>
public static IReadOnlyCollection<Type> AllBasicNullableValueTypes {
get;
} = new ReadOnlyCollection<Type>(BasicTypesInfo.Value.Where(kvp => kvp.Value.IsNullableValueType).Select(kvp => kvp.Key).ToArray());
}
}

View File

@ -1,39 +1,34 @@
using System.Text;
using System;
using System.Text;
namespace Swan
{
namespace Swan {
/// <summary>
/// Contains useful constants and definitions.
/// </summary>
public static partial class Definitions {
/// <summary>
/// Contains useful constants and definitions.
/// The MS Windows codepage 1252 encoding used in some legacy scenarios
/// such as default CSV text encoding from Excel.
/// </summary>
public static partial class Definitions
{
/// <summary>
/// The MS Windows codepage 1252 encoding used in some legacy scenarios
/// such as default CSV text encoding from Excel.
/// </summary>
public static readonly Encoding Windows1252Encoding;
/// <summary>
/// The encoding associated with the default ANSI code page in the operating
/// system's regional and language settings.
/// </summary>
public static readonly Encoding CurrentAnsiEncoding;
/// <summary>
/// Initializes the <see cref="Definitions"/> class.
/// </summary>
static Definitions()
{
CurrentAnsiEncoding = Encoding.GetEncoding(default(int));
try
{
Windows1252Encoding = Encoding.GetEncoding(1252);
}
catch
{
// ignore, the codepage is not available use default
Windows1252Encoding = CurrentAnsiEncoding;
}
}
}
public static readonly Encoding Windows1252Encoding;
/// <summary>
/// The encoding associated with the default ANSI code page in the operating
/// system's regional and language settings.
/// </summary>
public static readonly Encoding CurrentAnsiEncoding;
/// <summary>
/// Initializes the <see cref="Definitions"/> class.
/// </summary>
static Definitions() {
CurrentAnsiEncoding = Encoding.GetEncoding(default(Int32));
try {
Windows1252Encoding = Encoding.GetEncoding(1252);
} catch {
// ignore, the codepage is not available use default
Windows1252Encoding = CurrentAnsiEncoding;
}
}
}
}

View File

@ -4,118 +4,102 @@ using System.Diagnostics;
using System.Linq;
using System.Text;
namespace Swan.Diagnostics
{
namespace Swan.Diagnostics {
/// <summary>
/// A simple benchmarking class.
/// </summary>
/// <example>
/// The following code demonstrates how to create a simple benchmark.
/// <code>
/// namespace Examples.Benchmark.Simple
/// {
/// using Swan.Diagnostics;
///
/// public class SimpleBenchmark
/// {
/// public static void Main()
/// {
/// using (Benchmark.Start("Test"))
/// {
/// // do some logic in here
/// }
///
/// // dump results into a string
/// var results = Benchmark.Dump();
/// }
/// }
///
/// }
/// </code>
/// </example>
public static partial class Benchmark {
private static readonly Object SyncLock = new Object();
private static readonly Dictionary<String, List<TimeSpan>> Measures = new Dictionary<String, List<TimeSpan>>();
/// <summary>
/// A simple benchmarking class.
/// Starts measuring with the given identifier.
/// </summary>
/// <example>
/// The following code demonstrates how to create a simple benchmark.
/// <code>
/// namespace Examples.Benchmark.Simple
/// {
/// using Swan.Diagnostics;
///
/// public class SimpleBenchmark
/// {
/// public static void Main()
/// {
/// using (Benchmark.Start("Test"))
/// {
/// // do some logic in here
/// }
///
/// // dump results into a string
/// var results = Benchmark.Dump();
/// }
/// }
///
/// }
/// </code>
/// </example>
public static partial class Benchmark
{
private static readonly object SyncLock = new object();
private static readonly Dictionary<string, List<TimeSpan>> Measures = new Dictionary<string, List<TimeSpan>>();
/// <summary>
/// Starts measuring with the given identifier.
/// </summary>
/// <param name="identifier">The identifier.</param>
/// <returns>A disposable object that when disposed, adds a benchmark result.</returns>
public static IDisposable Start(string identifier) => new BenchmarkUnit(identifier);
/// <summary>
/// Outputs the benchmark statistics.
/// </summary>
/// <returns>A string containing human-readable statistics.</returns>
public static string Dump()
{
var builder = new StringBuilder();
lock (SyncLock)
{
foreach (var kvp in Measures)
{
builder.Append($"BID: {kvp.Key,-30} | ")
.Append($"CNT: {kvp.Value.Count,6} | ")
.Append($"AVG: {kvp.Value.Average(t => t.TotalMilliseconds),8:0.000} ms. | ")
.Append($"MAX: {kvp.Value.Max(t => t.TotalMilliseconds),8:0.000} ms. | ")
.Append($"MIN: {kvp.Value.Min(t => t.TotalMilliseconds),8:0.000} ms. | ")
.Append(Environment.NewLine);
}
}
return builder.ToString().TrimEnd();
}
/// <summary>
/// Measures the elapsed time of the given action as a TimeSpan
/// This method uses a high precision Stopwatch if it is available.
/// </summary>
/// <param name="target">The target.</param>
/// <returns>
/// A time interval that represents a specified time, where the specification is in units of ticks.
/// </returns>
/// <exception cref="ArgumentNullException">target.</exception>
public static TimeSpan BenchmarkAction(Action target)
{
if (target == null)
throw new ArgumentNullException(nameof(target));
var sw = Stopwatch.IsHighResolution ? new HighResolutionTimer() : new Stopwatch();
try
{
sw.Start();
target.Invoke();
}
catch
{
// swallow
}
finally
{
sw.Stop();
}
return TimeSpan.FromTicks(sw.ElapsedTicks);
}
/// <summary>
/// Adds the specified result to the given identifier.
/// </summary>
/// <param name="identifier">The identifier.</param>
/// <param name="elapsed">The elapsed.</param>
private static void Add(string identifier, TimeSpan elapsed)
{
lock (SyncLock)
{
if (Measures.ContainsKey(identifier) == false)
Measures[identifier] = new List<TimeSpan>(1024 * 1024);
Measures[identifier].Add(elapsed);
}
}
}
/// <param name="identifier">The identifier.</param>
/// <returns>A disposable object that when disposed, adds a benchmark result.</returns>
public static IDisposable Start(String identifier) => new BenchmarkUnit(identifier);
/// <summary>
/// Outputs the benchmark statistics.
/// </summary>
/// <returns>A string containing human-readable statistics.</returns>
public static String Dump() {
StringBuilder builder = new StringBuilder();
lock(SyncLock) {
foreach(KeyValuePair<String, List<TimeSpan>> kvp in Measures) {
_ = builder.Append($"BID: {kvp.Key,-30} | ").Append($"CNT: {kvp.Value.Count,6} | ").Append($"AVG: {kvp.Value.Average(t => t.TotalMilliseconds),8:0.000} ms. | ").Append($"MAX: {kvp.Value.Max(t => t.TotalMilliseconds),8:0.000} ms. | ").Append($"MIN: {kvp.Value.Min(t => t.TotalMilliseconds),8:0.000} ms. | ").Append(Environment.NewLine);
}
}
return builder.ToString().TrimEnd();
}
/// <summary>
/// Measures the elapsed time of the given action as a TimeSpan
/// This method uses a high precision Stopwatch if it is available.
/// </summary>
/// <param name="target">The target.</param>
/// <returns>
/// A time interval that represents a specified time, where the specification is in units of ticks.
/// </returns>
/// <exception cref="ArgumentNullException">target.</exception>
public static TimeSpan BenchmarkAction(Action target) {
if(target == null) {
throw new ArgumentNullException(nameof(target));
}
Stopwatch sw = Stopwatch.IsHighResolution ? new HighResolutionTimer() : new Stopwatch();
try {
sw.Start();
target.Invoke();
} catch {
// swallow
} finally {
sw.Stop();
}
return TimeSpan.FromTicks(sw.ElapsedTicks);
}
/// <summary>
/// Adds the specified result to the given identifier.
/// </summary>
/// <param name="identifier">The identifier.</param>
/// <param name="elapsed">The elapsed.</param>
private static void Add(String identifier, TimeSpan elapsed) {
lock(SyncLock) {
if(Measures.ContainsKey(identifier) == false) {
Measures[identifier] = new List<TimeSpan>(1024 * 1024);
}
Measures[identifier].Add(elapsed);
}
}
}
}

View File

@ -1,50 +1,47 @@
using System;
#nullable enable
using System;
using System.Diagnostics;
namespace Swan.Diagnostics
{
public static partial class Benchmark
{
/// <summary>
/// Represents a disposable benchmark unit.
/// </summary>
/// <seealso cref="IDisposable" />
private sealed class BenchmarkUnit : IDisposable
{
private readonly string _identifier;
private bool _isDisposed; // To detect redundant calls
private Stopwatch? _stopwatch = new Stopwatch();
/// <summary>
/// Initializes a new instance of the <see cref="BenchmarkUnit" /> class.
/// </summary>
/// <param name="identifier">The identifier.</param>
public BenchmarkUnit(string identifier)
{
_identifier = identifier;
_stopwatch?.Start();
}
/// <inheritdoc />
public void Dispose() => Dispose(true);
/// <summary>
/// Releases unmanaged and - optionally - managed resources.
/// </summary>
/// <param name="alsoManaged"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.</param>
private void Dispose(bool alsoManaged)
{
if (_isDisposed) return;
if (alsoManaged)
{
Add(_identifier, _stopwatch?.Elapsed ?? default);
_stopwatch?.Stop();
}
_stopwatch = null;
_isDisposed = true;
}
}
}
namespace Swan.Diagnostics {
public static partial class Benchmark {
/// <summary>
/// Represents a disposable benchmark unit.
/// </summary>
/// <seealso cref="IDisposable" />
private sealed class BenchmarkUnit : IDisposable {
private readonly String _identifier;
private Boolean _isDisposed; // To detect redundant calls
private Stopwatch? _stopwatch = new Stopwatch();
/// <summary>
/// Initializes a new instance of the <see cref="BenchmarkUnit" /> class.
/// </summary>
/// <param name="identifier">The identifier.</param>
public BenchmarkUnit(String identifier) {
this._identifier = identifier;
this._stopwatch?.Start();
}
/// <inheritdoc />
public void Dispose() => this.Dispose(true);
/// <summary>
/// Releases unmanaged and - optionally - managed resources.
/// </summary>
/// <param name="alsoManaged"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.</param>
private void Dispose(Boolean alsoManaged) {
if(this._isDisposed) {
return;
}
if(alsoManaged) {
Add(this._identifier, this._stopwatch?.Elapsed ?? default);
this._stopwatch?.Stop();
}
this._stopwatch = null;
this._isDisposed = true;
}
}
}
}

View File

@ -1,32 +1,30 @@
namespace Swan.Diagnostics
{
using System;
using System.Diagnostics;
using System;
using System.Diagnostics;
namespace Swan.Diagnostics {
/// <summary>
/// Provides access to a high-resolution, time measuring device.
/// </summary>
/// <seealso cref="Stopwatch" />
public class HighResolutionTimer : Stopwatch {
/// <summary>
/// Provides access to a high-resolution, time measuring device.
/// Initializes a new instance of the <see cref="HighResolutionTimer"/> class.
/// </summary>
/// <seealso cref="Stopwatch" />
public class HighResolutionTimer : Stopwatch
{
/// <summary>
/// Initializes a new instance of the <see cref="HighResolutionTimer"/> class.
/// </summary>
/// <exception cref="NotSupportedException">High-resolution timer not available.</exception>
public HighResolutionTimer()
{
if (!IsHighResolution)
throw new NotSupportedException("High-resolution timer not available");
}
/// <summary>
/// Gets the number of microseconds per timer tick.
/// </summary>
public static double MicrosecondsPerTick { get; } = 1000000d / Frequency;
/// <summary>
/// Gets the elapsed microseconds.
/// </summary>
public long ElapsedMicroseconds => (long)(ElapsedTicks * MicrosecondsPerTick);
}
/// <exception cref="NotSupportedException">High-resolution timer not available.</exception>
public HighResolutionTimer() {
if(!IsHighResolution) {
throw new NotSupportedException("High-resolution timer not available");
}
}
/// <summary>
/// Gets the number of microseconds per timer tick.
/// </summary>
public static Double MicrosecondsPerTick { get; } = 1000000d / Frequency;
/// <summary>
/// Gets the elapsed microseconds.
/// </summary>
public Int64 ElapsedMicroseconds => (Int64)(this.ElapsedTicks * MicrosecondsPerTick);
}
}

View File

@ -1,171 +1,114 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Swan.Collections;
namespace Swan
{
namespace Swan {
/// <summary>
/// Provide Enumerations helpers with internal cache.
/// </summary>
public class EnumHelper
: SingletonBase<CollectionCacheRepository<Tuple<String, Object>>> {
/// <summary>
/// Provide Enumerations helpers with internal cache.
/// Gets all the names and enumerators from a specific Enum type.
/// </summary>
public class EnumHelper
: SingletonBase<CollectionCacheRepository<Tuple<string, object>>>
{
/// <summary>
/// Gets all the names and enumerators from a specific Enum type.
/// </summary>
/// <typeparam name="T">The type of the attribute to be retrieved.</typeparam>
/// <returns>A tuple of enumerator names and their value stored for the specified type.</returns>
public static IEnumerable<Tuple<string, object>> Retrieve<T>()
where T : struct, IConvertible
{
return Instance.Retrieve(typeof(T), t => Enum.GetValues(t)
.Cast<object>()
.Select(item => Tuple.Create(Enum.GetName(t, item), item)));
}
/// <summary>
/// Gets the cached items with the enum item value.
/// </summary>
/// <typeparam name="T">The type of enumeration.</typeparam>
/// <param name="humanize">if set to <c>true</c> [humanize].</param>
/// <returns>
/// A collection of Type/Tuple pairs
/// that represents items with the enum item value.
/// </returns>
public static IEnumerable<Tuple<int, string>> GetItemsWithValue<T>(bool humanize = true)
where T : struct, IConvertible
{
return Retrieve<T>()
.Select(x => Tuple.Create((int) x.Item2, humanize ? x.Item1.Humanize() : x.Item1));
}
/// <summary>
/// Gets the flag values.
/// </summary>
/// <typeparam name="TEnum">The type of the enum.</typeparam>
/// <param name="value">The value.</param>
/// <param name="ignoreZero">if set to <c>true</c> [ignore zero].</param>
/// <returns>
/// A list of values in the flag.
/// </returns>
public static IEnumerable<int> GetFlagValues<TEnum>(int value, bool ignoreZero = false)
where TEnum : struct, IConvertible
{
return Retrieve<TEnum>()
.Select(x => (int) x.Item2)
.When(() => ignoreZero, q => q.Where(f => f != 0))
.Where(x => (x & value) == x);
}
/// <summary>
/// Gets the flag values.
/// </summary>
/// <typeparam name="TEnum">The type of the enum.</typeparam>
/// <param name="value">The value.</param>
/// <param name="ignoreZero">if set to <c>true</c> [ignore zero].</param>
/// <returns>
/// A list of values in the flag.
/// </returns>
public static IEnumerable<long> GetFlagValues<TEnum>(long value, bool ignoreZero = false)
where TEnum : struct, IConvertible
{
return Retrieve<TEnum>()
.Select(x => (long) x.Item2)
.When(() => ignoreZero, q => q.Where(f => f != 0))
.Where(x => (x & value) == x);
}
/// <summary>
/// Gets the flag values.
/// </summary>
/// <typeparam name="TEnum">The type of the enum.</typeparam>
/// <param name="value">The value.</param>
/// <param name="ignoreZero">if set to <c>true</c> [ignore zero].</param>
/// <returns>
/// A list of values in the flag.
/// </returns>
public static IEnumerable<byte> GetFlagValues<TEnum>(byte value, bool ignoreZero = false)
where TEnum : struct, IConvertible
{
return Retrieve<TEnum>()
.Select(x => (byte) x.Item2)
.When(() => ignoreZero, q => q.Where(f => f != 0))
.Where(x => (x & value) == x);
}
/// <summary>
/// Gets the flag names.
/// </summary>
/// <typeparam name="TEnum">The type of the enum.</typeparam>
/// <param name="value">the value.</param>
/// <param name="ignoreZero">if set to <c>true</c> [ignore zero].</param>
/// <param name="humanize">if set to <c>true</c> [humanize].</param>
/// <returns>
/// A list of flag names.
/// </returns>
public static IEnumerable<string> GetFlagNames<TEnum>(int value, bool ignoreZero = false, bool humanize = true)
where TEnum : struct, IConvertible
{
return Retrieve<TEnum>()
.When(() => ignoreZero, q => q.Where(f => (int) f.Item2 != 0))
.Where(x => ((int) x.Item2 & value) == (int) x.Item2)
.Select(x => humanize ? x.Item1.Humanize() : x.Item1);
}
/// <summary>
/// Gets the flag names.
/// </summary>
/// <typeparam name="TEnum">The type of the enum.</typeparam>
/// <param name="value">The value.</param>
/// <param name="ignoreZero">if set to <c>true</c> [ignore zero].</param>
/// <param name="humanize">if set to <c>true</c> [humanize].</param>
/// <returns>
/// A list of flag names.
/// </returns>
public static IEnumerable<string> GetFlagNames<TEnum>(long value, bool ignoreZero = false, bool humanize = true)
where TEnum : struct, IConvertible
{
return Retrieve<TEnum>()
.When(() => ignoreZero, q => q.Where(f => (long) f.Item2 != 0))
.Where(x => ((long) x.Item2 & value) == (long) x.Item2)
.Select(x => humanize ? x.Item1.Humanize() : x.Item1);
}
/// <summary>
/// Gets the flag names.
/// </summary>
/// <typeparam name="TEnum">The type of the enum.</typeparam>
/// <param name="value">The value.</param>
/// <param name="ignoreZero">if set to <c>true</c> [ignore zero].</param>
/// <param name="humanize">if set to <c>true</c> [humanize].</param>
/// <returns>
/// A list of flag names.
/// </returns>
public static IEnumerable<string> GetFlagNames<TEnum>(byte value, bool ignoreZero = false, bool humanize = true)
where TEnum : struct, IConvertible
{
return Retrieve<TEnum>()
.When(() => ignoreZero, q => q.Where(f => (byte) f.Item2 != 0))
.Where(x => ((byte) x.Item2 & value) == (byte) x.Item2)
.Select(x => humanize ? x.Item1.Humanize() : x.Item1);
}
/// <summary>
/// Gets the cached items with the enum item index.
/// </summary>
/// <typeparam name="T">The type of enumeration.</typeparam>
/// <param name="humanize">if set to <c>true</c> [humanize].</param>
/// <returns>
/// A collection of Type/Tuple pairs that represents items with the enum item value.
/// </returns>
public static IEnumerable<Tuple<int, string>> GetItemsWithIndex<T>(bool humanize = true)
where T : struct, IConvertible
{
var i = 0;
return Retrieve<T>()
.Select(x => Tuple.Create(i++, humanize ? x.Item1.Humanize() : x.Item1));
}
}
/// <typeparam name="T">The type of the attribute to be retrieved.</typeparam>
/// <returns>A tuple of enumerator names and their value stored for the specified type.</returns>
public static IEnumerable<Tuple<String, Object>> Retrieve<T>() where T : struct, IConvertible => Instance.Retrieve(typeof(T), t => Enum.GetValues(t).Cast<Object>().Select(item => Tuple.Create(Enum.GetName(t, item), item)));
/// <summary>
/// Gets the cached items with the enum item value.
/// </summary>
/// <typeparam name="T">The type of enumeration.</typeparam>
/// <param name="humanize">if set to <c>true</c> [humanize].</param>
/// <returns>
/// A collection of Type/Tuple pairs
/// that represents items with the enum item value.
/// </returns>
public static IEnumerable<Tuple<Int32, String>> GetItemsWithValue<T>(Boolean humanize = true) where T : struct, IConvertible => Retrieve<T>().Select(x => Tuple.Create((Int32)x.Item2, humanize ? x.Item1.Humanize() : x.Item1));
/// <summary>
/// Gets the flag values.
/// </summary>
/// <typeparam name="TEnum">The type of the enum.</typeparam>
/// <param name="value">The value.</param>
/// <param name="ignoreZero">if set to <c>true</c> [ignore zero].</param>
/// <returns>
/// A list of values in the flag.
/// </returns>
public static IEnumerable<Int32> GetFlagValues<TEnum>(Int32 value, Boolean ignoreZero = false) where TEnum : struct, IConvertible => Retrieve<TEnum>().Select(x => (Int32)x.Item2).When(() => ignoreZero, q => q.Where(f => f != 0)).Where(x => (x & value) == x);
/// <summary>
/// Gets the flag values.
/// </summary>
/// <typeparam name="TEnum">The type of the enum.</typeparam>
/// <param name="value">The value.</param>
/// <param name="ignoreZero">if set to <c>true</c> [ignore zero].</param>
/// <returns>
/// A list of values in the flag.
/// </returns>
public static IEnumerable<Int64> GetFlagValues<TEnum>(Int64 value, Boolean ignoreZero = false) where TEnum : struct, IConvertible => Retrieve<TEnum>().Select(x => (Int64)x.Item2).When(() => ignoreZero, q => q.Where(f => f != 0)).Where(x => (x & value) == x);
/// <summary>
/// Gets the flag values.
/// </summary>
/// <typeparam name="TEnum">The type of the enum.</typeparam>
/// <param name="value">The value.</param>
/// <param name="ignoreZero">if set to <c>true</c> [ignore zero].</param>
/// <returns>
/// A list of values in the flag.
/// </returns>
public static IEnumerable<Byte> GetFlagValues<TEnum>(Byte value, Boolean ignoreZero = false) where TEnum : struct, IConvertible => Retrieve<TEnum>().Select(x => (Byte)x.Item2).When(() => ignoreZero, q => q.Where(f => f != 0)).Where(x => (x & value) == x);
/// <summary>
/// Gets the flag names.
/// </summary>
/// <typeparam name="TEnum">The type of the enum.</typeparam>
/// <param name="value">the value.</param>
/// <param name="ignoreZero">if set to <c>true</c> [ignore zero].</param>
/// <param name="humanize">if set to <c>true</c> [humanize].</param>
/// <returns>
/// A list of flag names.
/// </returns>
public static IEnumerable<String> GetFlagNames<TEnum>(Int32 value, Boolean ignoreZero = false, Boolean humanize = true) where TEnum : struct, IConvertible => Retrieve<TEnum>().When(() => ignoreZero, q => q.Where(f => (Int32)f.Item2 != 0)).Where(x => ((Int32)x.Item2 & value) == (Int32)x.Item2).Select(x => humanize ? x.Item1.Humanize() : x.Item1);
/// <summary>
/// Gets the flag names.
/// </summary>
/// <typeparam name="TEnum">The type of the enum.</typeparam>
/// <param name="value">The value.</param>
/// <param name="ignoreZero">if set to <c>true</c> [ignore zero].</param>
/// <param name="humanize">if set to <c>true</c> [humanize].</param>
/// <returns>
/// A list of flag names.
/// </returns>
public static IEnumerable<String> GetFlagNames<TEnum>(Int64 value, Boolean ignoreZero = false, Boolean humanize = true) where TEnum : struct, IConvertible => Retrieve<TEnum>().When(() => ignoreZero, q => q.Where(f => (Int64)f.Item2 != 0)).Where(x => ((Int64)x.Item2 & value) == (Int64)x.Item2).Select(x => humanize ? x.Item1.Humanize() : x.Item1);
/// <summary>
/// Gets the flag names.
/// </summary>
/// <typeparam name="TEnum">The type of the enum.</typeparam>
/// <param name="value">The value.</param>
/// <param name="ignoreZero">if set to <c>true</c> [ignore zero].</param>
/// <param name="humanize">if set to <c>true</c> [humanize].</param>
/// <returns>
/// A list of flag names.
/// </returns>
public static IEnumerable<String> GetFlagNames<TEnum>(Byte value, Boolean ignoreZero = false, Boolean humanize = true) where TEnum : struct, IConvertible => Retrieve<TEnum>().When(() => ignoreZero, q => q.Where(f => (Byte)f.Item2 != 0)).Where(x => ((Byte)x.Item2 & value) == (Byte)x.Item2).Select(x => humanize ? x.Item1.Humanize() : x.Item1);
/// <summary>
/// Gets the cached items with the enum item index.
/// </summary>
/// <typeparam name="T">The type of enumeration.</typeparam>
/// <param name="humanize">if set to <c>true</c> [humanize].</param>
/// <returns>
/// A collection of Type/Tuple pairs that represents items with the enum item value.
/// </returns>
public static IEnumerable<Tuple<Int32, String>> GetItemsWithIndex<T>(Boolean humanize = true) where T : struct, IConvertible {
Int32 i = 0;
return Retrieve<T>().Select(x => Tuple.Create(i++, humanize ? x.Item1.Humanize() : x.Item1));
}
}
}

View File

@ -1,65 +1,61 @@
namespace Swan
{
namespace Swan {
/// <summary>
/// Enumeration of Operating Systems.
/// </summary>
public enum OperatingSystem {
/// <summary>
/// Enumeration of Operating Systems.
/// Unknown OS
/// </summary>
public enum OperatingSystem
{
/// <summary>
/// Unknown OS
/// </summary>
Unknown,
/// <summary>
/// Windows
/// </summary>
Windows,
/// <summary>
/// UNIX/Linux
/// </summary>
Unix,
/// <summary>
/// macOS (OSX)
/// </summary>
Osx,
}
Unknown,
/// <summary>
/// Defines Endianness, big or little.
/// Windows
/// </summary>
public enum Endianness
{
/// <summary>
/// In big endian, you store the most significant byte in the smallest address.
/// </summary>
Big,
/// <summary>
/// In little endian, you store the least significant byte in the smallest address.
/// </summary>
Little,
}
Windows,
/// <summary>
/// Enumerates the JSON serializer cases to use: None (keeps the same case), PascalCase, or camelCase.
/// UNIX/Linux
/// </summary>
public enum JsonSerializerCase
{
/// <summary>
/// The none
/// </summary>
None,
/// <summary>
/// The pascal case (eg. PascalCase)
/// </summary>
PascalCase,
/// <summary>
/// The camel case (eg. camelCase)
/// </summary>
CamelCase,
}
Unix,
/// <summary>
/// macOS (OSX)
/// </summary>
Osx,
}
/// <summary>
/// Defines Endianness, big or little.
/// </summary>
public enum Endianness {
/// <summary>
/// In big endian, you store the most significant byte in the smallest address.
/// </summary>
Big,
/// <summary>
/// In little endian, you store the least significant byte in the smallest address.
/// </summary>
Little,
}
/// <summary>
/// Enumerates the JSON serializer cases to use: None (keeps the same case), PascalCase, or camelCase.
/// </summary>
public enum JsonSerializerCase {
/// <summary>
/// The none
/// </summary>
None,
/// <summary>
/// The pascal case (eg. PascalCase)
/// </summary>
PascalCase,
/// <summary>
/// The camel case (eg. camelCase)
/// </summary>
CamelCase,
}
}

View File

@ -7,498 +7,492 @@ using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace Swan
{
namespace Swan {
/// <summary>
/// Provides various extension methods for byte arrays and streams.
/// </summary>
public static class ByteArrayExtensions {
/// <summary>
/// Provides various extension methods for byte arrays and streams.
/// Converts an array of bytes to its lower-case, hexadecimal representation.
/// </summary>
public static class ByteArrayExtensions
{
/// <summary>
/// Converts an array of bytes to its lower-case, hexadecimal representation.
/// </summary>
/// <param name="bytes">The bytes.</param>
/// <param name="addPrefix">if set to <c>true</c> add the 0x prefix tot he output.</param>
/// <returns>
/// The specified string instance; no actual conversion is performed.
/// </returns>
/// <exception cref="ArgumentNullException">bytes.</exception>
public static string ToLowerHex(this byte[] bytes, bool addPrefix = false)
=> ToHex(bytes, addPrefix, "x2");
/// <summary>
/// Converts an array of bytes to its upper-case, hexadecimal representation.
/// </summary>
/// <param name="bytes">The bytes.</param>
/// <param name="addPrefix">if set to <c>true</c> [add prefix].</param>
/// <returns>
/// The specified string instance; no actual conversion is performed.
/// </returns>
/// <exception cref="ArgumentNullException">bytes.</exception>
public static string ToUpperHex(this byte[] bytes, bool addPrefix = false)
=> ToHex(bytes, addPrefix, "X2");
/// <summary>
/// Converts an array of bytes to a sequence of dash-separated, hexadecimal,
/// uppercase characters.
/// </summary>
/// <param name="bytes">The bytes.</param>
/// <returns>
/// A string of hexadecimal pairs separated by hyphens, where each pair represents
/// the corresponding element in value; for example, "7F-2C-4A-00".
/// </returns>
public static string ToDashedHex(this byte[] bytes) => BitConverter.ToString(bytes);
/// <summary>
/// Converts an array of bytes to a base-64 encoded string.
/// </summary>
/// <param name="bytes">The bytes.</param>
/// <returns>A <see cref="string" /> converted from an array of bytes.</returns>
public static string ToBase64(this byte[] bytes) => Convert.ToBase64String(bytes);
/// <summary>
/// Converts a set of hexadecimal characters (uppercase or lowercase)
/// to a byte array. String length must be a multiple of 2 and
/// any prefix (such as 0x) has to be avoided for this to work properly.
/// </summary>
/// <param name="this">The hexadecimal.</param>
/// <returns>
/// A byte array containing the results of encoding the specified set of characters.
/// </returns>
/// <exception cref="ArgumentNullException">hex.</exception>
public static byte[] ConvertHexadecimalToBytes(this string @this)
{
if (string.IsNullOrWhiteSpace(@this))
throw new ArgumentNullException(nameof(@this));
return Enumerable
.Range(0, @this.Length / 2)
.Select(x => Convert.ToByte(@this.Substring(x * 2, 2), 16))
.ToArray();
}
/// <summary>
/// Gets the bit value at the given offset.
/// </summary>
/// <param name="this">The b.</param>
/// <param name="offset">The offset.</param>
/// <param name="length">The length.</param>
/// <returns>
/// Bit value at the given offset.
/// </returns>
public static byte GetBitValueAt(this byte @this, byte offset, byte length = 1) => (byte)((@this >> offset) & ~(0xff << length));
/// <summary>
/// Sets the bit value at the given offset.
/// </summary>
/// <param name="this">The b.</param>
/// <param name="offset">The offset.</param>
/// <param name="length">The length.</param>
/// <param name="value">The value.</param>
/// <returns>Bit value at the given offset.</returns>
public static byte SetBitValueAt(this byte @this, byte offset, byte length, byte value)
{
var mask = ~(0xff << length);
var valueAt = (byte)(value & mask);
return (byte)((valueAt << offset) | (@this & ~(mask << offset)));
}
/// <summary>
/// Sets the bit value at the given offset.
/// </summary>
/// <param name="this">The b.</param>
/// <param name="offset">The offset.</param>
/// <param name="value">The value.</param>
/// <returns>Bit value at the given offset.</returns>
public static byte SetBitValueAt(this byte @this, byte offset, byte value) => @this.SetBitValueAt(offset, 1, value);
/// <summary>
/// Splits a byte array delimited by the specified sequence of bytes.
/// Each individual element in the result will contain the split sequence terminator if it is found to be delimited by it.
/// For example if you split [1,2,3,4] by a sequence of [2,3] this method will return a list with 2 byte arrays, one containing [1,2,3] and the
/// second one containing 4. Use the Trim extension methods to remove terminator sequences.
/// </summary>
/// <param name="this">The buffer.</param>
/// <param name="offset">The offset at which to start splitting bytes. Any bytes before this will be discarded.</param>
/// <param name="sequence">The sequence.</param>
/// <returns>
/// A byte array containing the results the specified sequence of bytes.
/// </returns>
/// <exception cref="System.ArgumentNullException">
/// buffer
/// or
/// sequence.
/// </exception>
public static List<byte[]> Split(this byte[] @this, int offset, params byte[] sequence)
{
if (@this == null)
throw new ArgumentNullException(nameof(@this));
if (sequence == null)
throw new ArgumentNullException(nameof(sequence));
var seqOffset = offset.Clamp(0, @this.Length - 1);
var result = new List<byte[]>();
while (seqOffset < @this.Length)
{
var separatorStartIndex = @this.GetIndexOf(sequence, seqOffset);
if (separatorStartIndex >= 0)
{
var item = new byte[separatorStartIndex - seqOffset + sequence.Length];
Array.Copy(@this, seqOffset, item, 0, item.Length);
result.Add(item);
seqOffset += item.Length;
}
else
{
var item = new byte[@this.Length - seqOffset];
Array.Copy(@this, seqOffset, item, 0, item.Length);
result.Add(item);
break;
}
}
return result;
}
/// <summary>
/// Clones the specified buffer, byte by byte.
/// </summary>
/// <param name="this">The buffer.</param>
/// <returns>
/// A byte array containing the results of encoding the specified set of characters.
/// </returns>
/// <exception cref="System.ArgumentNullException">this</exception>
public static byte[] DeepClone(this byte[] @this)
{
if (@this == null)
throw new ArgumentNullException(nameof(@this));
var result = new byte[@this.Length];
Array.Copy(@this, result, @this.Length);
return result;
}
/// <summary>
/// Removes the specified sequence from the start of the buffer if the buffer begins with such sequence.
/// </summary>
/// <param name="buffer">The buffer.</param>
/// <param name="sequence">The sequence.</param>
/// <returns>
/// A new trimmed byte array.
/// </returns>
/// <exception cref="ArgumentNullException">buffer.</exception>
public static byte[] TrimStart(this byte[] buffer, params byte[] sequence)
{
if (buffer == null)
throw new ArgumentNullException(nameof(buffer));
if (buffer.StartsWith(sequence) == false)
return buffer.DeepClone();
var result = new byte[buffer.Length - sequence.Length];
Array.Copy(buffer, sequence.Length, result, 0, result.Length);
return result;
}
/// <summary>
/// Removes the specified sequence from the end of the buffer if the buffer ends with such sequence.
/// </summary>
/// <param name="buffer">The buffer.</param>
/// <param name="sequence">The sequence.</param>
/// <returns>
/// A byte array containing the results of encoding the specified set of characters.
/// </returns>
/// <exception cref="ArgumentNullException">buffer.</exception>
public static byte[] TrimEnd(this byte[] buffer, params byte[] sequence)
{
if (buffer == null)
throw new ArgumentNullException(nameof(buffer));
if (buffer.EndsWith(sequence) == false)
return buffer.DeepClone();
var result = new byte[buffer.Length - sequence.Length];
Array.Copy(buffer, 0, result, 0, result.Length);
return result;
}
/// <summary>
/// Removes the specified sequence from the end and the start of the buffer
/// if the buffer ends and/or starts with such sequence.
/// </summary>
/// <param name="buffer">The buffer.</param>
/// <param name="sequence">The sequence.</param>
/// <returns>A byte array containing the results of encoding the specified set of characters.</returns>
public static byte[] Trim(this byte[] buffer, params byte[] sequence)
{
var trimStart = buffer.TrimStart(sequence);
return trimStart.TrimEnd(sequence);
}
/// <summary>
/// Determines if the specified buffer ends with the given sequence of bytes.
/// </summary>
/// <param name="buffer">The buffer.</param>
/// <param name="sequence">The sequence.</param>
/// <returns>
/// True if the specified buffer is ends; otherwise, false.
/// </returns>
/// <exception cref="ArgumentNullException">buffer.</exception>
public static bool EndsWith(this byte[] buffer, params byte[] sequence)
{
if (buffer == null)
throw new ArgumentNullException(nameof(buffer));
var startIndex = buffer.Length - sequence.Length;
return buffer.GetIndexOf(sequence, startIndex) == startIndex;
}
/// <summary>
/// Determines if the specified buffer starts with the given sequence of bytes.
/// </summary>
/// <param name="buffer">The buffer.</param>
/// <param name="sequence">The sequence.</param>
/// <returns><c>true</c> if the specified buffer starts; otherwise, <c>false</c>.</returns>
public static bool StartsWith(this byte[] buffer, params byte[] sequence) => buffer.GetIndexOf(sequence) == 0;
/// <summary>
/// Determines whether the buffer contains the specified sequence.
/// </summary>
/// <param name="buffer">The buffer.</param>
/// <param name="sequence">The sequence.</param>
/// <returns>
/// <c>true</c> if [contains] [the specified sequence]; otherwise, <c>false</c>.
/// </returns>
public static bool Contains(this byte[] buffer, params byte[] sequence) => buffer.GetIndexOf(sequence) >= 0;
/// <summary>
/// Determines whether the buffer exactly matches, byte by byte the specified sequence.
/// </summary>
/// <param name="buffer">The buffer.</param>
/// <param name="sequence">The sequence.</param>
/// <returns>
/// <c>true</c> if [is equal to] [the specified sequence]; otherwise, <c>false</c>.
/// </returns>
/// <exception cref="ArgumentNullException">buffer.</exception>
public static bool IsEqualTo(this byte[] buffer, params byte[] sequence)
{
if (ReferenceEquals(buffer, sequence))
return true;
if (buffer == null)
throw new ArgumentNullException(nameof(buffer));
return buffer.Length == sequence.Length && buffer.GetIndexOf(sequence) == 0;
}
/// <summary>
/// Returns the first instance of the matched sequence based on the given offset.
/// If no matches are found then this method returns -1.
/// </summary>
/// <param name="buffer">The buffer.</param>
/// <param name="sequence">The sequence.</param>
/// <param name="offset">The offset.</param>
/// <returns>The index of the sequence.</returns>
/// <exception cref="ArgumentNullException">
/// buffer
/// or
/// sequence.
/// </exception>
public static int GetIndexOf(this byte[] buffer, byte[] sequence, int offset = 0)
{
if (buffer == null)
throw new ArgumentNullException(nameof(buffer));
if (sequence == null)
throw new ArgumentNullException(nameof(sequence));
if (sequence.Length == 0)
return -1;
if (sequence.Length > buffer.Length)
return -1;
var seqOffset = offset < 0 ? 0 : offset;
var matchedCount = 0;
for (var i = seqOffset; i < buffer.Length; i++)
{
if (buffer[i] == sequence[matchedCount])
matchedCount++;
else
matchedCount = 0;
if (matchedCount == sequence.Length)
return i - (matchedCount - 1);
}
return -1;
}
/// <summary>
/// Appends the Memory Stream with the specified buffer.
/// </summary>
/// <param name="stream">The stream.</param>
/// <param name="buffer">The buffer.</param>
/// <returns>
/// The same MemoryStream instance.
/// </returns>
/// <exception cref="ArgumentNullException">
/// stream
/// or
/// buffer.
/// </exception>
public static MemoryStream Append(this MemoryStream stream, byte[] buffer)
{
if (stream == null)
throw new ArgumentNullException(nameof(stream));
if (buffer == null)
throw new ArgumentNullException(nameof(buffer));
stream.Write(buffer, 0, buffer.Length);
return stream;
}
/// <summary>
/// Appends the Memory Stream with the specified buffer.
/// </summary>
/// <param name="stream">The stream.</param>
/// <param name="buffer">The buffer.</param>
/// <returns>
/// Block of bytes to the current stream using data read from a buffer.
/// </returns>
/// <exception cref="ArgumentNullException">buffer.</exception>
public static MemoryStream Append(this MemoryStream stream, IEnumerable<byte> buffer) => Append(stream, buffer?.ToArray());
/// <summary>
/// Appends the Memory Stream with the specified set of buffers.
/// </summary>
/// <param name="stream">The stream.</param>
/// <param name="buffers">The buffers.</param>
/// <returns>
/// Block of bytes to the current stream using data read from a buffer.
/// </returns>
/// <exception cref="ArgumentNullException">buffers.</exception>
public static MemoryStream Append(this MemoryStream stream, IEnumerable<byte[]> buffers)
{
if (buffers == null)
throw new ArgumentNullException(nameof(buffers));
foreach (var buffer in buffers)
Append(stream, buffer);
return stream;
}
/// <summary>
/// Converts an array of bytes into text with the specified encoding.
/// </summary>
/// <param name="buffer">The buffer.</param>
/// <param name="encoding">The encoding.</param>
/// <returns>A <see cref="System.String" /> that contains the results of decoding the specified sequence of bytes.</returns>
public static string ToText(this IEnumerable<byte> buffer, Encoding encoding) => encoding.GetString(buffer.ToArray());
/// <summary>
/// Converts an array of bytes into text with UTF8 encoding.
/// </summary>
/// <param name="buffer">The buffer.</param>
/// <returns>A <see cref="System.String" /> that contains the results of decoding the specified sequence of bytes.</returns>
public static string ToText(this IEnumerable<byte> buffer) => buffer.ToText(Encoding.UTF8);
/// <summary>
/// Reads the bytes asynchronous.
/// </summary>
/// <param name="stream">The stream.</param>
/// <param name="length">The length.</param>
/// <param name="bufferLength">Length of the buffer.</param>
/// <param name="cancellationToken">The cancellation token.</param>
/// <returns>
/// A byte array containing the results of encoding the specified set of characters.
/// </returns>
/// <exception cref="ArgumentNullException">stream.</exception>
public static async Task<byte[]> ReadBytesAsync(this Stream stream, long length, int bufferLength, CancellationToken cancellationToken = default)
{
if (stream == null)
throw new ArgumentNullException(nameof(stream));
using (var dest = new MemoryStream())
{
try
{
var buff = new byte[bufferLength];
while (length > 0)
{
if (length < bufferLength)
bufferLength = (int)length;
var nread = await stream.ReadAsync(buff, 0, bufferLength, cancellationToken).ConfigureAwait(false);
if (nread == 0)
break;
dest.Write(buff, 0, nread);
length -= nread;
}
}
catch
{
// ignored
}
return dest.ToArray();
}
}
/// <summary>
/// Reads the bytes asynchronous.
/// </summary>
/// <param name="stream">The stream.</param>
/// <param name="length">The length.</param>
/// <param name="cancellationToken">The cancellation token.</param>
/// <returns>
/// A byte array containing the results of encoding the specified set of characters.
/// </returns>
/// <exception cref="ArgumentNullException">stream.</exception>
public static async Task<byte[]> ReadBytesAsync(this Stream stream, int length, CancellationToken cancellationToken = default)
{
if (stream == null)
throw new ArgumentNullException(nameof(stream));
var buff = new byte[length];
var offset = 0;
try
{
while (length > 0)
{
var nread = await stream.ReadAsync(buff, offset, length, cancellationToken).ConfigureAwait(false);
if (nread == 0)
break;
offset += nread;
length -= nread;
}
}
catch
{
// ignored
}
return new ArraySegment<byte>(buff, 0, offset).ToArray();
}
private static string ToHex(byte[] bytes, bool addPrefix, string format)
{
if (bytes == null)
throw new ArgumentNullException(nameof(bytes));
var sb = new StringBuilder(bytes.Length * 2);
foreach (var item in bytes)
sb.Append(item.ToString(format, CultureInfo.InvariantCulture));
return $"{(addPrefix ? "0x" : string.Empty)}{sb}";
}
}
/// <param name="bytes">The bytes.</param>
/// <param name="addPrefix">if set to <c>true</c> add the 0x prefix tot he output.</param>
/// <returns>
/// The specified string instance; no actual conversion is performed.
/// </returns>
/// <exception cref="ArgumentNullException">bytes.</exception>
public static String ToLowerHex(this Byte[] bytes, Boolean addPrefix = false) => ToHex(bytes, addPrefix, "x2");
/// <summary>
/// Converts an array of bytes to its upper-case, hexadecimal representation.
/// </summary>
/// <param name="bytes">The bytes.</param>
/// <param name="addPrefix">if set to <c>true</c> [add prefix].</param>
/// <returns>
/// The specified string instance; no actual conversion is performed.
/// </returns>
/// <exception cref="ArgumentNullException">bytes.</exception>
public static String ToUpperHex(this Byte[] bytes, Boolean addPrefix = false) => ToHex(bytes, addPrefix, "X2");
/// <summary>
/// Converts an array of bytes to a sequence of dash-separated, hexadecimal,
/// uppercase characters.
/// </summary>
/// <param name="bytes">The bytes.</param>
/// <returns>
/// A string of hexadecimal pairs separated by hyphens, where each pair represents
/// the corresponding element in value; for example, "7F-2C-4A-00".
/// </returns>
public static String ToDashedHex(this Byte[] bytes) => BitConverter.ToString(bytes);
/// <summary>
/// Converts an array of bytes to a base-64 encoded string.
/// </summary>
/// <param name="bytes">The bytes.</param>
/// <returns>A <see cref="String" /> converted from an array of bytes.</returns>
public static String ToBase64(this Byte[] bytes) => Convert.ToBase64String(bytes);
/// <summary>
/// Converts a set of hexadecimal characters (uppercase or lowercase)
/// to a byte array. String length must be a multiple of 2 and
/// any prefix (such as 0x) has to be avoided for this to work properly.
/// </summary>
/// <param name="this">The hexadecimal.</param>
/// <returns>
/// A byte array containing the results of encoding the specified set of characters.
/// </returns>
/// <exception cref="ArgumentNullException">hex.</exception>
public static Byte[] ConvertHexadecimalToBytes(this String @this) {
if(String.IsNullOrWhiteSpace(@this)) {
throw new ArgumentNullException(nameof(@this));
}
return Enumerable.Range(0, @this.Length / 2).Select(x => Convert.ToByte(@this.Substring(x * 2, 2), 16)).ToArray();
}
/// <summary>
/// Gets the bit value at the given offset.
/// </summary>
/// <param name="this">The b.</param>
/// <param name="offset">The offset.</param>
/// <param name="length">The length.</param>
/// <returns>
/// Bit value at the given offset.
/// </returns>
public static Byte GetBitValueAt(this Byte @this, Byte offset, Byte length = 1) => (Byte)((@this >> offset) & ~(0xff << length));
/// <summary>
/// Sets the bit value at the given offset.
/// </summary>
/// <param name="this">The b.</param>
/// <param name="offset">The offset.</param>
/// <param name="length">The length.</param>
/// <param name="value">The value.</param>
/// <returns>Bit value at the given offset.</returns>
public static Byte SetBitValueAt(this Byte @this, Byte offset, Byte length, Byte value) {
Int32 mask = ~(0xff << length);
Byte valueAt = (Byte)(value & mask);
return (Byte)((valueAt << offset) | (@this & ~(mask << offset)));
}
/// <summary>
/// Sets the bit value at the given offset.
/// </summary>
/// <param name="this">The b.</param>
/// <param name="offset">The offset.</param>
/// <param name="value">The value.</param>
/// <returns>Bit value at the given offset.</returns>
public static Byte SetBitValueAt(this Byte @this, Byte offset, Byte value) => @this.SetBitValueAt(offset, 1, value);
/// <summary>
/// Splits a byte array delimited by the specified sequence of bytes.
/// Each individual element in the result will contain the split sequence terminator if it is found to be delimited by it.
/// For example if you split [1,2,3,4] by a sequence of [2,3] this method will return a list with 2 byte arrays, one containing [1,2,3] and the
/// second one containing 4. Use the Trim extension methods to remove terminator sequences.
/// </summary>
/// <param name="this">The buffer.</param>
/// <param name="offset">The offset at which to start splitting bytes. Any bytes before this will be discarded.</param>
/// <param name="sequence">The sequence.</param>
/// <returns>
/// A byte array containing the results the specified sequence of bytes.
/// </returns>
/// <exception cref="System.ArgumentNullException">
/// buffer
/// or
/// sequence.
/// </exception>
public static List<Byte[]> Split(this Byte[] @this, Int32 offset, params Byte[] sequence) {
if(@this == null) {
throw new ArgumentNullException(nameof(@this));
}
if(sequence == null) {
throw new ArgumentNullException(nameof(sequence));
}
Int32 seqOffset = offset.Clamp(0, @this.Length - 1);
List<Byte[]> result = new List<Byte[]>();
while(seqOffset < @this.Length) {
Int32 separatorStartIndex = @this.GetIndexOf(sequence, seqOffset);
if(separatorStartIndex >= 0) {
Byte[] item = new Byte[separatorStartIndex - seqOffset + sequence.Length];
Array.Copy(@this, seqOffset, item, 0, item.Length);
result.Add(item);
seqOffset += item.Length;
} else {
Byte[] item = new Byte[@this.Length - seqOffset];
Array.Copy(@this, seqOffset, item, 0, item.Length);
result.Add(item);
break;
}
}
return result;
}
/// <summary>
/// Clones the specified buffer, byte by byte.
/// </summary>
/// <param name="this">The buffer.</param>
/// <returns>
/// A byte array containing the results of encoding the specified set of characters.
/// </returns>
/// <exception cref="System.ArgumentNullException">this</exception>
public static Byte[] DeepClone(this Byte[] @this) {
if(@this == null) {
throw new ArgumentNullException(nameof(@this));
}
Byte[] result = new Byte[@this.Length];
Array.Copy(@this, result, @this.Length);
return result;
}
/// <summary>
/// Removes the specified sequence from the start of the buffer if the buffer begins with such sequence.
/// </summary>
/// <param name="buffer">The buffer.</param>
/// <param name="sequence">The sequence.</param>
/// <returns>
/// A new trimmed byte array.
/// </returns>
/// <exception cref="ArgumentNullException">buffer.</exception>
public static Byte[] TrimStart(this Byte[] buffer, params Byte[] sequence) {
if(buffer == null) {
throw new ArgumentNullException(nameof(buffer));
}
if(buffer.StartsWith(sequence) == false) {
return buffer.DeepClone();
}
Byte[] result = new Byte[buffer.Length - sequence.Length];
Array.Copy(buffer, sequence.Length, result, 0, result.Length);
return result;
}
/// <summary>
/// Removes the specified sequence from the end of the buffer if the buffer ends with such sequence.
/// </summary>
/// <param name="buffer">The buffer.</param>
/// <param name="sequence">The sequence.</param>
/// <returns>
/// A byte array containing the results of encoding the specified set of characters.
/// </returns>
/// <exception cref="ArgumentNullException">buffer.</exception>
public static Byte[] TrimEnd(this Byte[] buffer, params Byte[] sequence) {
if(buffer == null) {
throw new ArgumentNullException(nameof(buffer));
}
if(buffer.EndsWith(sequence) == false) {
return buffer.DeepClone();
}
Byte[] result = new Byte[buffer.Length - sequence.Length];
Array.Copy(buffer, 0, result, 0, result.Length);
return result;
}
/// <summary>
/// Removes the specified sequence from the end and the start of the buffer
/// if the buffer ends and/or starts with such sequence.
/// </summary>
/// <param name="buffer">The buffer.</param>
/// <param name="sequence">The sequence.</param>
/// <returns>A byte array containing the results of encoding the specified set of characters.</returns>
public static Byte[] Trim(this Byte[] buffer, params Byte[] sequence) {
Byte[] trimStart = buffer.TrimStart(sequence);
return trimStart.TrimEnd(sequence);
}
/// <summary>
/// Determines if the specified buffer ends with the given sequence of bytes.
/// </summary>
/// <param name="buffer">The buffer.</param>
/// <param name="sequence">The sequence.</param>
/// <returns>
/// True if the specified buffer is ends; otherwise, false.
/// </returns>
/// <exception cref="ArgumentNullException">buffer.</exception>
public static Boolean EndsWith(this Byte[] buffer, params Byte[] sequence) {
if(buffer == null) {
throw new ArgumentNullException(nameof(buffer));
}
Int32 startIndex = buffer.Length - sequence.Length;
return buffer.GetIndexOf(sequence, startIndex) == startIndex;
}
/// <summary>
/// Determines if the specified buffer starts with the given sequence of bytes.
/// </summary>
/// <param name="buffer">The buffer.</param>
/// <param name="sequence">The sequence.</param>
/// <returns><c>true</c> if the specified buffer starts; otherwise, <c>false</c>.</returns>
public static Boolean StartsWith(this Byte[] buffer, params Byte[] sequence) => buffer.GetIndexOf(sequence) == 0;
/// <summary>
/// Determines whether the buffer contains the specified sequence.
/// </summary>
/// <param name="buffer">The buffer.</param>
/// <param name="sequence">The sequence.</param>
/// <returns>
/// <c>true</c> if [contains] [the specified sequence]; otherwise, <c>false</c>.
/// </returns>
public static Boolean Contains(this Byte[] buffer, params Byte[] sequence) => buffer.GetIndexOf(sequence) >= 0;
/// <summary>
/// Determines whether the buffer exactly matches, byte by byte the specified sequence.
/// </summary>
/// <param name="buffer">The buffer.</param>
/// <param name="sequence">The sequence.</param>
/// <returns>
/// <c>true</c> if [is equal to] [the specified sequence]; otherwise, <c>false</c>.
/// </returns>
/// <exception cref="ArgumentNullException">buffer.</exception>
public static Boolean IsEqualTo(this Byte[] buffer, params Byte[] sequence) {
if(ReferenceEquals(buffer, sequence)) {
return true;
}
if(buffer == null) {
throw new ArgumentNullException(nameof(buffer));
}
return buffer.Length == sequence.Length && buffer.GetIndexOf(sequence) == 0;
}
/// <summary>
/// Returns the first instance of the matched sequence based on the given offset.
/// If no matches are found then this method returns -1.
/// </summary>
/// <param name="buffer">The buffer.</param>
/// <param name="sequence">The sequence.</param>
/// <param name="offset">The offset.</param>
/// <returns>The index of the sequence.</returns>
/// <exception cref="ArgumentNullException">
/// buffer
/// or
/// sequence.
/// </exception>
public static Int32 GetIndexOf(this Byte[] buffer, Byte[] sequence, Int32 offset = 0) {
if(buffer == null) {
throw new ArgumentNullException(nameof(buffer));
}
if(sequence == null) {
throw new ArgumentNullException(nameof(sequence));
}
if(sequence.Length == 0) {
return -1;
}
if(sequence.Length > buffer.Length) {
return -1;
}
Int32 seqOffset = offset < 0 ? 0 : offset;
Int32 matchedCount = 0;
for(Int32 i = seqOffset; i < buffer.Length; i++) {
if(buffer[i] == sequence[matchedCount]) {
matchedCount++;
} else {
matchedCount = 0;
}
if(matchedCount == sequence.Length) {
return i - (matchedCount - 1);
}
}
return -1;
}
/// <summary>
/// Appends the Memory Stream with the specified buffer.
/// </summary>
/// <param name="stream">The stream.</param>
/// <param name="buffer">The buffer.</param>
/// <returns>
/// The same MemoryStream instance.
/// </returns>
/// <exception cref="ArgumentNullException">
/// stream
/// or
/// buffer.
/// </exception>
public static MemoryStream Append(this MemoryStream stream, Byte[] buffer) {
if(stream == null) {
throw new ArgumentNullException(nameof(stream));
}
if(buffer == null) {
throw new ArgumentNullException(nameof(buffer));
}
stream.Write(buffer, 0, buffer.Length);
return stream;
}
/// <summary>
/// Appends the Memory Stream with the specified buffer.
/// </summary>
/// <param name="stream">The stream.</param>
/// <param name="buffer">The buffer.</param>
/// <returns>
/// Block of bytes to the current stream using data read from a buffer.
/// </returns>
/// <exception cref="ArgumentNullException">buffer.</exception>
public static MemoryStream Append(this MemoryStream stream, IEnumerable<Byte> buffer) => Append(stream, buffer?.ToArray());
/// <summary>
/// Appends the Memory Stream with the specified set of buffers.
/// </summary>
/// <param name="stream">The stream.</param>
/// <param name="buffers">The buffers.</param>
/// <returns>
/// Block of bytes to the current stream using data read from a buffer.
/// </returns>
/// <exception cref="ArgumentNullException">buffers.</exception>
public static MemoryStream Append(this MemoryStream stream, IEnumerable<Byte[]> buffers) {
if(buffers == null) {
throw new ArgumentNullException(nameof(buffers));
}
foreach(Byte[] buffer in buffers) {
_ = Append(stream, buffer);
}
return stream;
}
/// <summary>
/// Converts an array of bytes into text with the specified encoding.
/// </summary>
/// <param name="buffer">The buffer.</param>
/// <param name="encoding">The encoding.</param>
/// <returns>A <see cref="System.String" /> that contains the results of decoding the specified sequence of bytes.</returns>
public static String ToText(this IEnumerable<Byte> buffer, Encoding encoding) => encoding.GetString(buffer.ToArray());
/// <summary>
/// Converts an array of bytes into text with UTF8 encoding.
/// </summary>
/// <param name="buffer">The buffer.</param>
/// <returns>A <see cref="System.String" /> that contains the results of decoding the specified sequence of bytes.</returns>
public static String ToText(this IEnumerable<Byte> buffer) => buffer.ToText(Encoding.UTF8);
/// <summary>
/// Reads the bytes asynchronous.
/// </summary>
/// <param name="stream">The stream.</param>
/// <param name="length">The length.</param>
/// <param name="bufferLength">Length of the buffer.</param>
/// <param name="cancellationToken">The cancellation token.</param>
/// <returns>
/// A byte array containing the results of encoding the specified set of characters.
/// </returns>
/// <exception cref="ArgumentNullException">stream.</exception>
public static async Task<Byte[]> ReadBytesAsync(this Stream stream, Int64 length, Int32 bufferLength, CancellationToken cancellationToken = default) {
if(stream == null) {
throw new ArgumentNullException(nameof(stream));
}
using MemoryStream dest = new MemoryStream();
try {
Byte[] buff = new Byte[bufferLength];
while(length > 0) {
if(length < bufferLength) {
bufferLength = (Int32)length;
}
Int32 nread = await stream.ReadAsync(buff, 0, bufferLength, cancellationToken).ConfigureAwait(false);
if(nread == 0) {
break;
}
dest.Write(buff, 0, nread);
length -= nread;
}
} catch {
// ignored
}
return dest.ToArray();
}
/// <summary>
/// Reads the bytes asynchronous.
/// </summary>
/// <param name="stream">The stream.</param>
/// <param name="length">The length.</param>
/// <param name="cancellationToken">The cancellation token.</param>
/// <returns>
/// A byte array containing the results of encoding the specified set of characters.
/// </returns>
/// <exception cref="ArgumentNullException">stream.</exception>
public static async Task<Byte[]> ReadBytesAsync(this Stream stream, Int32 length, CancellationToken cancellationToken = default) {
if(stream == null) {
throw new ArgumentNullException(nameof(stream));
}
Byte[] buff = new Byte[length];
Int32 offset = 0;
try {
while(length > 0) {
Int32 nread = await stream.ReadAsync(buff, offset, length, cancellationToken).ConfigureAwait(false);
if(nread == 0) {
break;
}
offset += nread;
length -= nread;
}
} catch {
// ignored
}
return new ArraySegment<Byte>(buff, 0, offset).ToArray();
}
private static String ToHex(Byte[] bytes, Boolean addPrefix, String format) {
if(bytes == null) {
throw new ArgumentNullException(nameof(bytes));
}
StringBuilder sb = new StringBuilder(bytes.Length * 2);
foreach(Byte item in bytes) {
_ = sb.Append(item.ToString(format, CultureInfo.InvariantCulture));
}
return $"{(addPrefix ? "0x" : String.Empty)}{sb}";
}
}
}

View File

@ -1,21 +1,20 @@
using System;
using Swan.Collections;
namespace Swan
{
namespace Swan {
/// <summary>
/// Provides extension methods for types implementing <see cref="IComponentCollection{T}"/>.
/// </summary>
public static class ComponentCollectionExtensions {
/// <summary>
/// Provides extension methods for types implementing <see cref="IComponentCollection{T}"/>.
/// Adds the specified component to a collection, without giving it a name.
/// </summary>
public static class ComponentCollectionExtensions
{
/// <summary>
/// Adds the specified component to a collection, without giving it a name.
/// </summary>
/// <typeparam name="T">The type of components in the collection.</typeparam>
/// <param name="this">The <see cref="IComponentCollection{T}" /> on which this method is called.</param>
/// <param name="component">The component to add.</param>
/// <exception cref="NullReferenceException"><paramref name="this" /> is <see langword="null" />.</exception>
/// <seealso cref="IComponentCollection{T}.Add" />
public static void Add<T>(this IComponentCollection<T> @this, T component) => @this.Add(null, component);
}
/// <typeparam name="T">The type of components in the collection.</typeparam>
/// <param name="this">The <see cref="IComponentCollection{T}" /> on which this method is called.</param>
/// <param name="component">The component to add.</param>
/// <exception cref="NullReferenceException"><paramref name="this" /> is <see langword="null" />.</exception>
/// <seealso cref="IComponentCollection{T}.Add" />
public static void Add<T>(this IComponentCollection<T> @this, T component) => @this.Add(null, component);
}
}

View File

@ -3,232 +3,224 @@ using System.Collections.Generic;
using System.Globalization;
using System.Linq;
namespace Swan
{
namespace Swan {
/// <summary>
/// Provides extension methods for <see cref="DateTime"/>.
/// </summary>
public static class DateExtensions {
private static readonly Dictionary<String, Int32> DateRanges = new Dictionary<String, Int32>() {
{ "minute", 59},
{ "hour", 23},
{ "dayOfMonth", 31},
{ "month", 12},
{ "dayOfWeek", 6},
};
/// <summary>
/// Provides extension methods for <see cref="DateTime"/>.
/// Converts the date to a YYYY-MM-DD string.
/// </summary>
public static class DateExtensions
{
private static readonly Dictionary<string, int> DateRanges = new Dictionary<string, int>()
{
{ "minute", 59},
{ "hour", 23},
{ "dayOfMonth", 31},
{ "month", 12},
{ "dayOfWeek", 6},
};
/// <summary>
/// Converts the date to a YYYY-MM-DD string.
/// </summary>
/// <param name="this">The <see cref="DateTime"/> on which this method is called.</param>
/// <returns>The concatenation of date.Year, date.Month and date.Day.</returns>
public static string ToSortableDate(this DateTime @this)
=> $"{@this.Year:0000}-{@this.Month:00}-{@this.Day:00}";
/// <summary>
/// Converts the date to a YYYY-MM-DD HH:II:SS string.
/// </summary>
/// <param name="this">The <see cref="DateTime"/> on which this method is called.</param>
/// <returns>The concatenation of date.Year, date.Month, date.Day, date.Hour, date.Minute and date.Second.</returns>
public static string ToSortableDateTime(this DateTime @this)
=> $"{@this.Year:0000}-{@this.Month:00}-{@this.Day:00} {@this.Hour:00}:{@this.Minute:00}:{@this.Second:00}";
/// <summary>
/// Parses a YYYY-MM-DD and optionally it time part, HH:II:SS into a DateTime.
/// </summary>
/// <param name="this">The sortable date.</param>
/// <returns>
/// A new instance of the DateTime structure to
/// the specified year, month, day, hour, minute and second.
/// </returns>
/// <exception cref="ArgumentNullException">sortableDate.</exception>
/// <exception cref="Exception">
/// Represents errors that occur during application execution.
/// </exception>
/// <exception cref="ArgumentException">
/// Unable to parse sortable date and time. - sortableDate.
/// </exception>
public static DateTime ToDateTime(this string @this)
{
if (string.IsNullOrWhiteSpace(@this))
throw new ArgumentNullException(nameof(@this));
var hour = 0;
var minute = 0;
var second = 0;
var dateTimeParts = @this.Split(' ');
try
{
if (dateTimeParts.Length != 1 && dateTimeParts.Length != 2)
throw new Exception();
var dateParts = dateTimeParts[0].Split('-');
if (dateParts.Length != 3) throw new Exception();
var year = int.Parse(dateParts[0]);
var month = int.Parse(dateParts[1]);
var day = int.Parse(dateParts[2]);
if (dateTimeParts.Length > 1)
{
var timeParts = dateTimeParts[1].Split(':');
if (timeParts.Length != 3) throw new Exception();
hour = int.Parse(timeParts[0]);
minute = int.Parse(timeParts[1]);
second = int.Parse(timeParts[2]);
}
return new DateTime(year, month, day, hour, minute, second);
}
catch (Exception)
{
throw new ArgumentException("Unable to parse sortable date and time.", nameof(@this));
}
}
/// <summary>
/// Creates a date range.
/// </summary>
/// <param name="startDate">The start date.</param>
/// <param name="endDate">The end date.</param>
/// <returns>
/// A sequence of integral numbers within a specified date's range.
/// </returns>
public static IEnumerable<DateTime> DateRange(this DateTime startDate, DateTime endDate)
=> Enumerable.Range(0, (endDate - startDate).Days + 1).Select(d => startDate.AddDays(d));
/// <summary>
/// Rounds up a date to match a timespan.
/// </summary>
/// <param name="date">The datetime.</param>
/// <param name="timeSpan">The timespan to match.</param>
/// <returns>
/// A new instance of the DateTime structure to the specified datetime and timespan ticks.
/// </returns>
public static DateTime RoundUp(this DateTime date, TimeSpan timeSpan)
=> new DateTime(((date.Ticks + timeSpan.Ticks - 1) / timeSpan.Ticks) * timeSpan.Ticks);
/// <summary>
/// Get this datetime as a Unix epoch timestamp (seconds since Jan 1, 1970, midnight UTC).
/// </summary>
/// <param name="this">The <see cref="DateTime"/> on which this method is called.</param>
/// <returns>Seconds since Unix epoch.</returns>
public static long ToUnixEpochDate(this DateTime @this) => new DateTimeOffset(@this).ToUniversalTime().ToUnixTimeSeconds();
/// <summary>
/// Compares a Date to another and returns a <c>DateTimeSpan</c>.
/// </summary>
/// <param name="dateStart">The date start.</param>
/// <param name="dateEnd">The date end.</param>
/// <returns>A DateTimeSpan with the Years, Months, Days, Hours, Minutes, Seconds and Milliseconds between the dates.</returns>
public static DateTimeSpan GetDateTimeSpan(this DateTime dateStart, DateTime dateEnd)
=> DateTimeSpan.CompareDates(dateStart, dateEnd);
/// <summary>
/// Compare the Date elements(Months, Days, Hours, Minutes).
/// </summary>
/// <param name="this">The <see cref="DateTime"/> on which this method is called.</param>
/// <param name="minute">The minute (0-59).</param>
/// <param name="hour">The hour. (0-23).</param>
/// <param name="dayOfMonth">The day of month. (1-31).</param>
/// <param name="month">The month. (1-12).</param>
/// <param name="dayOfWeek">The day of week. (0-6)(Sunday = 0).</param>
/// <returns>Returns <c>true</c> if Months, Days, Hours and Minutes match, otherwise <c>false</c>.</returns>
public static bool AsCronCanRun(this DateTime @this, int? minute = null, int? hour = null, int? dayOfMonth = null, int? month = null, int? dayOfWeek = null)
{
var results = new List<bool?>
{
GetElementParts(minute, @this.Minute),
GetElementParts(hour, @this.Hour),
GetElementParts(dayOfMonth, @this.Day),
GetElementParts(month, @this.Month),
GetElementParts(dayOfWeek, (int) @this.DayOfWeek),
};
return results.Any(x => x != false);
}
/// <summary>
/// Compare the Date elements(Months, Days, Hours, Minutes).
/// </summary>
/// <param name="this">The <see cref="DateTime"/> on which this method is called.</param>
/// <param name="minute">The minute (0-59).</param>
/// <param name="hour">The hour. (0-23).</param>
/// <param name="dayOfMonth">The day of month. (1-31).</param>
/// <param name="month">The month. (1-12).</param>
/// <param name="dayOfWeek">The day of week. (0-6)(Sunday = 0).</param>
/// <returns>Returns <c>true</c> if Months, Days, Hours and Minutes match, otherwise <c>false</c>.</returns>
public static bool AsCronCanRun(this DateTime @this, string minute = "*", string hour = "*", string dayOfMonth = "*", string month = "*", string dayOfWeek = "*")
{
var results = new List<bool?>
{
GetElementParts(minute, nameof(minute), @this.Minute),
GetElementParts(hour, nameof(hour), @this.Hour),
GetElementParts(dayOfMonth, nameof(dayOfMonth), @this.Day),
GetElementParts(month, nameof(month), @this.Month),
GetElementParts(dayOfWeek, nameof(dayOfWeek), (int) @this.DayOfWeek),
};
return results.Any(x => x != false);
}
/// <summary>
/// Converts a <see cref="DateTime"/> to the <see href="https://docs.microsoft.com/en-us/dotnet/standard/base-types/standard-date-and-time-format-strings#RFC1123">RFC1123 format</see>.
/// </summary>
/// <param name="this">The <see cref="DateTime"/> on which this method is called.</param>
/// <returns>The string representation of <paramref name="this"/> according to <see href="https://tools.ietf.org/html/rfc1123#page-54">RFC1123</see>.</returns>
/// <remarks>
/// <para>If <paramref name="this"/> is not a UTC date / time, its UTC equivalent is converted, leaving <paramref name="this"/> unchanged.</para>
/// </remarks>
public static string ToRfc1123String(this DateTime @this)
=> @this.ToUniversalTime().ToString("R", CultureInfo.InvariantCulture);
private static bool? GetElementParts(int? status, int value) => status.HasValue ? status.Value == value : (bool?) null;
private static bool? GetElementParts(string parts, string type, int value)
{
if (string.IsNullOrWhiteSpace(parts) || parts == "*")
return null;
if (parts.Contains(","))
{
return parts.Split(',').Select(int.Parse).Contains(value);
}
var stop = DateRanges[type];
if (parts.Contains("/"))
{
var multiple = int.Parse(parts.Split('/').Last());
var start = type == "dayOfMonth" || type == "month" ? 1 : 0;
for (var i = start; i <= stop; i += multiple)
if (i == value) return true;
return false;
}
if (parts.Contains("-"))
{
var range = parts.Split('-');
var start = int.Parse(range.First());
stop = Math.Max(stop, int.Parse(range.Last()));
if ((type == "dayOfMonth" || type == "month") && start == 0)
start = 1;
for (var i = start; i <= stop; i++)
if (i == value) return true;
return false;
}
return int.Parse(parts) == value;
}
}
/// <param name="this">The <see cref="DateTime"/> on which this method is called.</param>
/// <returns>The concatenation of date.Year, date.Month and date.Day.</returns>
public static String ToSortableDate(this DateTime @this) => $"{@this.Year:0000}-{@this.Month:00}-{@this.Day:00}";
/// <summary>
/// Converts the date to a YYYY-MM-DD HH:II:SS string.
/// </summary>
/// <param name="this">The <see cref="DateTime"/> on which this method is called.</param>
/// <returns>The concatenation of date.Year, date.Month, date.Day, date.Hour, date.Minute and date.Second.</returns>
public static String ToSortableDateTime(this DateTime @this) => $"{@this.Year:0000}-{@this.Month:00}-{@this.Day:00} {@this.Hour:00}:{@this.Minute:00}:{@this.Second:00}";
/// <summary>
/// Parses a YYYY-MM-DD and optionally it time part, HH:II:SS into a DateTime.
/// </summary>
/// <param name="this">The sortable date.</param>
/// <returns>
/// A new instance of the DateTime structure to
/// the specified year, month, day, hour, minute and second.
/// </returns>
/// <exception cref="ArgumentNullException">sortableDate.</exception>
/// <exception cref="Exception">
/// Represents errors that occur during application execution.
/// </exception>
/// <exception cref="ArgumentException">
/// Unable to parse sortable date and time. - sortableDate.
/// </exception>
public static DateTime ToDateTime(this String @this) {
if(String.IsNullOrWhiteSpace(@this)) {
throw new ArgumentNullException(nameof(@this));
}
Int32 hour = 0;
Int32 minute = 0;
Int32 second = 0;
String[] dateTimeParts = @this.Split(' ');
try {
if(dateTimeParts.Length != 1 && dateTimeParts.Length != 2) {
throw new Exception();
}
String[] dateParts = dateTimeParts[0].Split('-');
if(dateParts.Length != 3) {
throw new Exception();
}
Int32 year = Int32.Parse(dateParts[0]);
Int32 month = Int32.Parse(dateParts[1]);
Int32 day = Int32.Parse(dateParts[2]);
if(dateTimeParts.Length > 1) {
String[] timeParts = dateTimeParts[1].Split(':');
if(timeParts.Length != 3) {
throw new Exception();
}
hour = Int32.Parse(timeParts[0]);
minute = Int32.Parse(timeParts[1]);
second = Int32.Parse(timeParts[2]);
}
return new DateTime(year, month, day, hour, minute, second);
} catch(Exception) {
throw new ArgumentException("Unable to parse sortable date and time.", nameof(@this));
}
}
/// <summary>
/// Creates a date range.
/// </summary>
/// <param name="startDate">The start date.</param>
/// <param name="endDate">The end date.</param>
/// <returns>
/// A sequence of integral numbers within a specified date's range.
/// </returns>
public static IEnumerable<DateTime> DateRange(this DateTime startDate, DateTime endDate) => Enumerable.Range(0, (endDate - startDate).Days + 1).Select(d => startDate.AddDays(d));
/// <summary>
/// Rounds up a date to match a timespan.
/// </summary>
/// <param name="date">The datetime.</param>
/// <param name="timeSpan">The timespan to match.</param>
/// <returns>
/// A new instance of the DateTime structure to the specified datetime and timespan ticks.
/// </returns>
public static DateTime RoundUp(this DateTime date, TimeSpan timeSpan) => new DateTime(((date.Ticks + timeSpan.Ticks - 1) / timeSpan.Ticks) * timeSpan.Ticks);
/// <summary>
/// Get this datetime as a Unix epoch timestamp (seconds since Jan 1, 1970, midnight UTC).
/// </summary>
/// <param name="this">The <see cref="DateTime"/> on which this method is called.</param>
/// <returns>Seconds since Unix epoch.</returns>
public static Int64 ToUnixEpochDate(this DateTime @this) => new DateTimeOffset(@this).ToUniversalTime().ToUnixTimeSeconds();
/// <summary>
/// Compares a Date to another and returns a <c>DateTimeSpan</c>.
/// </summary>
/// <param name="dateStart">The date start.</param>
/// <param name="dateEnd">The date end.</param>
/// <returns>A DateTimeSpan with the Years, Months, Days, Hours, Minutes, Seconds and Milliseconds between the dates.</returns>
public static DateTimeSpan GetDateTimeSpan(this DateTime dateStart, DateTime dateEnd) => DateTimeSpan.CompareDates(dateStart, dateEnd);
/// <summary>
/// Compare the Date elements(Months, Days, Hours, Minutes).
/// </summary>
/// <param name="this">The <see cref="DateTime"/> on which this method is called.</param>
/// <param name="minute">The minute (0-59).</param>
/// <param name="hour">The hour. (0-23).</param>
/// <param name="dayOfMonth">The day of month. (1-31).</param>
/// <param name="month">The month. (1-12).</param>
/// <param name="dayOfWeek">The day of week. (0-6)(Sunday = 0).</param>
/// <returns>Returns <c>true</c> if Months, Days, Hours and Minutes match, otherwise <c>false</c>.</returns>
public static Boolean AsCronCanRun(this DateTime @this, Int32? minute = null, Int32? hour = null, Int32? dayOfMonth = null, Int32? month = null, Int32? dayOfWeek = null) {
List<Boolean?> results = new List<Boolean?> {
GetElementParts(minute, @this.Minute),
GetElementParts(hour, @this.Hour),
GetElementParts(dayOfMonth, @this.Day),
GetElementParts(month, @this.Month),
GetElementParts(dayOfWeek, (Int32) @this.DayOfWeek),
};
return results.Any(x => x != false);
}
/// <summary>
/// Compare the Date elements(Months, Days, Hours, Minutes).
/// </summary>
/// <param name="this">The <see cref="DateTime"/> on which this method is called.</param>
/// <param name="minute">The minute (0-59).</param>
/// <param name="hour">The hour. (0-23).</param>
/// <param name="dayOfMonth">The day of month. (1-31).</param>
/// <param name="month">The month. (1-12).</param>
/// <param name="dayOfWeek">The day of week. (0-6)(Sunday = 0).</param>
/// <returns>Returns <c>true</c> if Months, Days, Hours and Minutes match, otherwise <c>false</c>.</returns>
public static Boolean AsCronCanRun(this DateTime @this, String minute = "*", String hour = "*", String dayOfMonth = "*", String month = "*", String dayOfWeek = "*") {
List<Boolean?> results = new List<Boolean?> {
GetElementParts(minute, nameof(minute), @this.Minute),
GetElementParts(hour, nameof(hour), @this.Hour),
GetElementParts(dayOfMonth, nameof(dayOfMonth), @this.Day),
GetElementParts(month, nameof(month), @this.Month),
GetElementParts(dayOfWeek, nameof(dayOfWeek), (Int32) @this.DayOfWeek),
};
return results.Any(x => x != false);
}
/// <summary>
/// Converts a <see cref="DateTime"/> to the <see href="https://docs.microsoft.com/en-us/dotnet/standard/base-types/standard-date-and-time-format-strings#RFC1123">RFC1123 format</see>.
/// </summary>
/// <param name="this">The <see cref="DateTime"/> on which this method is called.</param>
/// <returns>The string representation of <paramref name="this"/> according to <see href="https://tools.ietf.org/html/rfc1123#page-54">RFC1123</see>.</returns>
/// <remarks>
/// <para>If <paramref name="this"/> is not a UTC date / time, its UTC equivalent is converted, leaving <paramref name="this"/> unchanged.</para>
/// </remarks>
public static String ToRfc1123String(this DateTime @this) => @this.ToUniversalTime().ToString("R", CultureInfo.InvariantCulture);
private static Boolean? GetElementParts(Int32? status, Int32 value) => status.HasValue ? status.Value == value : (Boolean?)null;
private static Boolean? GetElementParts(String parts, String type, Int32 value) {
if(String.IsNullOrWhiteSpace(parts) || parts == "*") {
return null;
}
if(parts.Contains(",")) {
return parts.Split(',').Select(Int32.Parse).Contains(value);
}
Int32 stop = DateRanges[type];
if(parts.Contains("/")) {
Int32 multiple = Int32.Parse(parts.Split('/').Last());
Int32 start = type == "dayOfMonth" || type == "month" ? 1 : 0;
for(Int32 i = start; i <= stop; i += multiple) {
if(i == value) {
return true;
}
}
return false;
}
if(parts.Contains("-")) {
String[] range = parts.Split('-');
Int32 start = Int32.Parse(range.First());
stop = Math.Max(stop, Int32.Parse(range.Last()));
if((type == "dayOfMonth" || type == "month") && start == 0) {
start = 1;
}
for(Int32 i = start; i <= stop; i++) {
if(i == value) {
return true;
}
}
return false;
}
return Int32.Parse(parts) == value;
}
}
}

View File

@ -1,86 +1,86 @@
using System;
using System.Collections.Generic;
namespace Swan
{
namespace Swan {
/// <summary>
/// Extension methods.
/// </summary>
public static partial class Extensions {
/// <summary>
/// Extension methods.
/// Gets the value if exists or default.
/// </summary>
public static partial class Extensions
{
/// <summary>
/// Gets the value if exists or default.
/// </summary>
/// <typeparam name="TKey">The type of the key.</typeparam>
/// <typeparam name="TValue">The type of the value.</typeparam>
/// <param name="dict">The dictionary.</param>
/// <param name="key">The key.</param>
/// <param name="defaultValue">The default value.</param>
/// <returns>
/// The value of the provided key or default.
/// </returns>
/// <exception cref="ArgumentNullException">dict.</exception>
public static TValue GetValueOrDefault<TKey, TValue>(this IDictionary<TKey, TValue> dict, TKey key, TValue defaultValue = default)
{
if (dict == null)
throw new ArgumentNullException(nameof(dict));
return dict.ContainsKey(key) ? dict[key] : defaultValue;
}
/// <summary>
/// Adds a key/value pair to the Dictionary if the key does not already exist.
/// If the value is null, the key will not be updated.
/// Based on <c>ConcurrentDictionary.GetOrAdd</c> method.
/// </summary>
/// <typeparam name="TKey">The type of the key.</typeparam>
/// <typeparam name="TValue">The type of the value.</typeparam>
/// <param name="dict">The dictionary.</param>
/// <param name="key">The key.</param>
/// <param name="valueFactory">The value factory.</param>
/// <returns>
/// The value for the key.
/// </returns>
/// <exception cref="ArgumentNullException">
/// dict
/// or
/// valueFactory.
/// </exception>
public static TValue GetOrAdd<TKey, TValue>(this IDictionary<TKey, TValue> dict, TKey key, Func<TKey, TValue> valueFactory)
{
if (dict == null)
throw new ArgumentNullException(nameof(dict));
if (valueFactory == null)
throw new ArgumentNullException(nameof(valueFactory));
if (!dict.ContainsKey(key))
{
var value = valueFactory(key);
if (Equals(value, default)) return default;
dict[key] = value;
}
return dict[key];
}
/// <summary>
/// Executes the item action for each element in the Dictionary.
/// </summary>
/// <typeparam name="TKey">The type of the key.</typeparam>
/// <typeparam name="TValue">The type of the value.</typeparam>
/// <param name="dict">The dictionary.</param>
/// <param name="itemAction">The item action.</param>
/// <exception cref="ArgumentNullException">dict.</exception>
public static void ForEach<TKey, TValue>(this IDictionary<TKey, TValue> dict, Action<TKey, TValue> itemAction)
{
if (dict == null)
throw new ArgumentNullException(nameof(dict));
foreach (var kvp in dict)
{
itemAction(kvp.Key, kvp.Value);
}
}
}
/// <typeparam name="TKey">The type of the key.</typeparam>
/// <typeparam name="TValue">The type of the value.</typeparam>
/// <param name="dict">The dictionary.</param>
/// <param name="key">The key.</param>
/// <param name="defaultValue">The default value.</param>
/// <returns>
/// The value of the provided key or default.
/// </returns>
/// <exception cref="ArgumentNullException">dict.</exception>
public static TValue GetValueOrDefault<TKey, TValue>(this IDictionary<TKey, TValue> dict, TKey key, TValue defaultValue = default) {
if(dict == null) {
throw new ArgumentNullException(nameof(dict));
}
return dict.ContainsKey(key) ? dict[key] : defaultValue;
}
/// <summary>
/// Adds a key/value pair to the Dictionary if the key does not already exist.
/// If the value is null, the key will not be updated.
/// Based on <c>ConcurrentDictionary.GetOrAdd</c> method.
/// </summary>
/// <typeparam name="TKey">The type of the key.</typeparam>
/// <typeparam name="TValue">The type of the value.</typeparam>
/// <param name="dict">The dictionary.</param>
/// <param name="key">The key.</param>
/// <param name="valueFactory">The value factory.</param>
/// <returns>
/// The value for the key.
/// </returns>
/// <exception cref="ArgumentNullException">
/// dict
/// or
/// valueFactory.
/// </exception>
public static TValue GetOrAdd<TKey, TValue>(this IDictionary<TKey, TValue> dict, TKey key, Func<TKey, TValue> valueFactory) {
if(dict == null) {
throw new ArgumentNullException(nameof(dict));
}
if(valueFactory == null) {
throw new ArgumentNullException(nameof(valueFactory));
}
if(!dict.ContainsKey(key)) {
TValue value = valueFactory(key);
if(Equals(value, default)) {
return default;
}
dict[key] = value;
}
return dict[key];
}
/// <summary>
/// Executes the item action for each element in the Dictionary.
/// </summary>
/// <typeparam name="TKey">The type of the key.</typeparam>
/// <typeparam name="TValue">The type of the value.</typeparam>
/// <param name="dict">The dictionary.</param>
/// <param name="itemAction">The item action.</param>
/// <exception cref="ArgumentNullException">dict.</exception>
public static void ForEach<TKey, TValue>(this IDictionary<TKey, TValue> dict, Action<TKey, TValue> itemAction) {
if(dict == null) {
throw new ArgumentNullException(nameof(dict));
}
foreach(KeyValuePair<TKey, TValue> kvp in dict) {
itemAction(kvp.Key, kvp.Value);
}
}
}
}

View File

@ -2,49 +2,31 @@
using System.Linq;
using System.Threading;
namespace Swan
{
namespace Swan {
/// <summary>
/// Provides extension methods for <see cref="Exception"/>.
/// </summary>
public static class ExceptionExtensions {
/// <summary>
/// Provides extension methods for <see cref="Exception"/>.
/// Returns a value that tells whether an <see cref="Exception"/> is of a type that
/// we better not catch and ignore.
/// </summary>
public static class ExceptionExtensions
{
/// <summary>
/// Returns a value that tells whether an <see cref="Exception"/> is of a type that
/// we better not catch and ignore.
/// </summary>
/// <param name="this">The exception being thrown.</param>
/// <returns><see langword="true"/> if <paramref name="this"/> is a critical exception;
/// otherwise, <see langword="false"/>.</returns>
public static bool IsCriticalException(this Exception @this)
=> @this.IsCriticalExceptionCore()
|| (@this.InnerException?.IsCriticalException() ?? false)
|| (@this is AggregateException aggregateException && aggregateException.InnerExceptions.Any(e => e.IsCriticalException()));
/// <summary>
/// Returns a value that tells whether an <see cref="Exception"/> is of a type that
/// will likely cause application failure.
/// </summary>
/// <param name="this">The exception being thrown.</param>
/// <returns><see langword="true"/> if <paramref name="this"/> is a fatal exception;
/// otherwise, <see langword="false"/>.</returns>
public static bool IsFatalException(this Exception @this)
=> @this.IsFatalExceptionCore()
|| (@this.InnerException?.IsFatalException() ?? false)
|| (@this is AggregateException aggregateException && aggregateException.InnerExceptions.Any(e => e.IsFatalException()));
private static bool IsCriticalExceptionCore(this Exception @this)
=> IsFatalExceptionCore(@this)
|| @this is AppDomainUnloadedException
|| @this is BadImageFormatException
|| @this is CannotUnloadAppDomainException
|| @this is InvalidProgramException
|| @this is NullReferenceException;
private static bool IsFatalExceptionCore(this Exception @this)
=> @this is StackOverflowException
|| @this is OutOfMemoryException
|| @this is ThreadAbortException
|| @this is AccessViolationException;
}
/// <param name="this">The exception being thrown.</param>
/// <returns><see langword="true"/> if <paramref name="this"/> is a critical exception;
/// otherwise, <see langword="false"/>.</returns>
public static Boolean IsCriticalException(this Exception @this) => @this.IsCriticalExceptionCore() || (@this.InnerException?.IsCriticalException() ?? false) || @this is AggregateException aggregateException && aggregateException.InnerExceptions.Any(e => e.IsCriticalException());
/// <summary>
/// Returns a value that tells whether an <see cref="Exception"/> is of a type that
/// will likely cause application failure.
/// </summary>
/// <param name="this">The exception being thrown.</param>
/// <returns><see langword="true"/> if <paramref name="this"/> is a fatal exception;
/// otherwise, <see langword="false"/>.</returns>
public static Boolean IsFatalException(this Exception @this) => @this.IsFatalExceptionCore() || (@this.InnerException?.IsFatalException() ?? false) || @this is AggregateException aggregateException && aggregateException.InnerExceptions.Any(e => e.IsFatalException());
private static Boolean IsCriticalExceptionCore(this Exception @this) => IsFatalExceptionCore(@this) || @this is AppDomainUnloadedException || @this is BadImageFormatException || @this is CannotUnloadAppDomainException || @this is InvalidProgramException || @this is NullReferenceException;
private static Boolean IsFatalExceptionCore(this Exception @this) => @this is StackOverflowException || @this is OutOfMemoryException || @this is ThreadAbortException || @this is AccessViolationException;
}
}

View File

@ -2,178 +2,172 @@
using System.Collections.Generic;
using System.Linq;
namespace Swan
{
namespace Swan {
/// <summary>
/// Functional programming extension methods.
/// </summary>
public static class FunctionalExtensions {
/// <summary>
/// Functional programming extension methods.
/// Whens the specified condition.
/// </summary>
public static class FunctionalExtensions
{
/// <summary>
/// Whens the specified condition.
/// </summary>
/// <typeparam name="T">The type of IQueryable.</typeparam>
/// <param name="list">The list.</param>
/// <param name="condition">The condition.</param>
/// <param name="fn">The function.</param>
/// <returns>
/// The IQueryable.
/// </returns>
/// <exception cref="ArgumentNullException">
/// this
/// or
/// condition
/// or
/// fn.
/// </exception>
public static IQueryable<T> When<T>(
this IQueryable<T> list,
Func<bool> condition,
Func<IQueryable<T>, IQueryable<T>> fn)
{
if (list == null)
throw new ArgumentNullException(nameof(list));
if (condition == null)
throw new ArgumentNullException(nameof(condition));
if (fn == null)
throw new ArgumentNullException(nameof(fn));
return condition() ? fn(list) : list;
}
/// <summary>
/// Whens the specified condition.
/// </summary>
/// <typeparam name="T">The type of IEnumerable.</typeparam>
/// <param name="list">The list.</param>
/// <param name="condition">The condition.</param>
/// <param name="fn">The function.</param>
/// <returns>
/// The IEnumerable.
/// </returns>
/// <exception cref="ArgumentNullException">
/// this
/// or
/// condition
/// or
/// fn.
/// </exception>
public static IEnumerable<T> When<T>(
this IEnumerable<T> list,
Func<bool> condition,
Func<IEnumerable<T>, IEnumerable<T>> fn)
{
if (list == null)
throw new ArgumentNullException(nameof(list));
if (condition == null)
throw new ArgumentNullException(nameof(condition));
if (fn == null)
throw new ArgumentNullException(nameof(fn));
return condition() ? fn(list) : list;
}
/// <summary>
/// Adds the value when the condition is true.
/// </summary>
/// <typeparam name="T">The type of IList element.</typeparam>
/// <param name="list">The list.</param>
/// <param name="condition">The condition.</param>
/// <param name="value">The value.</param>
/// <returns>
/// The IList.
/// </returns>
/// <exception cref="ArgumentNullException">
/// this
/// or
/// condition
/// or
/// value.
/// </exception>
public static IList<T> AddWhen<T>(
this IList<T> list,
Func<bool> condition,
Func<T> value)
{
if (list == null)
throw new ArgumentNullException(nameof(list));
if (condition == null)
throw new ArgumentNullException(nameof(condition));
if (value == null)
throw new ArgumentNullException(nameof(value));
if (condition())
list.Add(value());
return list;
}
/// <summary>
/// Adds the value when the condition is true.
/// </summary>
/// <typeparam name="T">The type of IList element.</typeparam>
/// <param name="list">The list.</param>
/// <param name="condition">if set to <c>true</c> [condition].</param>
/// <param name="value">The value.</param>
/// <returns>
/// The IList.
/// </returns>
/// <exception cref="ArgumentNullException">list.</exception>
public static IList<T> AddWhen<T>(
this IList<T> list,
bool condition,
T value)
{
if (list == null)
throw new ArgumentNullException(nameof(list));
if (condition)
list.Add(value);
return list;
}
/// <summary>
/// Adds the range when the condition is true.
/// </summary>
/// <typeparam name="T">The type of List element.</typeparam>
/// <param name="list">The list.</param>
/// <param name="condition">The condition.</param>
/// <param name="value">The value.</param>
/// <returns>
/// The List.
/// </returns>
/// <exception cref="ArgumentNullException">
/// this
/// or
/// condition
/// or
/// value.
/// </exception>
public static List<T> AddRangeWhen<T>(
this List<T> list,
Func<bool> condition,
Func<IEnumerable<T>> value)
{
if (list == null)
throw new ArgumentNullException(nameof(list));
if (condition == null)
throw new ArgumentNullException(nameof(condition));
if (value == null)
throw new ArgumentNullException(nameof(value));
if (condition())
list.AddRange(value());
return list;
}
}
/// <typeparam name="T">The type of IQueryable.</typeparam>
/// <param name="list">The list.</param>
/// <param name="condition">The condition.</param>
/// <param name="fn">The function.</param>
/// <returns>
/// The IQueryable.
/// </returns>
/// <exception cref="ArgumentNullException">
/// this
/// or
/// condition
/// or
/// fn.
/// </exception>
public static IQueryable<T> When<T>(this IQueryable<T> list, Func<Boolean> condition, Func<IQueryable<T>, IQueryable<T>> fn) {
if(list == null) {
throw new ArgumentNullException(nameof(list));
}
if(condition == null) {
throw new ArgumentNullException(nameof(condition));
}
if(fn == null) {
throw new ArgumentNullException(nameof(fn));
}
return condition() ? fn(list) : list;
}
/// <summary>
/// Whens the specified condition.
/// </summary>
/// <typeparam name="T">The type of IEnumerable.</typeparam>
/// <param name="list">The list.</param>
/// <param name="condition">The condition.</param>
/// <param name="fn">The function.</param>
/// <returns>
/// The IEnumerable.
/// </returns>
/// <exception cref="ArgumentNullException">
/// this
/// or
/// condition
/// or
/// fn.
/// </exception>
public static IEnumerable<T> When<T>(this IEnumerable<T> list, Func<Boolean> condition, Func<IEnumerable<T>, IEnumerable<T>> fn) {
if(list == null) {
throw new ArgumentNullException(nameof(list));
}
if(condition == null) {
throw new ArgumentNullException(nameof(condition));
}
if(fn == null) {
throw new ArgumentNullException(nameof(fn));
}
return condition() ? fn(list) : list;
}
/// <summary>
/// Adds the value when the condition is true.
/// </summary>
/// <typeparam name="T">The type of IList element.</typeparam>
/// <param name="list">The list.</param>
/// <param name="condition">The condition.</param>
/// <param name="value">The value.</param>
/// <returns>
/// The IList.
/// </returns>
/// <exception cref="ArgumentNullException">
/// this
/// or
/// condition
/// or
/// value.
/// </exception>
public static IList<T> AddWhen<T>(this IList<T> list, Func<Boolean> condition, Func<T> value) {
if(list == null) {
throw new ArgumentNullException(nameof(list));
}
if(condition == null) {
throw new ArgumentNullException(nameof(condition));
}
if(value == null) {
throw new ArgumentNullException(nameof(value));
}
if(condition()) {
list.Add(value());
}
return list;
}
/// <summary>
/// Adds the value when the condition is true.
/// </summary>
/// <typeparam name="T">The type of IList element.</typeparam>
/// <param name="list">The list.</param>
/// <param name="condition">if set to <c>true</c> [condition].</param>
/// <param name="value">The value.</param>
/// <returns>
/// The IList.
/// </returns>
/// <exception cref="ArgumentNullException">list.</exception>
public static IList<T> AddWhen<T>(this IList<T> list, Boolean condition, T value) {
if(list == null) {
throw new ArgumentNullException(nameof(list));
}
if(condition) {
list.Add(value);
}
return list;
}
/// <summary>
/// Adds the range when the condition is true.
/// </summary>
/// <typeparam name="T">The type of List element.</typeparam>
/// <param name="list">The list.</param>
/// <param name="condition">The condition.</param>
/// <param name="value">The value.</param>
/// <returns>
/// The List.
/// </returns>
/// <exception cref="ArgumentNullException">
/// this
/// or
/// condition
/// or
/// value.
/// </exception>
public static List<T> AddRangeWhen<T>(this List<T> list, Func<Boolean> condition, Func<IEnumerable<T>> value) {
if(list == null) {
throw new ArgumentNullException(nameof(list));
}
if(condition == null) {
throw new ArgumentNullException(nameof(condition));
}
if(value == null) {
throw new ArgumentNullException(nameof(value));
}
if(condition()) {
list.AddRange(value());
}
return list;
}
}
}

View File

@ -1,455 +1,411 @@
using System;
#nullable enable
using System;
using System.Collections;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Reflection;
using Swan.Configuration;
using Swan.Reflection;
namespace Swan
{
namespace Swan {
/// <summary>
/// Provides various extension methods for Reflection and Types.
/// </summary>
public static class ReflectionExtensions {
private static readonly Lazy<ConcurrentDictionary<Tuple<Boolean, PropertyInfo>, Func<Object, Object>>> CacheGetMethods = new Lazy<ConcurrentDictionary<Tuple<Boolean, PropertyInfo>, Func<Object, Object>>>(() => new ConcurrentDictionary<Tuple<Boolean, PropertyInfo>, Func<Object, Object>>(), true);
private static readonly Lazy<ConcurrentDictionary<Tuple<Boolean, PropertyInfo>, Action<Object, Object[]>>> CacheSetMethods = new Lazy<ConcurrentDictionary<Tuple<Boolean, PropertyInfo>, Action<Object, Object[]>>>(() => new ConcurrentDictionary<Tuple<Boolean, PropertyInfo>, Action<Object, Object[]>>(), true);
#region Assembly Extensions
/// <summary>
/// Provides various extension methods for Reflection and Types.
/// Gets all types within an assembly in a safe manner.
/// </summary>
public static class ReflectionExtensions
{
private static readonly Lazy<ConcurrentDictionary<Tuple<bool, PropertyInfo>, Func<object, object>>> CacheGetMethods =
new Lazy<ConcurrentDictionary<Tuple<bool, PropertyInfo>, Func<object, object>>>(() => new ConcurrentDictionary<Tuple<bool, PropertyInfo>, Func<object, object>>(), true);
private static readonly Lazy<ConcurrentDictionary<Tuple<bool, PropertyInfo>, Action<object, object[]>>> CacheSetMethods =
new Lazy<ConcurrentDictionary<Tuple<bool, PropertyInfo>, Action<object, object[]>>>(() => new ConcurrentDictionary<Tuple<bool, PropertyInfo>, Action<object, object[]>>(), true);
#region Assembly Extensions
/// <summary>
/// Gets all types within an assembly in a safe manner.
/// </summary>
/// <param name="assembly">The assembly.</param>
/// <returns>
/// Array of Type objects representing the types specified by an assembly.
/// </returns>
/// <exception cref="ArgumentNullException">assembly.</exception>
public static IEnumerable<Type> GetAllTypes(this Assembly assembly)
{
if (assembly == null)
throw new ArgumentNullException(nameof(assembly));
try
{
return assembly.GetTypes();
}
catch (ReflectionTypeLoadException e)
{
return e.Types.Where(t => t != null);
}
}
#endregion
#region Type Extensions
/// <summary>
/// The closest programmatic equivalent of default(T).
/// </summary>
/// <param name="type">The type.</param>
/// <returns>
/// Default value of this type.
/// </returns>
/// <exception cref="ArgumentNullException">type.</exception>
public static object? GetDefault(this Type type)
{
if (type == null)
throw new ArgumentNullException(nameof(type));
return type.IsValueType ? Activator.CreateInstance(type) : default;
}
/// <summary>
/// Determines whether this type is compatible with ICollection.
/// </summary>
/// <param name="sourceType">The type.</param>
/// <returns>
/// <c>true</c> if the specified source type is collection; otherwise, <c>false</c>.
/// </returns>
/// <exception cref="ArgumentNullException">sourceType.</exception>
public static bool IsCollection(this Type sourceType)
{
if (sourceType == null)
throw new ArgumentNullException(nameof(sourceType));
return sourceType != typeof(string) &&
typeof(IEnumerable).IsAssignableFrom(sourceType);
}
/// <summary>
/// Gets a method from a type given the method name, binding flags, generic types and parameter types.
/// </summary>
/// <param name="type">Type of the source.</param>
/// <param name="bindingFlags">The binding flags.</param>
/// <param name="methodName">Name of the method.</param>
/// <param name="genericTypes">The generic types.</param>
/// <param name="parameterTypes">The parameter types.</param>
/// <returns>
/// An object that represents the method with the specified name.
/// </returns>
/// <exception cref="System.Reflection.AmbiguousMatchException">
/// The exception that is thrown when binding to a member results in more than one member matching the
/// binding criteria. This class cannot be inherited.
/// </exception>
public static MethodInfo GetMethod(
this Type type,
BindingFlags bindingFlags,
string methodName,
Type[] genericTypes,
Type[] parameterTypes)
{
if (type == null)
throw new ArgumentNullException(nameof(type));
if (methodName == null)
throw new ArgumentNullException(nameof(methodName));
if (genericTypes == null)
throw new ArgumentNullException(nameof(genericTypes));
if (parameterTypes == null)
throw new ArgumentNullException(nameof(parameterTypes));
var methods = type
.GetMethods(bindingFlags)
.Where(mi => string.Equals(methodName, mi.Name, StringComparison.Ordinal))
.Where(mi => mi.ContainsGenericParameters)
.Where(mi => mi.GetGenericArguments().Length == genericTypes.Length)
.Where(mi => mi.GetParameters().Length == parameterTypes.Length)
.Select(mi => mi.MakeGenericMethod(genericTypes))
.Where(mi => mi.GetParameters().Select(pi => pi.ParameterType).SequenceEqual(parameterTypes))
.ToList();
return methods.Count > 1 ? throw new AmbiguousMatchException() : methods.FirstOrDefault();
}
/// <summary>
/// Determines whether [is i enumerable request].
/// </summary>
/// <param name="type">The type.</param>
/// <returns>
/// <c>true</c> if [is i enumerable request] [the specified type]; otherwise, <c>false</c>.
/// </returns>
/// <exception cref="ArgumentNullException">type.</exception>
public static bool IsIEnumerable(this Type type)
=> type == null
? throw new ArgumentNullException(nameof(type))
: type.IsGenericType && type.GetGenericTypeDefinition() == typeof(IEnumerable<>);
#endregion
/// <summary>
/// Tries to parse using the basic types.
/// </summary>
/// <param name="type">The type.</param>
/// <param name="value">The value.</param>
/// <param name="result">The result.</param>
/// <returns>
/// <c>true</c> if parsing was successful; otherwise, <c>false</c>.
/// </returns>
/// <exception cref="ArgumentNullException">type</exception>
public static bool TryParseBasicType(this Type type, object value, out object? result)
{
if (type == null)
throw new ArgumentNullException(nameof(type));
if (type != typeof(bool))
return TryParseBasicType(type, value.ToStringInvariant(), out result);
result = value.ToBoolean();
return true;
}
/// <summary>
/// Tries to parse using the basic types.
/// </summary>
/// <param name="type">The type.</param>
/// <param name="value">The value.</param>
/// <param name="result">The result.</param>
/// <returns>
/// <c>true</c> if parsing was successful; otherwise, <c>false</c>.
/// </returns>
/// <exception cref="ArgumentNullException">type</exception>
public static bool TryParseBasicType(this Type type, string value, out object? result)
{
if (type == null)
throw new ArgumentNullException(nameof(type));
result = null;
return Definitions.BasicTypesInfo.Value.ContainsKey(type) && Definitions.BasicTypesInfo.Value[type].TryParse(value, out result);
}
/// <summary>
/// Tries the type of the set basic value to a property.
/// </summary>
/// <param name="propertyInfo">The property information.</param>
/// <param name="value">The value.</param>
/// <param name="target">The object.</param>
/// <returns>
/// <c>true</c> if parsing was successful; otherwise, <c>false</c>.
/// </returns>
/// <exception cref="ArgumentNullException">propertyInfo.</exception>
public static bool TrySetBasicType(this PropertyInfo propertyInfo, object value, object target)
{
if (propertyInfo == null)
throw new ArgumentNullException(nameof(propertyInfo));
try
{
if (propertyInfo.PropertyType.TryParseBasicType(value, out var propertyValue))
{
propertyInfo.SetValue(target, propertyValue);
return true;
}
}
catch
{
// swallow
}
return false;
}
/// <summary>
/// Tries the type of the set to an array a basic type.
/// </summary>
/// <param name="type">The type.</param>
/// <param name="value">The value.</param>
/// <param name="target">The array.</param>
/// <param name="index">The index.</param>
/// <returns>
/// <c>true</c> if parsing was successful; otherwise, <c>false</c>.
/// </returns>
/// <exception cref="ArgumentNullException">type</exception>
public static bool TrySetArrayBasicType(this Type type, object value, Array target, int index)
{
if (type == null)
throw new ArgumentNullException(nameof(type));
if (target == null)
return false;
try
{
if (value == null)
{
target.SetValue(null, index);
return true;
}
if (type.TryParseBasicType(value, out var propertyValue))
{
target.SetValue(propertyValue, index);
return true;
}
if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Nullable<>))
{
target.SetValue(null, index);
return true;
}
}
catch
{
// swallow
}
return false;
}
/// <summary>
/// Tries to set a property array with another array.
/// </summary>
/// <param name="propertyInfo">The property.</param>
/// <param name="value">The value.</param>
/// <param name="obj">The object.</param>
/// <returns>
/// <c>true</c> if parsing was successful; otherwise, <c>false</c>.
/// </returns>
/// <exception cref="ArgumentNullException">propertyInfo.</exception>
public static bool TrySetArray(this PropertyInfo propertyInfo, IEnumerable<object>? value, object obj)
{
if (propertyInfo == null)
throw new ArgumentNullException(nameof(propertyInfo));
var elementType = propertyInfo.PropertyType.GetElementType();
if (elementType == null || value == null)
return false;
var targetArray = Array.CreateInstance(elementType, value.Count());
var i = 0;
foreach (var sourceElement in value)
{
var result = elementType.TrySetArrayBasicType(sourceElement, targetArray, i++);
if (!result) return false;
}
propertyInfo.SetValue(obj, targetArray);
return true;
}
/// <summary>
/// Gets property actual value or <c>PropertyDisplayAttribute.DefaultValue</c> if presented.
///
/// If the <c>PropertyDisplayAttribute.Format</c> value is presented, the property value
/// will be formatted accordingly.
///
/// If the object contains a null value, a empty string will be returned.
/// </summary>
/// <param name="propertyInfo">The property information.</param>
/// <param name="target">The object.</param>
/// <returns>The property value or null.</returns>
/// <exception cref="ArgumentNullException">propertyInfo.</exception>
public static string? ToFormattedString(this PropertyInfo propertyInfo, object target)
{
if (propertyInfo == null)
throw new ArgumentNullException(nameof(propertyInfo));
try
{
var value = propertyInfo.GetValue(target);
var attr = AttributeCache.DefaultCache.Value.RetrieveOne<PropertyDisplayAttribute>(propertyInfo);
if (attr == null) return value?.ToString() ?? string.Empty;
var valueToFormat = value ?? attr.DefaultValue;
return string.IsNullOrEmpty(attr.Format)
? (valueToFormat?.ToString() ?? string.Empty)
: ConvertObjectAndFormat(propertyInfo.PropertyType, valueToFormat, attr.Format);
}
catch
{
return null;
}
}
/// <summary>
/// Gets a MethodInfo from a Property Get method.
/// </summary>
/// <param name="propertyInfo">The property information.</param>
/// <param name="nonPublic">if set to <c>true</c> [non public].</param>
/// <returns>
/// The cached MethodInfo.
/// </returns>
public static Func<object, object>? GetCacheGetMethod(this PropertyInfo propertyInfo, bool nonPublic = false)
{
var key = Tuple.Create(!nonPublic, propertyInfo);
// TODO: Fix public logic
return !nonPublic && !CacheGetMethods.Value.ContainsKey(key) && !propertyInfo.GetGetMethod(true).IsPublic
? null
: CacheGetMethods.Value
.GetOrAdd(key,
x => y => x.Item2.GetGetMethod(nonPublic).Invoke(y, null));
//y => x => y.Item2.CreatePropertyProxy().GetValue(x));
}
/// <summary>
/// Gets a MethodInfo from a Property Set method.
/// </summary>
/// <param name="propertyInfo">The property information.</param>
/// <param name="nonPublic">if set to <c>true</c> [non public].</param>
/// <returns>
/// The cached MethodInfo.
/// </returns>
public static Action<object, object[]>? GetCacheSetMethod(this PropertyInfo propertyInfo, bool nonPublic = false)
{
var key = Tuple.Create(!nonPublic, propertyInfo);
return !nonPublic && !CacheSetMethods.Value.ContainsKey(key) && !propertyInfo.GetSetMethod(true).IsPublic
? null
: CacheSetMethods.Value
.GetOrAdd(key,
x => (obj, args) => x.Item2.GetSetMethod(nonPublic).Invoke(obj, args));
//y => (obj, args) => y.Item2.CreatePropertyProxy().SetValue(obj, args));
}
/// <summary>
/// Convert a string to a boolean.
/// </summary>
/// <param name="str">The string.</param>
/// <returns>
/// <c>true</c> if the string represents a valid truly value, otherwise <c>false</c>.
/// </returns>
public static bool ToBoolean(this string str)
{
try
{
return Convert.ToBoolean(str);
}
catch (FormatException)
{
// ignored
}
try
{
return Convert.ToBoolean(Convert.ToInt32(str));
}
catch
{
// ignored
}
return false;
}
/// <summary>
/// Creates a property proxy that stores getter and setter delegates.
/// </summary>
/// <param name="this">The property information.</param>
/// <returns>
/// The property proxy.
/// </returns>
/// <exception cref="ArgumentNullException">this.</exception>
public static IPropertyProxy? CreatePropertyProxy(this PropertyInfo @this)
{
if (@this == null)
throw new ArgumentNullException(nameof(@this));
var genericType = typeof(PropertyProxy<,>)
.MakeGenericType(@this.DeclaringType, @this.PropertyType);
return Activator.CreateInstance(genericType, @this) as IPropertyProxy;
}
/// <summary>
/// Convert a object to a boolean.
/// </summary>
/// <param name="value">The value.</param>
/// <returns>
/// <c>true</c> if the string represents a valid truly value, otherwise <c>false</c>.
/// </returns>
public static bool ToBoolean(this object value) => value.ToStringInvariant().ToBoolean();
private static string ConvertObjectAndFormat(Type propertyType, object value, string format)
{
if (propertyType == typeof(DateTime) || propertyType == typeof(DateTime?))
return Convert.ToDateTime(value, CultureInfo.InvariantCulture).ToString(format);
if (propertyType == typeof(int) || propertyType == typeof(int?))
return Convert.ToInt32(value, CultureInfo.InvariantCulture).ToString(format);
if (propertyType == typeof(decimal) || propertyType == typeof(decimal?))
return Convert.ToDecimal(value, CultureInfo.InvariantCulture).ToString(format);
if (propertyType == typeof(double) || propertyType == typeof(double?))
return Convert.ToDouble(value, CultureInfo.InvariantCulture).ToString(format);
if (propertyType == typeof(byte) || propertyType == typeof(byte?))
return Convert.ToByte(value, CultureInfo.InvariantCulture).ToString(format);
return value?.ToString() ?? string.Empty;
}
}
/// <param name="assembly">The assembly.</param>
/// <returns>
/// Array of Type objects representing the types specified by an assembly.
/// </returns>
/// <exception cref="ArgumentNullException">assembly.</exception>
public static IEnumerable<Type> GetAllTypes(this Assembly assembly) {
if(assembly == null) {
throw new ArgumentNullException(nameof(assembly));
}
try {
return assembly.GetTypes();
} catch(ReflectionTypeLoadException e) {
return e.Types.Where(t => t != null);
}
}
#endregion
#region Type Extensions
/// <summary>
/// The closest programmatic equivalent of default(T).
/// </summary>
/// <param name="type">The type.</param>
/// <returns>
/// Default value of this type.
/// </returns>
/// <exception cref="ArgumentNullException">type.</exception>
public static Object? GetDefault(this Type type) {
if(type == null) {
throw new ArgumentNullException(nameof(type));
}
return type.IsValueType ? Activator.CreateInstance(type) : default;
}
/// <summary>
/// Determines whether this type is compatible with ICollection.
/// </summary>
/// <param name="sourceType">The type.</param>
/// <returns>
/// <c>true</c> if the specified source type is collection; otherwise, <c>false</c>.
/// </returns>
/// <exception cref="ArgumentNullException">sourceType.</exception>
public static Boolean IsCollection(this Type sourceType) {
if(sourceType == null) {
throw new ArgumentNullException(nameof(sourceType));
}
return sourceType != typeof(String) && typeof(IEnumerable).IsAssignableFrom(sourceType);
}
/// <summary>
/// Gets a method from a type given the method name, binding flags, generic types and parameter types.
/// </summary>
/// <param name="type">Type of the source.</param>
/// <param name="bindingFlags">The binding flags.</param>
/// <param name="methodName">Name of the method.</param>
/// <param name="genericTypes">The generic types.</param>
/// <param name="parameterTypes">The parameter types.</param>
/// <returns>
/// An object that represents the method with the specified name.
/// </returns>
/// <exception cref="System.Reflection.AmbiguousMatchException">
/// The exception that is thrown when binding to a member results in more than one member matching the
/// binding criteria. This class cannot be inherited.
/// </exception>
public static MethodInfo GetMethod(this Type type, BindingFlags bindingFlags, String methodName, Type[] genericTypes, Type[] parameterTypes) {
if(type == null) {
throw new ArgumentNullException(nameof(type));
}
if(methodName == null) {
throw new ArgumentNullException(nameof(methodName));
}
if(genericTypes == null) {
throw new ArgumentNullException(nameof(genericTypes));
}
if(parameterTypes == null) {
throw new ArgumentNullException(nameof(parameterTypes));
}
List<MethodInfo> methods = type.GetMethods(bindingFlags)
.Where(mi => String.Equals(methodName, mi.Name, StringComparison.Ordinal))
.Where(mi => mi.ContainsGenericParameters)
.Where(mi => mi.GetGenericArguments().Length == genericTypes.Length)
.Where(mi => mi.GetParameters().Length == parameterTypes.Length).Select(mi => mi.MakeGenericMethod(genericTypes))
.Where(mi => mi.GetParameters().Select(pi => pi.ParameterType).SequenceEqual(parameterTypes)).ToList();
return methods.Count > 1 ? throw new AmbiguousMatchException() : methods.FirstOrDefault();
}
/// <summary>
/// Determines whether [is i enumerable request].
/// </summary>
/// <param name="type">The type.</param>
/// <returns>
/// <c>true</c> if [is i enumerable request] [the specified type]; otherwise, <c>false</c>.
/// </returns>
/// <exception cref="ArgumentNullException">type.</exception>
public static Boolean IsIEnumerable(this Type type) => type == null ? throw new ArgumentNullException(nameof(type)) : type.IsGenericType && type.GetGenericTypeDefinition() == typeof(IEnumerable<>);
#endregion
/// <summary>
/// Tries to parse using the basic types.
/// </summary>
/// <param name="type">The type.</param>
/// <param name="value">The value.</param>
/// <param name="result">The result.</param>
/// <returns>
/// <c>true</c> if parsing was successful; otherwise, <c>false</c>.
/// </returns>
/// <exception cref="ArgumentNullException">type</exception>
public static Boolean TryParseBasicType(this Type type, Object value, out Object? result) {
if(type == null) {
throw new ArgumentNullException(nameof(type));
}
if(type != typeof(Boolean)) {
return TryParseBasicType(type, value.ToStringInvariant(), out result);
}
result = value.ToBoolean();
return true;
}
/// <summary>
/// Tries to parse using the basic types.
/// </summary>
/// <param name="type">The type.</param>
/// <param name="value">The value.</param>
/// <param name="result">The result.</param>
/// <returns>
/// <c>true</c> if parsing was successful; otherwise, <c>false</c>.
/// </returns>
/// <exception cref="ArgumentNullException">type</exception>
public static Boolean TryParseBasicType(this Type type, String value, out Object? result) {
if(type == null) {
throw new ArgumentNullException(nameof(type));
}
result = null;
return Definitions.BasicTypesInfo.Value.ContainsKey(type) && Definitions.BasicTypesInfo.Value[type].TryParse(value, out result);
}
/// <summary>
/// Tries the type of the set basic value to a property.
/// </summary>
/// <param name="propertyInfo">The property information.</param>
/// <param name="value">The value.</param>
/// <param name="target">The object.</param>
/// <returns>
/// <c>true</c> if parsing was successful; otherwise, <c>false</c>.
/// </returns>
/// <exception cref="ArgumentNullException">propertyInfo.</exception>
public static Boolean TrySetBasicType(this PropertyInfo propertyInfo, Object value, Object target) {
if(propertyInfo == null) {
throw new ArgumentNullException(nameof(propertyInfo));
}
try {
if(propertyInfo.PropertyType.TryParseBasicType(value, out Object? propertyValue)) {
propertyInfo.SetValue(target, propertyValue);
return true;
}
} catch {
// swallow
}
return false;
}
/// <summary>
/// Tries the type of the set to an array a basic type.
/// </summary>
/// <param name="type">The type.</param>
/// <param name="value">The value.</param>
/// <param name="target">The array.</param>
/// <param name="index">The index.</param>
/// <returns>
/// <c>true</c> if parsing was successful; otherwise, <c>false</c>.
/// </returns>
/// <exception cref="ArgumentNullException">type</exception>
public static Boolean TrySetArrayBasicType(this Type type, Object value, Array target, Int32 index) {
if(type == null) {
throw new ArgumentNullException(nameof(type));
}
if(target == null) {
return false;
}
try {
if(value == null) {
target.SetValue(null, index);
return true;
}
if(type.TryParseBasicType(value, out Object? propertyValue)) {
target.SetValue(propertyValue, index);
return true;
}
if(type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Nullable<>)) {
target.SetValue(null, index);
return true;
}
} catch {
// swallow
}
return false;
}
/// <summary>
/// Tries to set a property array with another array.
/// </summary>
/// <param name="propertyInfo">The property.</param>
/// <param name="value">The value.</param>
/// <param name="obj">The object.</param>
/// <returns>
/// <c>true</c> if parsing was successful; otherwise, <c>false</c>.
/// </returns>
/// <exception cref="ArgumentNullException">propertyInfo.</exception>
public static Boolean TrySetArray(this PropertyInfo propertyInfo, IEnumerable<Object>? value, Object obj) {
if(propertyInfo == null) {
throw new ArgumentNullException(nameof(propertyInfo));
}
Type? elementType = propertyInfo.PropertyType.GetElementType();
if(elementType == null || value == null) {
return false;
}
Array targetArray = Array.CreateInstance(elementType, value.Count());
Int32 i = 0;
foreach(Object sourceElement in value) {
Boolean result = elementType.TrySetArrayBasicType(sourceElement, targetArray, i++);
if(!result) {
return false;
}
}
propertyInfo.SetValue(obj, targetArray);
return true;
}
/// <summary>
/// Gets property actual value or <c>PropertyDisplayAttribute.DefaultValue</c> if presented.
///
/// If the <c>PropertyDisplayAttribute.Format</c> value is presented, the property value
/// will be formatted accordingly.
///
/// If the object contains a null value, a empty string will be returned.
/// </summary>
/// <param name="propertyInfo">The property information.</param>
/// <param name="target">The object.</param>
/// <returns>The property value or null.</returns>
/// <exception cref="ArgumentNullException">propertyInfo.</exception>
public static String? ToFormattedString(this PropertyInfo propertyInfo, Object target) {
if(propertyInfo == null) {
throw new ArgumentNullException(nameof(propertyInfo));
}
try {
Object? value = propertyInfo.GetValue(target);
PropertyDisplayAttribute attr = AttributeCache.DefaultCache.Value.RetrieveOne<PropertyDisplayAttribute>(propertyInfo);
if(attr == null) {
return value?.ToString() ?? String.Empty;
}
Object valueToFormat = value ?? attr.DefaultValue;
return String.IsNullOrEmpty(attr.Format) ? (valueToFormat?.ToString() ?? String.Empty) : ConvertObjectAndFormat(propertyInfo.PropertyType, valueToFormat, attr.Format);
} catch {
return null;
}
}
/// <summary>
/// Gets a MethodInfo from a Property Get method.
/// </summary>
/// <param name="propertyInfo">The property information.</param>
/// <param name="nonPublic">if set to <c>true</c> [non public].</param>
/// <returns>
/// The cached MethodInfo.
/// </returns>
public static Func<Object, Object>? GetCacheGetMethod(this PropertyInfo propertyInfo, Boolean nonPublic = false) {
Tuple<Boolean, PropertyInfo> key = Tuple.Create(!nonPublic, propertyInfo);
// TODO: Fix public logic
return !nonPublic && !CacheGetMethods.Value.ContainsKey(key) && !propertyInfo.GetGetMethod(true)!.IsPublic ? null : CacheGetMethods.Value.GetOrAdd(key, x => y => x.Item2.GetGetMethod(nonPublic)?.Invoke(y, null)!);
//y => x => y.Item2.CreatePropertyProxy().GetValue(x));
}
/// <summary>
/// Gets a MethodInfo from a Property Set method.
/// </summary>
/// <param name="propertyInfo">The property information.</param>
/// <param name="nonPublic">if set to <c>true</c> [non public].</param>
/// <returns>
/// The cached MethodInfo.
/// </returns>
public static Action<Object, Object[]>? GetCacheSetMethod(this PropertyInfo propertyInfo, Boolean nonPublic = false) {
Tuple<Boolean, PropertyInfo> key = Tuple.Create(!nonPublic, propertyInfo);
return !nonPublic && !CacheSetMethods.Value.ContainsKey(key) && !propertyInfo.GetSetMethod(true)!.IsPublic ? null : CacheSetMethods.Value.GetOrAdd(key, x => (obj, args) => x.Item2.GetSetMethod(nonPublic)!.Invoke(obj, args));
//y => (obj, args) => y.Item2.CreatePropertyProxy().SetValue(obj, args));
}
/// <summary>
/// Convert a string to a boolean.
/// </summary>
/// <param name="str">The string.</param>
/// <returns>
/// <c>true</c> if the string represents a valid truly value, otherwise <c>false</c>.
/// </returns>
public static Boolean ToBoolean(this String str) {
try {
return Convert.ToBoolean(str);
} catch(FormatException) {
// ignored
}
try {
return Convert.ToBoolean(Convert.ToInt32(str));
} catch {
// ignored
}
return false;
}
/// <summary>
/// Creates a property proxy that stores getter and setter delegates.
/// </summary>
/// <param name="this">The property information.</param>
/// <returns>
/// The property proxy.
/// </returns>
/// <exception cref="ArgumentNullException">this.</exception>
public static IPropertyProxy? CreatePropertyProxy(this PropertyInfo @this) {
if(@this == null) {
throw new ArgumentNullException(nameof(@this));
}
Type genericType = typeof(PropertyProxy<,>).MakeGenericType(@this.DeclaringType!, @this.PropertyType);
return Activator.CreateInstance(genericType, @this) as IPropertyProxy;
}
/// <summary>
/// Convert a object to a boolean.
/// </summary>
/// <param name="value">The value.</param>
/// <returns>
/// <c>true</c> if the string represents a valid truly value, otherwise <c>false</c>.
/// </returns>
public static Boolean ToBoolean(this Object value) => value.ToStringInvariant().ToBoolean();
private static String ConvertObjectAndFormat(Type propertyType, Object value, String format) =>
propertyType == typeof(DateTime) || propertyType == typeof(DateTime?)
? Convert.ToDateTime(value, CultureInfo.InvariantCulture).ToString(format)
: propertyType == typeof(Int32) || propertyType == typeof(Int32?)
? Convert.ToInt32(value, CultureInfo.InvariantCulture).ToString(format)
: propertyType == typeof(Decimal) || propertyType == typeof(Decimal?)
? Convert.ToDecimal(value, CultureInfo.InvariantCulture).ToString(format)
: propertyType == typeof(Double) || propertyType == typeof(Double?)
? Convert.ToDouble(value, CultureInfo.InvariantCulture).ToString(format)
: propertyType == typeof(Byte) || propertyType == typeof(Byte?)
? Convert.ToByte(value, CultureInfo.InvariantCulture).ToString(format)
: value?.ToString() ?? String.Empty;
}
}

View File

@ -1,405 +1,364 @@
using System;
#nullable enable
using System;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using Swan.Formatters;
namespace Swan
{
namespace Swan {
/// <summary>
/// String related extension methods.
/// </summary>
public static class StringExtensions {
#region Private Declarations
private const RegexOptions StandardRegexOptions = RegexOptions.Multiline | RegexOptions.Compiled | RegexOptions.CultureInvariant;
private static readonly String[] ByteSuffixes = { "B", "KB", "MB", "GB", "TB" };
private static readonly Lazy<Regex> SplitLinesRegex = new Lazy<Regex>(() => new Regex("\r\n|\r|\n", StandardRegexOptions));
private static readonly Lazy<Regex> UnderscoreRegex = new Lazy<Regex>(() => new Regex(@"_", StandardRegexOptions));
private static readonly Lazy<Regex> CamelCaseRegEx = new Lazy<Regex>(() => new Regex(@"[a-z][A-Z]", StandardRegexOptions));
private static readonly Lazy<MatchEvaluator> SplitCamelCaseString = new Lazy<MatchEvaluator>(() => m => {
String x = m.ToString();
return x[0] + " " + x[1..];
});
private static readonly Lazy<String[]> InvalidFilenameChars = new Lazy<String[]>(() => Path.GetInvalidFileNameChars().Select(c => c.ToString()).ToArray());
#endregion
/// <summary>
/// String related extension methods.
/// Returns a string that represents the given item
/// It tries to use InvariantCulture if the ToString(IFormatProvider)
/// overload exists.
/// </summary>
public static class StringExtensions
{
#region Private Declarations
private const RegexOptions StandardRegexOptions =
RegexOptions.Multiline | RegexOptions.Compiled | RegexOptions.CultureInvariant;
private static readonly string[] ByteSuffixes = { "B", "KB", "MB", "GB", "TB" };
private static readonly Lazy<Regex> SplitLinesRegex =
new Lazy<Regex>(() => new Regex("\r\n|\r|\n", StandardRegexOptions));
private static readonly Lazy<Regex> UnderscoreRegex =
new Lazy<Regex>(() => new Regex(@"_", StandardRegexOptions));
private static readonly Lazy<Regex> CamelCaseRegEx =
new Lazy<Regex>(() => new Regex(@"[a-z][A-Z]", StandardRegexOptions));
private static readonly Lazy<MatchEvaluator> SplitCamelCaseString = new Lazy<MatchEvaluator>(() => m =>
{
var x = m.ToString();
return x[0] + " " + x.Substring(1, x.Length - 1);
});
private static readonly Lazy<string[]> InvalidFilenameChars =
new Lazy<string[]>(() => Path.GetInvalidFileNameChars().Select(c => c.ToString()).ToArray());
#endregion
/// <summary>
/// Returns a string that represents the given item
/// It tries to use InvariantCulture if the ToString(IFormatProvider)
/// overload exists.
/// </summary>
/// <param name="this">The item.</param>
/// <returns>A <see cref="string" /> that represents the current object.</returns>
public static string ToStringInvariant(this object? @this)
{
if (@this == null)
return string.Empty;
var itemType = @this.GetType();
if (itemType == typeof(string))
return @this as string ?? string.Empty;
return Definitions.BasicTypesInfo.Value.ContainsKey(itemType)
? Definitions.BasicTypesInfo.Value[itemType].ToStringInvariant(@this)
: @this.ToString();
}
/// <summary>
/// Returns a string that represents the given item
/// It tries to use InvariantCulture if the ToString(IFormatProvider)
/// overload exists.
/// </summary>
/// <typeparam name="T">The type to get the string.</typeparam>
/// <param name="item">The item.</param>
/// <returns>A <see cref="string" /> that represents the current object.</returns>
public static string ToStringInvariant<T>(this T item)
=> typeof(string) == typeof(T) ? item as string ?? string.Empty : ToStringInvariant(item as object);
/// <summary>
/// Removes the control characters from a string except for those specified.
/// </summary>
/// <param name="value">The input.</param>
/// <param name="excludeChars">When specified, these characters will not be removed.</param>
/// <returns>
/// A string that represents the current object.
/// </returns>
/// <exception cref="ArgumentNullException">input.</exception>
public static string RemoveControlCharsExcept(this string value, params char[] excludeChars)
{
if (value == null)
throw new ArgumentNullException(nameof(value));
if (excludeChars == null)
excludeChars = Array.Empty<char>();
return new string(value
.Where(c => char.IsControl(c) == false || excludeChars.Contains(c))
.ToArray());
}
/// <summary>
/// Removes all control characters from a string, including new line sequences.
/// </summary>
/// <param name="value">The input.</param>
/// <returns>A <see cref="string" /> that represents the current object.</returns>
/// <exception cref="ArgumentNullException">input.</exception>
public static string RemoveControlChars(this string value) => value.RemoveControlCharsExcept(null);
/// <summary>
/// Outputs JSON string representing this object.
/// </summary>
/// <param name="this">The object.</param>
/// <param name="format">if set to <c>true</c> format the output.</param>
/// <returns>A <see cref="string" /> that represents the current object.</returns>
public static string ToJson(this object @this, bool format = true) =>
@this == null ? string.Empty : Json.Serialize(@this, format);
/// <summary>
/// Returns text representing the properties of the specified object in a human-readable format.
/// While this method is fairly expensive computationally speaking, it provides an easy way to
/// examine objects.
/// </summary>
/// <param name="this">The object.</param>
/// <returns>A <see cref="string" /> that represents the current object.</returns>
public static string Stringify(this object @this)
{
if (@this == null)
return "(null)";
try
{
var jsonText = Json.Serialize(@this, false, "$type");
var jsonData = Json.Deserialize(jsonText);
return new HumanizeJson(jsonData, 0).GetResult();
}
catch
{
return @this.ToStringInvariant();
}
}
/// <summary>
/// Retrieves a section of the string, inclusive of both, the start and end indexes.
/// This behavior is unlike JavaScript's Slice behavior where the end index is non-inclusive
/// If the string is null it returns an empty string.
/// </summary>
/// <param name="this">The string.</param>
/// <param name="startIndex">The start index.</param>
/// <param name="endIndex">The end index.</param>
/// <returns>Retrieves a substring from this instance.</returns>
public static string Slice(this string @this, int startIndex, int endIndex)
{
if (@this == null)
return string.Empty;
var end = endIndex.Clamp(startIndex, @this.Length - 1);
return startIndex >= end ? string.Empty : @this.Substring(startIndex, (end - startIndex) + 1);
}
/// <summary>
/// Gets a part of the string clamping the length and startIndex parameters to safe values.
/// If the string is null it returns an empty string. This is basically just a safe version
/// of string.Substring.
/// </summary>
/// <param name="this">The string.</param>
/// <param name="startIndex">The start index.</param>
/// <param name="length">The length.</param>
/// <returns>Retrieves a substring from this instance.</returns>
public static string SliceLength(this string @this, int startIndex, int length)
{
if (@this == null)
return string.Empty;
var start = startIndex.Clamp(0, @this.Length - 1);
var len = length.Clamp(0, @this.Length - start);
return len == 0 ? string.Empty : @this.Substring(start, len);
}
/// <summary>
/// Splits the specified text into r, n or rn separated lines.
/// </summary>
/// <param name="this">The text.</param>
/// <returns>
/// An array whose elements contain the substrings from this instance
/// that are delimited by one or more characters in separator.
/// </returns>
public static string[] ToLines(this string @this) =>
@this == null ? Array.Empty<string>() : SplitLinesRegex.Value.Split(@this);
/// <summary>
/// Humanizes (make more human-readable) an identifier-style string
/// in either camel case or snake case. For example, CamelCase will be converted to
/// Camel Case and Snake_Case will be converted to Snake Case.
/// </summary>
/// <param name="value">The identifier-style string.</param>
/// <returns>A <see cref="string" /> humanized.</returns>
public static string Humanize(this string value)
{
if (value == null)
return string.Empty;
var returnValue = UnderscoreRegex.Value.Replace(value, " ");
returnValue = CamelCaseRegEx.Value.Replace(returnValue, SplitCamelCaseString.Value);
return returnValue;
}
/// <summary>
/// Humanizes (make more human-readable) an boolean.
/// </summary>
/// <param name="value">if set to <c>true</c> [value].</param>
/// <returns>A <see cref="string" /> that represents the current boolean.</returns>
public static string Humanize(this bool value) => value ? "Yes" : "No";
/// <summary>
/// Humanizes (make more human-readable) the specified value.
/// </summary>
/// <param name="value">The value.</param>
/// <returns>A <see cref="string" /> that represents the current object.</returns>
public static string Humanize(this object value) =>
/// <param name="this">The item.</param>
/// <returns>A <see cref="String" /> that represents the current object.</returns>
public static String ToStringInvariant(this Object? @this) {
if(@this == null) {
return String.Empty;
}
Type itemType = @this.GetType();
return itemType == typeof(String) ? @this as String ?? String.Empty : Definitions.BasicTypesInfo.Value.ContainsKey(itemType) ? Definitions.BasicTypesInfo.Value[itemType].ToStringInvariant(@this) : @this.ToString()!;
}
/// <summary>
/// Returns a string that represents the given item
/// It tries to use InvariantCulture if the ToString(IFormatProvider)
/// overload exists.
/// </summary>
/// <typeparam name="T">The type to get the string.</typeparam>
/// <param name="item">The item.</param>
/// <returns>A <see cref="String" /> that represents the current object.</returns>
public static String ToStringInvariant<T>(this T item) => typeof(String) == typeof(T) ? item as String ?? String.Empty : ToStringInvariant(item as Object);
/// <summary>
/// Removes the control characters from a string except for those specified.
/// </summary>
/// <param name="value">The input.</param>
/// <param name="excludeChars">When specified, these characters will not be removed.</param>
/// <returns>
/// A string that represents the current object.
/// </returns>
/// <exception cref="ArgumentNullException">input.</exception>
public static String RemoveControlCharsExcept(this String value, params Char[]? excludeChars) {
if(value == null) {
throw new ArgumentNullException(nameof(value));
}
if(excludeChars == null) {
excludeChars = Array.Empty<Char>();
}
return new String(value.Where(c => Char.IsControl(c) == false || excludeChars.Contains(c)).ToArray());
}
/// <summary>
/// Removes all control characters from a string, including new line sequences.
/// </summary>
/// <param name="value">The input.</param>
/// <returns>A <see cref="String" /> that represents the current object.</returns>
/// <exception cref="ArgumentNullException">input.</exception>
public static String RemoveControlChars(this String value) => value.RemoveControlCharsExcept(null);
/// <summary>
/// Outputs JSON string representing this object.
/// </summary>
/// <param name="this">The object.</param>
/// <param name="format">if set to <c>true</c> format the output.</param>
/// <returns>A <see cref="String" /> that represents the current object.</returns>
public static String ToJson(this Object @this, Boolean format = true) => @this == null ? String.Empty : Json.Serialize(@this, format);
/// <summary>
/// Returns text representing the properties of the specified object in a human-readable format.
/// While this method is fairly expensive computationally speaking, it provides an easy way to
/// examine objects.
/// </summary>
/// <param name="this">The object.</param>
/// <returns>A <see cref="String" /> that represents the current object.</returns>
public static String Stringify(this Object @this) {
if(@this == null) {
return "(null)";
}
try {
String jsonText = Json.Serialize(@this, false, "$type");
Object? jsonData = Json.Deserialize(jsonText);
return new HumanizeJson(jsonData, 0).GetResult();
} catch {
return @this.ToStringInvariant();
}
}
/// <summary>
/// Retrieves a section of the string, inclusive of both, the start and end indexes.
/// This behavior is unlike JavaScript's Slice behavior where the end index is non-inclusive
/// If the string is null it returns an empty string.
/// </summary>
/// <param name="this">The string.</param>
/// <param name="startIndex">The start index.</param>
/// <param name="endIndex">The end index.</param>
/// <returns>Retrieves a substring from this instance.</returns>
public static String Slice(this String @this, Int32 startIndex, Int32 endIndex) {
if(@this == null) {
return String.Empty;
}
Int32 end = endIndex.Clamp(startIndex, @this.Length - 1);
return startIndex >= end ? String.Empty : @this.Substring(startIndex, end - startIndex + 1);
}
/// <summary>
/// Gets a part of the string clamping the length and startIndex parameters to safe values.
/// If the string is null it returns an empty string. This is basically just a safe version
/// of string.Substring.
/// </summary>
/// <param name="this">The string.</param>
/// <param name="startIndex">The start index.</param>
/// <param name="length">The length.</param>
/// <returns>Retrieves a substring from this instance.</returns>
public static String SliceLength(this String @this, Int32 startIndex, Int32 length) {
if(@this == null) {
return String.Empty;
}
Int32 start = startIndex.Clamp(0, @this.Length - 1);
Int32 len = length.Clamp(0, @this.Length - start);
return len == 0 ? String.Empty : @this.Substring(start, len);
}
/// <summary>
/// Splits the specified text into r, n or rn separated lines.
/// </summary>
/// <param name="this">The text.</param>
/// <returns>
/// An array whose elements contain the substrings from this instance
/// that are delimited by one or more characters in separator.
/// </returns>
public static String[] ToLines(this String @this) => @this == null ? Array.Empty<String>() : SplitLinesRegex.Value.Split(@this);
/// <summary>
/// Humanizes (make more human-readable) an identifier-style string
/// in either camel case or snake case. For example, CamelCase will be converted to
/// Camel Case and Snake_Case will be converted to Snake Case.
/// </summary>
/// <param name="value">The identifier-style string.</param>
/// <returns>A <see cref="String" /> humanized.</returns>
public static String Humanize(this String value) {
if(value == null) {
return String.Empty;
}
String returnValue = UnderscoreRegex.Value.Replace(value, " ");
returnValue = CamelCaseRegEx.Value.Replace(returnValue, SplitCamelCaseString.Value);
return returnValue;
}
/// <summary>
/// Humanizes (make more human-readable) an boolean.
/// </summary>
/// <param name="value">if set to <c>true</c> [value].</param>
/// <returns>A <see cref="String" /> that represents the current boolean.</returns>
public static String Humanize(this Boolean value) => value ? "Yes" : "No";
/// <summary>
/// Humanizes (make more human-readable) the specified value.
/// </summary>
/// <param name="value">The value.</param>
/// <returns>A <see cref="String" /> that represents the current object.</returns>
public static String Humanize(this Object value) =>
value switch
{
string stringValue => stringValue.Humanize(),
bool boolValue => boolValue.Humanize(),
_ => value.Stringify()
};
/// <summary>
/// Indents the specified multi-line text with the given amount of leading spaces
/// per line.
/// </summary>
/// <param name="value">The text.</param>
/// <param name="spaces">The spaces.</param>
/// <returns>A <see cref="string" /> that represents the current object.</returns>
public static string Indent(this string value, int spaces = 4)
{
if (value == null) value = string.Empty;
if (spaces <= 0) return value;
var lines = value.ToLines();
var builder = new StringBuilder();
var indentStr = new string(' ', spaces);
foreach (var line in lines)
{
builder.AppendLine($"{indentStr}{line}");
}
return builder.ToString().TrimEnd();
}
/// <summary>
/// Gets the line and column number (i.e. not index) of the
/// specified character index. Useful to locate text in a multi-line
/// string the same way a text editor does.
/// Please not that the tuple contains first the line number and then the
/// column number.
/// </summary>
/// <param name="value">The string.</param>
/// <param name="charIndex">Index of the character.</param>
/// <returns>A 2-tuple whose value is (item1, item2).</returns>
public static Tuple<int, int> TextPositionAt(this string value, int charIndex)
{
if (value == null)
return Tuple.Create(0, 0);
var index = charIndex.Clamp(0, value.Length - 1);
var lineIndex = 0;
var colNumber = 0;
for (var i = 0; i <= index; i++)
{
if (value[i] == '\n')
{
lineIndex++;
colNumber = 0;
continue;
}
if (value[i] != '\r')
colNumber++;
}
return Tuple.Create(lineIndex + 1, colNumber);
}
/// <summary>
/// Makes the file name system safe.
/// </summary>
/// <param name="value">The s.</param>
/// <returns>
/// A string with a safe file name.
/// </returns>
/// <exception cref="ArgumentNullException">s.</exception>
public static string ToSafeFilename(this string value) =>
value == null
? throw new ArgumentNullException(nameof(value))
: InvalidFilenameChars.Value
.Aggregate(value, (current, c) => current.Replace(c, string.Empty))
.Slice(0, 220);
/// <summary>
/// Formats a long into the closest bytes string.
/// </summary>
/// <param name="bytes">The bytes length.</param>
/// <returns>
/// The string representation of the current Byte object, formatted as specified by the format parameter.
/// </returns>
public static string FormatBytes(this long bytes) => ((ulong)bytes).FormatBytes();
/// <summary>
/// Formats a long into the closest bytes string.
/// </summary>
/// <param name="bytes">The bytes length.</param>
/// <returns>
/// A copy of format in which the format items have been replaced by the string
/// representations of the corresponding arguments.
/// </returns>
public static string FormatBytes(this ulong bytes)
{
int i;
double dblSByte = bytes;
for (i = 0; i < ByteSuffixes.Length && bytes >= 1024; i++, bytes /= 1024)
{
dblSByte = bytes / 1024.0;
}
return $"{dblSByte:0.##} {ByteSuffixes[i]}";
}
/// <summary>
/// Truncates the specified value.
/// </summary>
/// <param name="value">The value.</param>
/// <param name="maximumLength">The maximum length.</param>
/// <returns>
/// Retrieves a substring from this instance.
/// The substring starts at a specified character position and has a specified length.
/// </returns>
public static string? Truncate(this string value, int maximumLength) =>
Truncate(value, maximumLength, string.Empty);
/// <summary>
/// Truncates the specified value and append the omission last.
/// </summary>
/// <param name="value">The value.</param>
/// <param name="maximumLength">The maximum length.</param>
/// <param name="omission">The omission.</param>
/// <returns>
/// Retrieves a substring from this instance.
/// The substring starts at a specified character position and has a specified length.
/// </returns>
public static string? Truncate(this string value, int maximumLength, string omission)
{
if (value == null)
return null;
return value.Length > maximumLength
? value.Substring(0, maximumLength) + (omission ?? string.Empty)
: value;
}
/// <summary>
/// Determines whether the specified <see cref="string"/> contains any of characters in
/// the specified array of <see cref="char"/>.
/// </summary>
/// <returns>
/// <c>true</c> if <paramref name="value"/> contains any of <paramref name="chars"/>;
/// otherwise, <c>false</c>.
/// </returns>
/// <param name="value">
/// A <see cref="string"/> to test.
/// </param>
/// <param name="chars">
/// An array of <see cref="char"/> that contains characters to find.
/// </param>
public static bool Contains(this string value, params char[] chars) =>
chars?.Length == 0 || (!string.IsNullOrEmpty(value) && value.IndexOfAny(chars) > -1);
/// <summary>
/// Replaces all chars in a string.
/// </summary>
/// <param name="value">The value.</param>
/// <param name="replaceValue">The replace value.</param>
/// <param name="chars">The chars.</param>
/// <returns>The string with the characters replaced.</returns>
public static string ReplaceAll(this string value, string replaceValue, params char[] chars) =>
chars.Aggregate(value, (current, c) => current.Replace(new string(new[] { c }), replaceValue));
/// <summary>
/// Convert hex character to an integer. Return -1 if char is something
/// other than a hex char.
/// </summary>
/// <param name="value">The c.</param>
/// <returns>Converted integer.</returns>
public static int Hex2Int(this char value) =>
value >= '0' && value <= '9'
? value - '0'
: value >= 'A' && value <= 'F'
? value - 'A' + 10
: value >= 'a' && value <= 'f'
? value - 'a' + 10
: -1;
}
{
String stringValue => stringValue.Humanize(),
Boolean boolValue => boolValue.Humanize(),
_ => value.Stringify()
};
/// <summary>
/// Indents the specified multi-line text with the given amount of leading spaces
/// per line.
/// </summary>
/// <param name="value">The text.</param>
/// <param name="spaces">The spaces.</param>
/// <returns>A <see cref="String" /> that represents the current object.</returns>
public static String Indent(this String value, Int32 spaces = 4) {
if(value == null) {
value = String.Empty;
}
if(spaces <= 0) {
return value;
}
String[] lines = value.ToLines();
StringBuilder builder = new StringBuilder();
String indentStr = new String(' ', spaces);
foreach(String line in lines) {
_ = builder.AppendLine($"{indentStr}{line}");
}
return builder.ToString().TrimEnd();
}
/// <summary>
/// Gets the line and column number (i.e. not index) of the
/// specified character index. Useful to locate text in a multi-line
/// string the same way a text editor does.
/// Please not that the tuple contains first the line number and then the
/// column number.
/// </summary>
/// <param name="value">The string.</param>
/// <param name="charIndex">Index of the character.</param>
/// <returns>A 2-tuple whose value is (item1, item2).</returns>
public static Tuple<Int32, Int32> TextPositionAt(this String value, Int32 charIndex) {
if(value == null) {
return Tuple.Create(0, 0);
}
Int32 index = charIndex.Clamp(0, value.Length - 1);
Int32 lineIndex = 0;
Int32 colNumber = 0;
for(Int32 i = 0; i <= index; i++) {
if(value[i] == '\n') {
lineIndex++;
colNumber = 0;
continue;
}
if(value[i] != '\r') {
colNumber++;
}
}
return Tuple.Create(lineIndex + 1, colNumber);
}
/// <summary>
/// Makes the file name system safe.
/// </summary>
/// <param name="value">The s.</param>
/// <returns>
/// A string with a safe file name.
/// </returns>
/// <exception cref="ArgumentNullException">s.</exception>
public static String ToSafeFilename(this String value) => value == null ? throw new ArgumentNullException(nameof(value)) : InvalidFilenameChars.Value.Aggregate(value, (current, c) => current.Replace(c, String.Empty)).Slice(0, 220);
/// <summary>
/// Formats a long into the closest bytes string.
/// </summary>
/// <param name="bytes">The bytes length.</param>
/// <returns>
/// The string representation of the current Byte object, formatted as specified by the format parameter.
/// </returns>
public static String FormatBytes(this Int64 bytes) => ((UInt64)bytes).FormatBytes();
/// <summary>
/// Formats a long into the closest bytes string.
/// </summary>
/// <param name="bytes">The bytes length.</param>
/// <returns>
/// A copy of format in which the format items have been replaced by the string
/// representations of the corresponding arguments.
/// </returns>
public static String FormatBytes(this UInt64 bytes) {
Int32 i;
Double dblSByte = bytes;
for(i = 0; i < ByteSuffixes.Length && bytes >= 1024; i++, bytes /= 1024) {
dblSByte = bytes / 1024.0;
}
return $"{dblSByte:0.##} {ByteSuffixes[i]}";
}
/// <summary>
/// Truncates the specified value.
/// </summary>
/// <param name="value">The value.</param>
/// <param name="maximumLength">The maximum length.</param>
/// <returns>
/// Retrieves a substring from this instance.
/// The substring starts at a specified character position and has a specified length.
/// </returns>
public static String? Truncate(this String value, Int32 maximumLength) => Truncate(value, maximumLength, String.Empty);
/// <summary>
/// Truncates the specified value and append the omission last.
/// </summary>
/// <param name="value">The value.</param>
/// <param name="maximumLength">The maximum length.</param>
/// <param name="omission">The omission.</param>
/// <returns>
/// Retrieves a substring from this instance.
/// The substring starts at a specified character position and has a specified length.
/// </returns>
public static String? Truncate(this String value, Int32 maximumLength, String omission) => value == null ? null : value.Length > maximumLength ? value.Substring(0, maximumLength) + (omission ?? String.Empty) : value;
/// <summary>
/// Determines whether the specified <see cref="String"/> contains any of characters in
/// the specified array of <see cref="Char"/>.
/// </summary>
/// <returns>
/// <c>true</c> if <paramref name="value"/> contains any of <paramref name="chars"/>;
/// otherwise, <c>false</c>.
/// </returns>
/// <param name="value">
/// A <see cref="String"/> to test.
/// </param>
/// <param name="chars">
/// An array of <see cref="Char"/> that contains characters to find.
/// </param>
public static Boolean Contains(this String value, params Char[] chars) => chars?.Length == 0 || !String.IsNullOrEmpty(value) && chars != null && value.IndexOfAny(chars) > -1;
/// <summary>
/// Replaces all chars in a string.
/// </summary>
/// <param name="value">The value.</param>
/// <param name="replaceValue">The replace value.</param>
/// <param name="chars">The chars.</param>
/// <returns>The string with the characters replaced.</returns>
public static String ReplaceAll(this String value, String replaceValue, params Char[] chars) => chars.Aggregate(value, (current, c) => current.Replace(new String(new[] { c }), replaceValue));
/// <summary>
/// Convert hex character to an integer. Return -1 if char is something
/// other than a hex char.
/// </summary>
/// <param name="value">The c.</param>
/// <returns>Converted integer.</returns>
public static Int32 Hex2Int(this Char value) => value >= '0' && value <= '9' ? value - '0' : value >= 'A' && value <= 'F' ? value - 'A' + 10 : value >= 'a' && value <= 'f' ? value - 'a' + 10 : -1;
}
}

View File

@ -1,63 +1,59 @@
using System;
using System.Threading.Tasks;
namespace Swan
{
namespace Swan {
/// <summary>
/// Provides extension methods for <see cref="Task"/> and <see cref="Task{TResult}"/>.
/// </summary>
public static class TaskExtensions {
/// <summary>
/// Provides extension methods for <see cref="Task"/> and <see cref="Task{TResult}"/>.
/// <para>Suspends execution until the specified <see cref="Task"/> is completed.</para>
/// <para>This method operates similarly to the <see langword="await"/> C# operator,
/// but is meant to be called from a non-<see langword="async"/> method.</para>
/// </summary>
public static class TaskExtensions
{
/// <summary>
/// <para>Suspends execution until the specified <see cref="Task"/> is completed.</para>
/// <para>This method operates similarly to the <see langword="await"/> C# operator,
/// but is meant to be called from a non-<see langword="async"/> method.</para>
/// </summary>
/// <param name="this">The <see cref="Task"/> on which this method is called.</param>
/// <exception cref="NullReferenceException"><paramref name="this"/> is <see langword="null"/>.</exception>
public static void Await(this Task @this) => @this.GetAwaiter().GetResult();
/// <summary>
/// <para>Suspends execution until the specified <see cref="Task"/> is completed
/// and returns its result.</para>
/// <para>This method operates similarly to the <see langword="await"/> C# operator,
/// but is meant to be called from a non-<see langword="async"/> method.</para>
/// </summary>
/// <typeparam name="TResult">The type of the task's result.</typeparam>
/// <param name="this">The <see cref="Task{TResult}"/> on which this method is called.</param>
/// <returns>The result of <paramref name="this"/>.</returns>
/// <exception cref="NullReferenceException"><paramref name="this"/> is <see langword="null"/>.</exception>
public static TResult Await<TResult>(this Task<TResult> @this) => @this.GetAwaiter().GetResult();
/// <summary>
/// <para>Suspends execution until the specified <see cref="Task"/> is completed.</para>
/// <para>This method operates similarly to the <see langword="await" /> C# operator,
/// but is meant to be called from a non-<see langword="async" /> method.</para>
/// </summary>
/// <param name="this">The <see cref="Task" /> on which this method is called.</param>
/// <param name="continueOnCapturedContext">If set to <see langword="true"/>,
/// attempts to marshal the continuation back to the original context captured.
/// This parameter has the same effect as calling the <see cref="Task.ConfigureAwait"/>
/// method.</param>
/// <exception cref="NullReferenceException"><paramref name="this"/> is <see langword="null"/>.</exception>
public static void Await(this Task @this, bool continueOnCapturedContext)
=> @this.ConfigureAwait(continueOnCapturedContext).GetAwaiter().GetResult();
/// <summary>
/// <para>Suspends execution until the specified <see cref="Task"/> is completed
/// and returns its result.</para>
/// <para>This method operates similarly to the <see langword="await"/> C# operator,
/// but is meant to be called from a non-<see langword="async"/> method.</para>
/// </summary>
/// <typeparam name="TResult">The type of the task's result.</typeparam>
/// <param name="this">The <see cref="Task{TResult}"/> on which this method is called.</param>
/// <param name="continueOnCapturedContext">If set to <see langword="true"/>,
/// attempts to marshal the continuation back to the original context captured.
/// This parameter has the same effect as calling the <see cref="Task.ConfigureAwait"/>
/// method.</param>
/// <returns>The result of <paramref name="this"/>.</returns>
/// <exception cref="NullReferenceException"><paramref name="this"/> is <see langword="null"/>.</exception>
public static TResult Await<TResult>(this Task<TResult> @this, bool continueOnCapturedContext)
=> @this.ConfigureAwait(continueOnCapturedContext).GetAwaiter().GetResult();
}
/// <param name="this">The <see cref="Task"/> on which this method is called.</param>
/// <exception cref="NullReferenceException"><paramref name="this"/> is <see langword="null"/>.</exception>
public static void Await(this Task @this) => @this.GetAwaiter().GetResult();
/// <summary>
/// <para>Suspends execution until the specified <see cref="Task"/> is completed
/// and returns its result.</para>
/// <para>This method operates similarly to the <see langword="await"/> C# operator,
/// but is meant to be called from a non-<see langword="async"/> method.</para>
/// </summary>
/// <typeparam name="TResult">The type of the task's result.</typeparam>
/// <param name="this">The <see cref="Task{TResult}"/> on which this method is called.</param>
/// <returns>The result of <paramref name="this"/>.</returns>
/// <exception cref="NullReferenceException"><paramref name="this"/> is <see langword="null"/>.</exception>
public static TResult Await<TResult>(this Task<TResult> @this) => @this.GetAwaiter().GetResult();
/// <summary>
/// <para>Suspends execution until the specified <see cref="Task"/> is completed.</para>
/// <para>This method operates similarly to the <see langword="await" /> C# operator,
/// but is meant to be called from a non-<see langword="async" /> method.</para>
/// </summary>
/// <param name="this">The <see cref="Task" /> on which this method is called.</param>
/// <param name="continueOnCapturedContext">If set to <see langword="true"/>,
/// attempts to marshal the continuation back to the original context captured.
/// This parameter has the same effect as calling the <see cref="Task.ConfigureAwait"/>
/// method.</param>
/// <exception cref="NullReferenceException"><paramref name="this"/> is <see langword="null"/>.</exception>
public static void Await(this Task @this, Boolean continueOnCapturedContext) => @this.ConfigureAwait(continueOnCapturedContext).GetAwaiter().GetResult();
/// <summary>
/// <para>Suspends execution until the specified <see cref="Task"/> is completed
/// and returns its result.</para>
/// <para>This method operates similarly to the <see langword="await"/> C# operator,
/// but is meant to be called from a non-<see langword="async"/> method.</para>
/// </summary>
/// <typeparam name="TResult">The type of the task's result.</typeparam>
/// <param name="this">The <see cref="Task{TResult}"/> on which this method is called.</param>
/// <param name="continueOnCapturedContext">If set to <see langword="true"/>,
/// attempts to marshal the continuation back to the original context captured.
/// This parameter has the same effect as calling the <see cref="Task.ConfigureAwait"/>
/// method.</param>
/// <returns>The result of <paramref name="this"/>.</returns>
/// <exception cref="NullReferenceException"><paramref name="this"/> is <see langword="null"/>.</exception>
public static TResult Await<TResult>(this Task<TResult> @this, Boolean continueOnCapturedContext) => @this.ConfigureAwait(continueOnCapturedContext).GetAwaiter().GetResult();
}
}

View File

@ -1,165 +1,135 @@
using System;
using System.Reflection;
using System.Runtime.InteropServices;
using Swan.Reflection;
namespace Swan
{
namespace Swan {
/// <summary>
/// Provides various extension methods for value types and structs.
/// </summary>
public static class ValueTypeExtensions {
/// <summary>
/// Provides various extension methods for value types and structs.
/// Clamps the specified value between the minimum and the maximum.
/// </summary>
public static class ValueTypeExtensions
{
/// <summary>
/// Clamps the specified value between the minimum and the maximum.
/// </summary>
/// <typeparam name="T">The type of value to clamp.</typeparam>
/// <param name="this">The value.</param>
/// <param name="min">The minimum.</param>
/// <param name="max">The maximum.</param>
/// <returns>A value that indicates the relative order of the objects being compared.</returns>
public static T Clamp<T>(this T @this, T min, T max)
where T : struct, IComparable
{
if (@this.CompareTo(min) < 0) return min;
return @this.CompareTo(max) > 0 ? max : @this;
}
/// <summary>
/// Clamps the specified value between the minimum and the maximum.
/// </summary>
/// <param name="this">The value.</param>
/// <param name="min">The minimum.</param>
/// <param name="max">The maximum.</param>
/// <returns>A value that indicates the relative order of the objects being compared.</returns>
public static int Clamp(this int @this, int min, int max)
=> @this < min ? min : (@this > max ? max : @this);
/// <summary>
/// Determines whether the specified value is between a minimum and a maximum value.
/// </summary>
/// <typeparam name="T">The type of value to check.</typeparam>
/// <param name="this">The value.</param>
/// <param name="min">The minimum.</param>
/// <param name="max">The maximum.</param>
/// <returns>
/// <c>true</c> if the specified minimum is between; otherwise, <c>false</c>.
/// </returns>
public static bool IsBetween<T>(this T @this, T min, T max)
where T : struct, IComparable
{
return @this.CompareTo(min) >= 0 && @this.CompareTo(max) <= 0;
}
/// <summary>
/// Converts an array of bytes into the given struct type.
/// </summary>
/// <typeparam name="T">The type of structure to convert.</typeparam>
/// <param name="this">The data.</param>
/// <returns>a struct type derived from convert an array of bytes ref=ToStruct".</returns>
public static T ToStruct<T>(this byte[] @this)
where T : struct
{
return @this == null ? throw new ArgumentNullException(nameof(@this)) : ToStruct<T>(@this, 0, @this.Length);
}
/// <summary>
/// Converts an array of bytes into the given struct type.
/// </summary>
/// <typeparam name="T">The type of structure to convert.</typeparam>
/// <param name="this">The data.</param>
/// <param name="offset">The offset.</param>
/// <param name="length">The length.</param>
/// <returns>
/// A managed object containing the data pointed to by the ptr parameter.
/// </returns>
/// <exception cref="ArgumentNullException">data.</exception>
public static T ToStruct<T>(this byte[] @this, int offset, int length)
where T : struct
{
if (@this == null)
throw new ArgumentNullException(nameof(@this));
var buffer = new byte[length];
Array.Copy(@this, offset, buffer, 0, buffer.Length);
var handle = GCHandle.Alloc(GetStructBytes<T>(buffer), GCHandleType.Pinned);
try
{
return Marshal.PtrToStructure<T>(handle.AddrOfPinnedObject());
}
finally
{
handle.Free();
}
}
/// <summary>
/// Converts a struct to an array of bytes.
/// </summary>
/// <typeparam name="T">The type of structure to convert.</typeparam>
/// <param name="this">The object.</param>
/// <returns>A byte array containing the results of encoding the specified set of characters.</returns>
public static byte[] ToBytes<T>(this T @this)
where T : struct
{
var data = new byte[Marshal.SizeOf(@this)];
var handle = GCHandle.Alloc(data, GCHandleType.Pinned);
try
{
Marshal.StructureToPtr(@this, handle.AddrOfPinnedObject(), false);
return GetStructBytes<T>(data);
}
finally
{
handle.Free();
}
}
/// <summary>
/// Swaps the endianness of an unsigned long to an unsigned integer.
/// </summary>
/// <param name="this">The bytes contained in a long.</param>
/// <returns>
/// A 32-bit unsigned integer equivalent to the ulong
/// contained in longBytes.
/// </returns>
public static uint SwapEndianness(this ulong @this)
=> (uint)(((@this & 0x000000ff) << 24) +
((@this & 0x0000ff00) << 8) +
((@this & 0x00ff0000) >> 8) +
((@this & 0xff000000) >> 24));
private static byte[] GetStructBytes<T>(byte[] data)
{
if (data == null)
throw new ArgumentNullException(nameof(data));
var fields = typeof(T).GetTypeInfo()
.GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
var endian = AttributeCache.DefaultCache.Value.RetrieveOne<StructEndiannessAttribute, T>();
foreach (var field in fields)
{
if (endian == null && !field.IsDefined(typeof(StructEndiannessAttribute), false))
continue;
var offset = Marshal.OffsetOf<T>(field.Name).ToInt32();
var length = Marshal.SizeOf(field.FieldType);
endian = endian ?? AttributeCache.DefaultCache.Value.RetrieveOne<StructEndiannessAttribute>(field);
if (endian != null && (endian.Endianness == Endianness.Big && BitConverter.IsLittleEndian ||
endian.Endianness == Endianness.Little && !BitConverter.IsLittleEndian))
{
Array.Reverse(data, offset, length);
}
}
return data;
}
}
/// <typeparam name="T">The type of value to clamp.</typeparam>
/// <param name="this">The value.</param>
/// <param name="min">The minimum.</param>
/// <param name="max">The maximum.</param>
/// <returns>A value that indicates the relative order of the objects being compared.</returns>
public static T Clamp<T>(this T @this, T min, T max) where T : struct, IComparable => @this.CompareTo(min) < 0 ? min : @this.CompareTo(max) > 0 ? max : @this;
/// <summary>
/// Clamps the specified value between the minimum and the maximum.
/// </summary>
/// <param name="this">The value.</param>
/// <param name="min">The minimum.</param>
/// <param name="max">The maximum.</param>
/// <returns>A value that indicates the relative order of the objects being compared.</returns>
public static Int32 Clamp(this Int32 @this, Int32 min, Int32 max) => @this < min ? min : (@this > max ? max : @this);
/// <summary>
/// Determines whether the specified value is between a minimum and a maximum value.
/// </summary>
/// <typeparam name="T">The type of value to check.</typeparam>
/// <param name="this">The value.</param>
/// <param name="min">The minimum.</param>
/// <param name="max">The maximum.</param>
/// <returns>
/// <c>true</c> if the specified minimum is between; otherwise, <c>false</c>.
/// </returns>
public static Boolean IsBetween<T>(this T @this, T min, T max) where T : struct, IComparable => @this.CompareTo(min) >= 0 && @this.CompareTo(max) <= 0;
/// <summary>
/// Converts an array of bytes into the given struct type.
/// </summary>
/// <typeparam name="T">The type of structure to convert.</typeparam>
/// <param name="this">The data.</param>
/// <returns>a struct type derived from convert an array of bytes ref=ToStruct".</returns>
public static T ToStruct<T>(this Byte[] @this) where T : struct => @this == null ? throw new ArgumentNullException(nameof(@this)) : ToStruct<T>(@this, 0, @this.Length);
/// <summary>
/// Converts an array of bytes into the given struct type.
/// </summary>
/// <typeparam name="T">The type of structure to convert.</typeparam>
/// <param name="this">The data.</param>
/// <param name="offset">The offset.</param>
/// <param name="length">The length.</param>
/// <returns>
/// A managed object containing the data pointed to by the ptr parameter.
/// </returns>
/// <exception cref="ArgumentNullException">data.</exception>
public static T ToStruct<T>(this Byte[] @this, Int32 offset, Int32 length) where T : struct {
if(@this == null) {
throw new ArgumentNullException(nameof(@this));
}
Byte[] buffer = new Byte[length];
Array.Copy(@this, offset, buffer, 0, buffer.Length);
GCHandle handle = GCHandle.Alloc(GetStructBytes<T>(buffer), GCHandleType.Pinned);
try {
return Marshal.PtrToStructure<T>(handle.AddrOfPinnedObject());
} finally {
handle.Free();
}
}
/// <summary>
/// Converts a struct to an array of bytes.
/// </summary>
/// <typeparam name="T">The type of structure to convert.</typeparam>
/// <param name="this">The object.</param>
/// <returns>A byte array containing the results of encoding the specified set of characters.</returns>
public static Byte[] ToBytes<T>(this T @this) where T : struct {
Byte[] data = new Byte[Marshal.SizeOf(@this)];
GCHandle handle = GCHandle.Alloc(data, GCHandleType.Pinned);
try {
Marshal.StructureToPtr(@this, handle.AddrOfPinnedObject(), false);
return GetStructBytes<T>(data);
} finally {
handle.Free();
}
}
/// <summary>
/// Swaps the endianness of an unsigned long to an unsigned integer.
/// </summary>
/// <param name="this">The bytes contained in a long.</param>
/// <returns>
/// A 32-bit unsigned integer equivalent to the ulong
/// contained in longBytes.
/// </returns>
public static UInt32 SwapEndianness(this UInt64 @this) => (UInt32)(((@this & 0x000000ff) << 24) + ((@this & 0x0000ff00) << 8) + ((@this & 0x00ff0000) >> 8) + ((@this & 0xff000000) >> 24));
private static Byte[] GetStructBytes<T>(Byte[] data) {
if(data == null) {
throw new ArgumentNullException(nameof(data));
}
FieldInfo[] fields = typeof(T).GetTypeInfo()
.GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
StructEndiannessAttribute endian = AttributeCache.DefaultCache.Value.RetrieveOne<StructEndiannessAttribute, T>();
foreach(FieldInfo field in fields) {
if(endian == null && !field.IsDefined(typeof(StructEndiannessAttribute), false)) {
continue;
}
Int32 offset = Marshal.OffsetOf<T>(field.Name).ToInt32();
Int32 length = Marshal.SizeOf(field.FieldType);
endian ??= AttributeCache.DefaultCache.Value.RetrieveOne<StructEndiannessAttribute>(field);
if(endian != null && (endian.Endianness == Endianness.Big && BitConverter.IsLittleEndian ||
endian.Endianness == Endianness.Little && !BitConverter.IsLittleEndian)) {
Array.Reverse(data, offset, length);
}
}
return data;
}
}
}

View File

@ -1,276 +1,230 @@
using System;
#nullable enable
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Swan.Lite.Reflection;
using Swan.Mappers;
using Swan.Reflection;
namespace Swan
{
namespace Swan {
/// <summary>
/// Extension methods.
/// </summary>
public static partial class Extensions {
/// <summary>
/// Extension methods.
/// Iterates over the public, instance, readable properties of the source and
/// tries to write a compatible value to a public, instance, writable property in the destination.
/// </summary>
public static partial class Extensions
{
/// <summary>
/// Iterates over the public, instance, readable properties of the source and
/// tries to write a compatible value to a public, instance, writable property in the destination.
/// </summary>
/// <typeparam name="T">The type of the source.</typeparam>
/// <param name="source">The source.</param>
/// <param name="target">The target.</param>
/// <param name="ignoreProperties">The ignore properties.</param>
/// <returns>
/// Number of properties that was copied successful.
/// </returns>
public static int CopyPropertiesTo<T>(this T source, object target, params string[]? ignoreProperties)
where T : class =>
ObjectMapper.Copy(source, target, GetCopyableProperties(target), ignoreProperties);
/// <summary>
/// Iterates over the public, instance, readable properties of the source and
/// tries to write a compatible value to a public, instance, writable property in the destination.
/// </summary>
/// <param name="source">The source.</param>
/// <param name="target">The destination.</param>
/// <param name="propertiesToCopy">Properties to copy.</param>
/// <returns>
/// Number of properties that were successfully copied.
/// </returns>
public static int CopyOnlyPropertiesTo(this object source, object target, params string[]? propertiesToCopy)
=> ObjectMapper.Copy(source, target, propertiesToCopy);
/// <summary>
/// Copies the properties to new instance of T.
/// </summary>
/// <typeparam name="T">The new object type.</typeparam>
/// <param name="source">The source.</param>
/// <param name="ignoreProperties">The ignore properties.</param>
/// <returns>
/// The specified type with properties copied.
/// </returns>
/// <exception cref="ArgumentNullException">source.</exception>
public static T CopyPropertiesToNew<T>(this object source, string[]? ignoreProperties = null)
where T : class
{
if (source == null)
throw new ArgumentNullException(nameof(source));
var target = Activator.CreateInstance<T>();
ObjectMapper.Copy(source, target, GetCopyableProperties(target), ignoreProperties);
return target;
}
/// <summary>
/// Copies the only properties to new instance of T.
/// </summary>
/// <typeparam name="T">Object Type.</typeparam>
/// <param name="source">The source.</param>
/// <param name="propertiesToCopy">The properties to copy.</param>
/// <returns>
/// The specified type with properties copied.
/// </returns>
/// <exception cref="ArgumentNullException">source.</exception>
public static T CopyOnlyPropertiesToNew<T>(this object source, params string[] propertiesToCopy)
where T : class
{
if (source == null)
throw new ArgumentNullException(nameof(source));
var target = Activator.CreateInstance<T>();
ObjectMapper.Copy(source, target, propertiesToCopy);
return target;
}
/// <summary>
/// Iterates over the keys of the source and tries to write a compatible value to a public,
/// instance, writable property in the destination.
/// </summary>
/// <param name="source">The source.</param>
/// <param name="target">The target.</param>
/// <param name="ignoreKeys">The ignore keys.</param>
/// <returns>Number of properties that was copied successful.</returns>
public static int CopyKeyValuePairTo(
this IDictionary<string, object> source,
object target,
params string[] ignoreKeys) =>
source == null
? throw new ArgumentNullException(nameof(source))
: ObjectMapper.Copy(source, target, null, ignoreKeys);
/// <summary>
/// Iterates over the keys of the source and tries to write a compatible value to a public,
/// instance, writable property in the destination.
/// </summary>
/// <typeparam name="T">Object Type.</typeparam>
/// <param name="source">The source.</param>
/// <param name="ignoreKeys">The ignore keys.</param>
/// <returns>
/// The specified type with properties copied.
/// </returns>
public static T CopyKeyValuePairToNew<T>(
this IDictionary<string, object> source,
params string[] ignoreKeys)
{
if (source == null)
throw new ArgumentNullException(nameof(source));
var target = Activator.CreateInstance<T>();
source.CopyKeyValuePairTo(target, ignoreKeys);
return target;
}
/// <summary>
/// Does the specified action.
/// </summary>
/// <param name="action">The action.</param>
/// <param name="retryInterval">The retry interval.</param>
/// <param name="retryCount">The retry count.</param>
public static void Retry(
this Action action,
TimeSpan retryInterval = default,
int retryCount = 3)
{
if (action == null)
throw new ArgumentNullException(nameof(action));
Retry<object?>(() =>
{
action();
return null;
},
retryInterval,
retryCount);
}
/// <summary>
/// Does the specified action.
/// </summary>
/// <typeparam name="T">The type of the source.</typeparam>
/// <param name="action">The action.</param>
/// <param name="retryInterval">The retry interval.</param>
/// <param name="retryCount">The retry count.</param>
/// <returns>
/// The return value of the method that this delegate encapsulates.
/// </returns>
/// <exception cref="ArgumentNullException">action.</exception>
/// <exception cref="AggregateException">Represents one or many errors that occur during application execution.</exception>
public static T Retry<T>(
this Func<T> action,
TimeSpan retryInterval = default,
int retryCount = 3)
{
if (action == null)
throw new ArgumentNullException(nameof(action));
if (retryInterval == default)
retryInterval = TimeSpan.FromSeconds(1);
var exceptions = new List<Exception>();
for (var retry = 0; retry < retryCount; retry++)
{
try
{
if (retry > 0)
Task.Delay(retryInterval).Wait();
return action();
}
catch (Exception ex)
{
exceptions.Add(ex);
}
}
throw new AggregateException(exceptions);
}
/// <summary>
/// Gets the copyable properties.
///
/// If there is no properties with the attribute <c>AttributeCache</c> returns all the properties.
/// </summary>
/// <param name="this">The object.</param>
/// <returns>
/// Array of properties.
/// </returns>
/// <exception cref="ArgumentNullException">model.</exception>
/// <seealso cref="AttributeCache"/>
public static IEnumerable<string> GetCopyableProperties(this object @this)
{
if (@this == null)
throw new ArgumentNullException(nameof(@this));
var collection = PropertyTypeCache.DefaultCache.Value
.RetrieveAllProperties(@this.GetType(), true);
var properties = collection
.Select(x => new
{
x.Name,
HasAttribute = AttributeCache.DefaultCache.Value.RetrieveOne<CopyableAttribute>(x) != null,
})
.Where(x => x.HasAttribute)
.Select(x => x.Name);
return properties.Any()
? properties
: collection.Select(x => x.Name);
}
internal static void CreateTarget(
this object source,
Type targetType,
bool includeNonPublic,
ref object? target)
{
switch (source)
{
// do nothing. Simply skip creation
case string _:
break;
// When using arrays, there is no default constructor, attempt to build a compatible array
case IList sourceObjectList when targetType.IsArray:
var elementType = targetType.GetElementType();
if (elementType != null)
target = Array.CreateInstance(elementType, sourceObjectList.Count);
break;
default:
var constructors = ConstructorTypeCache.DefaultCache.Value
.RetrieveAllConstructors(targetType, includeNonPublic);
// Try to check if empty constructor is available
if (constructors.Any(x => x.Item2.Length == 0))
{
target = Activator.CreateInstance(targetType, includeNonPublic);
}
else
{
var firstCtor = constructors
.OrderBy(x => x.Item2.Length)
.FirstOrDefault();
target = Activator.CreateInstance(targetType,
firstCtor?.Item2.Select(arg => arg.GetType().GetDefault()).ToArray());
}
break;
}
}
internal static string GetNameWithCase(this string name, JsonSerializerCase jsonSerializerCase) =>
jsonSerializerCase switch
{
JsonSerializerCase.PascalCase => char.ToUpperInvariant(name[0]) + name.Substring(1),
JsonSerializerCase.CamelCase => char.ToLowerInvariant(name[0]) + name.Substring(1),
JsonSerializerCase.None => name,
_ => throw new ArgumentOutOfRangeException(nameof(jsonSerializerCase), jsonSerializerCase, null)
};
}
/// <typeparam name="T">The type of the source.</typeparam>
/// <param name="source">The source.</param>
/// <param name="target">The target.</param>
/// <param name="ignoreProperties">The ignore properties.</param>
/// <returns>
/// Number of properties that was copied successful.
/// </returns>
public static Int32 CopyPropertiesTo<T>(this T source, Object? target, params String[]? ignoreProperties) where T : class => ObjectMapper.Copy(source, target, GetCopyableProperties(target), ignoreProperties);
/// <summary>
/// Iterates over the public, instance, readable properties of the source and
/// tries to write a compatible value to a public, instance, writable property in the destination.
/// </summary>
/// <param name="source">The source.</param>
/// <param name="target">The destination.</param>
/// <param name="propertiesToCopy">Properties to copy.</param>
/// <returns>
/// Number of properties that were successfully copied.
/// </returns>
public static Int32 CopyOnlyPropertiesTo(this Object source, Object target, params String[]? propertiesToCopy) => ObjectMapper.Copy(source, target, propertiesToCopy);
/// <summary>
/// Copies the properties to new instance of T.
/// </summary>
/// <typeparam name="T">The new object type.</typeparam>
/// <param name="source">The source.</param>
/// <param name="ignoreProperties">The ignore properties.</param>
/// <returns>
/// The specified type with properties copied.
/// </returns>
/// <exception cref="ArgumentNullException">source.</exception>
public static T CopyPropertiesToNew<T>(this Object source, String[]? ignoreProperties = null) where T : class {
if(source == null) {
throw new ArgumentNullException(nameof(source));
}
T target = Activator.CreateInstance<T>();
_ = ObjectMapper.Copy(source, target, GetCopyableProperties(target), ignoreProperties);
return target;
}
/// <summary>
/// Copies the only properties to new instance of T.
/// </summary>
/// <typeparam name="T">Object Type.</typeparam>
/// <param name="source">The source.</param>
/// <param name="propertiesToCopy">The properties to copy.</param>
/// <returns>
/// The specified type with properties copied.
/// </returns>
/// <exception cref="ArgumentNullException">source.</exception>
public static T CopyOnlyPropertiesToNew<T>(this Object source, params String[] propertiesToCopy) where T : class {
if(source == null) {
throw new ArgumentNullException(nameof(source));
}
T target = Activator.CreateInstance<T>();
_ = ObjectMapper.Copy(source, target, propertiesToCopy);
return target;
}
/// <summary>
/// Iterates over the keys of the source and tries to write a compatible value to a public,
/// instance, writable property in the destination.
/// </summary>
/// <param name="source">The source.</param>
/// <param name="target">The target.</param>
/// <param name="ignoreKeys">The ignore keys.</param>
/// <returns>Number of properties that was copied successful.</returns>
public static Int32 CopyKeyValuePairTo(this IDictionary<String, Object> source, Object? target, params String[] ignoreKeys) => source == null ? throw new ArgumentNullException(nameof(source)) : ObjectMapper.Copy(source, target, null, ignoreKeys);
/// <summary>
/// Iterates over the keys of the source and tries to write a compatible value to a public,
/// instance, writable property in the destination.
/// </summary>
/// <typeparam name="T">Object Type.</typeparam>
/// <param name="source">The source.</param>
/// <param name="ignoreKeys">The ignore keys.</param>
/// <returns>
/// The specified type with properties copied.
/// </returns>
public static T CopyKeyValuePairToNew<T>(this IDictionary<String, Object> source, params String[] ignoreKeys) {
if(source == null) {
throw new ArgumentNullException(nameof(source));
}
T target = Activator.CreateInstance<T>();
_ = source.CopyKeyValuePairTo(target, ignoreKeys);
return target;
}
/// <summary>
/// Does the specified action.
/// </summary>
/// <param name="action">The action.</param>
/// <param name="retryInterval">The retry interval.</param>
/// <param name="retryCount">The retry count.</param>
public static void Retry(this Action action, TimeSpan retryInterval = default, Int32 retryCount = 3) {
if(action == null) {
throw new ArgumentNullException(nameof(action));
}
_ = Retry<Object?>(() => { action(); return null; }, retryInterval, retryCount);
}
/// <summary>
/// Does the specified action.
/// </summary>
/// <typeparam name="T">The type of the source.</typeparam>
/// <param name="action">The action.</param>
/// <param name="retryInterval">The retry interval.</param>
/// <param name="retryCount">The retry count.</param>
/// <returns>
/// The return value of the method that this delegate encapsulates.
/// </returns>
/// <exception cref="ArgumentNullException">action.</exception>
/// <exception cref="AggregateException">Represents one or many errors that occur during application execution.</exception>
public static T Retry<T>(this Func<T> action, TimeSpan retryInterval = default, Int32 retryCount = 3) {
if(action == null) {
throw new ArgumentNullException(nameof(action));
}
if(retryInterval == default) {
retryInterval = TimeSpan.FromSeconds(1);
}
global::System.Collections.Generic.List<global::System.Exception> exceptions = new List<Exception>();
for(Int32 retry = 0; retry < retryCount; retry++) {
try {
if(retry > 0) {
Task.Delay(retryInterval).Wait();
}
return action();
} catch(Exception ex) {
exceptions.Add(ex);
}
}
throw new AggregateException(exceptions);
}
/// <summary>
/// Gets the copyable properties.
///
/// If there is no properties with the attribute <c>AttributeCache</c> returns all the properties.
/// </summary>
/// <param name="this">The object.</param>
/// <returns>
/// Array of properties.
/// </returns>
/// <exception cref="ArgumentNullException">model.</exception>
/// <seealso cref="AttributeCache"/>
public static IEnumerable<String> GetCopyableProperties(this Object? @this) {
if(@this == null) {
throw new ArgumentNullException(nameof(@this));
}
global::System.Collections.Generic.IEnumerable<global::System.Reflection.PropertyInfo> collection = PropertyTypeCache.DefaultCache.Value.RetrieveAllProperties(@this.GetType(), true);
global::System.Collections.Generic.IEnumerable<global::System.String> properties = collection.Select(x => new {
x.Name,
HasAttribute = AttributeCache.DefaultCache.Value.RetrieveOne<CopyableAttribute>(x) != null,
}).Where(x => x.HasAttribute).Select(x => x.Name);
return properties.Any() ? properties : collection.Select(x => x.Name);
}
internal static void CreateTarget(this Object source, Type targetType, Boolean includeNonPublic, ref Object? target) {
switch(source) {
// do nothing. Simply skip creation
case String _:
break;
// When using arrays, there is no default constructor, attempt to build a compatible array
case IList sourceObjectList when targetType.IsArray:
Type? elementType = targetType.GetElementType();
if(elementType != null) {
target = Array.CreateInstance(elementType, sourceObjectList.Count);
}
break;
default:
IEnumerable<Tuple<System.Reflection.ConstructorInfo, System.Reflection.ParameterInfo[]>> constructors = ConstructorTypeCache.DefaultCache.Value.RetrieveAllConstructors(targetType, includeNonPublic);
// Try to check if empty constructor is available
if(constructors.Any(x => x.Item2.Length == 0)) {
target = Activator.CreateInstance(targetType, includeNonPublic);
} else {
Tuple<System.Reflection.ConstructorInfo, System.Reflection.ParameterInfo[]> firstCtor = constructors.OrderBy(x => x.Item2.Length).FirstOrDefault();
target = Activator.CreateInstance(targetType, firstCtor?.Item2.Select(arg => arg.GetType().GetDefault()).ToArray());
}
break;
}
}
internal static String GetNameWithCase(this String name, JsonSerializerCase jsonSerializerCase) => jsonSerializerCase switch
{
JsonSerializerCase.PascalCase => Char.ToUpperInvariant(name[0]) + name.Substring(1),
JsonSerializerCase.CamelCase => Char.ToLowerInvariant(name[0]) + name.Substring(1),
JsonSerializerCase.None => name,
_ => throw new ArgumentOutOfRangeException(nameof(jsonSerializerCase), jsonSerializerCase, null)
};
}
}

File diff suppressed because it is too large Load Diff

View File

@ -7,453 +7,415 @@ using System.Reflection;
using System.Text;
using Swan.Reflection;
namespace Swan.Formatters
{
namespace Swan.Formatters {
/// <summary>
/// A CSV writer useful for exporting a set of objects.
/// </summary>
/// <example>
/// The following code describes how to save a list of objects into a CSV file.
/// <code>
/// using System.Collections.Generic;
/// using Swan.Formatters;
///
/// class Example
/// {
/// class Person
/// {
/// public string Name { get; set; }
/// public int Age { get; set; }
/// }
///
/// static void Main()
/// {
/// // create a list of people
/// var people = new List&lt;Person&gt;
/// {
/// new Person { Name = "Artyom", Age = 20 },
/// new Person { Name = "Aloy", Age = 18 }
/// }
///
/// // write items inside file.csv
/// CsvWriter.SaveRecords(people, "C:\\Users\\user\\Documents\\file.csv");
///
/// // output
/// // | Name | Age |
/// // | Artyom | 20 |
/// // | Aloy | 18 |
/// }
/// }
/// </code>
/// </example>
public class CsvWriter : IDisposable {
private static readonly PropertyTypeCache TypeCache = new PropertyTypeCache();
private readonly Object _syncLock = new Object();
private readonly Stream _outputStream;
private readonly Encoding _encoding;
private readonly Boolean _leaveStreamOpen;
private Boolean _isDisposing;
private UInt64 _mCount;
#region Constructors
/// <summary>
/// A CSV writer useful for exporting a set of objects.
/// Initializes a new instance of the <see cref="CsvWriter" /> class.
/// </summary>
/// <example>
/// The following code describes how to save a list of objects into a CSV file.
/// <code>
/// using System.Collections.Generic;
/// using Swan.Formatters;
///
/// class Example
/// {
/// class Person
/// {
/// public string Name { get; set; }
/// public int Age { get; set; }
/// }
///
/// static void Main()
/// {
/// // create a list of people
/// var people = new List&lt;Person&gt;
/// {
/// new Person { Name = "Artyom", Age = 20 },
/// new Person { Name = "Aloy", Age = 18 }
/// }
///
/// // write items inside file.csv
/// CsvWriter.SaveRecords(people, "C:\\Users\\user\\Documents\\file.csv");
///
/// // output
/// // | Name | Age |
/// // | Artyom | 20 |
/// // | Aloy | 18 |
/// }
/// }
/// </code>
/// </example>
public class CsvWriter : IDisposable
{
private static readonly PropertyTypeCache TypeCache = new PropertyTypeCache();
private readonly object _syncLock = new object();
private readonly Stream _outputStream;
private readonly Encoding _encoding;
private readonly bool _leaveStreamOpen;
private bool _isDisposing;
private ulong _mCount;
#region Constructors
/// <summary>
/// Initializes a new instance of the <see cref="CsvWriter" /> class.
/// </summary>
/// <param name="outputStream">The output stream.</param>
/// <param name="leaveOpen">if set to <c>true</c> [leave open].</param>
/// <param name="encoding">The encoding.</param>
public CsvWriter(Stream outputStream, bool leaveOpen, Encoding encoding)
{
_outputStream = outputStream;
_encoding = encoding;
_leaveStreamOpen = leaveOpen;
}
/// <summary>
/// Initializes a new instance of the <see cref="CsvWriter"/> class.
/// It automatically closes the stream when disposing this writer.
/// </summary>
/// <param name="outputStream">The output stream.</param>
/// <param name="encoding">The encoding.</param>
public CsvWriter(Stream outputStream, Encoding encoding)
: this(outputStream, false, encoding)
{
// placeholder
}
/// <summary>
/// Initializes a new instance of the <see cref="CsvWriter"/> class.
/// It uses the Windows 1252 encoding and automatically closes
/// the stream upon disposing this writer.
/// </summary>
/// <param name="outputStream">The output stream.</param>
public CsvWriter(Stream outputStream)
: this(outputStream, false, Definitions.Windows1252Encoding)
{
// placeholder
}
/// <summary>
/// Initializes a new instance of the <see cref="CsvWriter"/> class.
/// It opens the file given file, automatically closes the stream upon
/// disposing of this writer, and uses the Windows 1252 encoding.
/// </summary>
/// <param name="filename">The filename.</param>
public CsvWriter(string filename)
: this(File.OpenWrite(filename), false, Definitions.Windows1252Encoding)
{
// placeholder
}
/// <summary>
/// Initializes a new instance of the <see cref="CsvWriter"/> class.
/// It opens the file given file, automatically closes the stream upon
/// disposing of this writer, and uses the given text encoding for output.
/// </summary>
/// <param name="filename">The filename.</param>
/// <param name="encoding">The encoding.</param>
public CsvWriter(string filename, Encoding encoding)
: this(File.OpenWrite(filename), false, encoding)
{
// placeholder
}
#endregion
#region Properties
/// <summary>
/// Gets or sets the field separator character.
/// </summary>
/// <value>
/// The separator character.
/// </value>
public char SeparatorCharacter { get; set; } = ',';
/// <summary>
/// Gets or sets the escape character to use to escape field values.
/// </summary>
/// <value>
/// The escape character.
/// </value>
public char EscapeCharacter { get; set; } = '"';
/// <summary>
/// Gets or sets the new line character sequence to use when writing a line.
/// </summary>
/// <value>
/// The new line sequence.
/// </value>
public string NewLineSequence { get; set; } = Environment.NewLine;
/// <summary>
/// Defines a list of properties to ignore when outputting CSV lines.
/// </summary>
/// <value>
/// The ignore property names.
/// </value>
public List<string> IgnorePropertyNames { get; } = new List<string>();
/// <summary>
/// Gets number of lines that have been written, including the headings line.
/// </summary>
/// <value>
/// The count.
/// </value>
public ulong Count
{
get
{
lock (_syncLock)
{
return _mCount;
}
}
}
#endregion
#region Helpers
/// <summary>
/// Saves the items to a stream.
/// It uses the Windows 1252 text encoding for output.
/// </summary>
/// <typeparam name="T">The type of enumeration.</typeparam>
/// <param name="items">The items.</param>
/// <param name="stream">The stream.</param>
/// <param name="truncateData"><c>true</c> if stream is truncated, default <c>false</c>.</param>
/// <returns>Number of item saved.</returns>
public static int SaveRecords<T>(IEnumerable<T> items, Stream stream, bool truncateData = false)
{
// truncate the file if it had data
if (truncateData && stream.Length > 0)
stream.SetLength(0);
using (var writer = new CsvWriter(stream))
{
writer.WriteHeadings<T>();
writer.WriteObjects(items);
return (int)writer.Count;
}
}
/// <summary>
/// Saves the items to a CSV file.
/// If the file exits, it overwrites it. If it does not, it creates it.
/// It uses the Windows 1252 text encoding for output.
/// </summary>
/// <typeparam name="T">The type of enumeration.</typeparam>
/// <param name="items">The items.</param>
/// <param name="filePath">The file path.</param>
/// <returns>Number of item saved.</returns>
public static int SaveRecords<T>(IEnumerable<T> items, string filePath) => SaveRecords(items, File.OpenWrite(filePath), true);
#endregion
#region Generic, main Write Line Method
/// <summary>
/// Writes a line of CSV text. Items are converted to strings.
/// If items are found to be null, empty strings are written out.
/// If items are not string, the ToStringInvariant() method is called on them.
/// </summary>
/// <param name="items">The items.</param>
public void WriteLine(params object[] items)
=> WriteLine(items.Select(x => x == null ? string.Empty : x.ToStringInvariant()));
/// <summary>
/// Writes a line of CSV text. Items are converted to strings.
/// If items are found to be null, empty strings are written out.
/// If items are not string, the ToStringInvariant() method is called on them.
/// </summary>
/// <param name="items">The items.</param>
public void WriteLine(IEnumerable<object> items)
=> WriteLine(items.Select(x => x == null ? string.Empty : x.ToStringInvariant()));
/// <summary>
/// Writes a line of CSV text.
/// If items are found to be null, empty strings are written out.
/// </summary>
/// <param name="items">The items.</param>
public void WriteLine(params string[] items) => WriteLine((IEnumerable<string>) items);
/// <summary>
/// Writes a line of CSV text.
/// If items are found to be null, empty strings are written out.
/// </summary>
/// <param name="items">The items.</param>
public void WriteLine(IEnumerable<string> items)
{
lock (_syncLock)
{
var length = items.Count();
var separatorBytes = _encoding.GetBytes(new[] { SeparatorCharacter });
var endOfLineBytes = _encoding.GetBytes(NewLineSequence);
// Declare state variables here to avoid recreation, allocation and
// reassignment in every loop
bool needsEnclosing;
string textValue;
byte[] output;
for (var i = 0; i < length; i++)
{
textValue = items.ElementAt(i);
// Determine if we need the string to be enclosed
// (it either contains an escape, new line, or separator char)
needsEnclosing = textValue.IndexOf(SeparatorCharacter) >= 0
|| textValue.IndexOf(EscapeCharacter) >= 0
|| textValue.IndexOf('\r') >= 0
|| textValue.IndexOf('\n') >= 0;
// Escape the escape characters by repeating them twice for every instance
textValue = textValue.Replace($"{EscapeCharacter}",
$"{EscapeCharacter}{EscapeCharacter}");
// Enclose the text value if we need to
if (needsEnclosing)
textValue = string.Format($"{EscapeCharacter}{textValue}{EscapeCharacter}", textValue);
// Get the bytes to write to the stream and write them
output = _encoding.GetBytes(textValue);
_outputStream.Write(output, 0, output.Length);
// only write a separator if we are moving in between values.
// the last value should not be written.
if (i < length - 1)
_outputStream.Write(separatorBytes, 0, separatorBytes.Length);
}
// output the newline sequence
_outputStream.Write(endOfLineBytes, 0, endOfLineBytes.Length);
_mCount += 1;
}
}
#endregion
#region Write Object Method
/// <summary>
/// Writes a row of CSV text. It handles the special cases where the object is
/// a dynamic object or and array. It also handles non-collection objects fine.
/// If you do not like the way the output is handled, you can simply write an extension
/// method of this class and use the WriteLine method instead.
/// </summary>
/// <param name="item">The item.</param>
/// <exception cref="System.ArgumentNullException">item.</exception>
public void WriteObject(object item)
{
if (item == null)
throw new ArgumentNullException(nameof(item));
lock (_syncLock)
{
switch (item)
{
case IDictionary typedItem:
WriteLine(GetFilteredDictionary(typedItem));
return;
case ICollection typedItem:
WriteLine(typedItem.Cast<object>());
return;
default:
WriteLine(GetFilteredTypeProperties(item.GetType())
.Select(x => x.ToFormattedString(item)));
break;
}
}
}
/// <summary>
/// Writes a row of CSV text. It handles the special cases where the object is
/// a dynamic object or and array. It also handles non-collection objects fine.
/// If you do not like the way the output is handled, you can simply write an extension
/// method of this class and use the WriteLine method instead.
/// </summary>
/// <typeparam name="T">The type of object to write.</typeparam>
/// <param name="item">The item.</param>
public void WriteObject<T>(T item) => WriteObject(item as object);
/// <summary>
/// Writes a set of items, one per line and atomically by repeatedly calling the
/// WriteObject method. For more info check out the description of the WriteObject
/// method.
/// </summary>
/// <typeparam name="T">The type of object to write.</typeparam>
/// <param name="items">The items.</param>
public void WriteObjects<T>(IEnumerable<T> items)
{
lock (_syncLock)
{
foreach (var item in items)
WriteObject(item);
}
}
#endregion
#region Write Headings Methods
/// <summary>
/// Writes the headings.
/// </summary>
/// <param name="type">The type of object to extract headings.</param>
/// <exception cref="System.ArgumentNullException">type.</exception>
public void WriteHeadings(Type type)
{
if (type == null)
throw new ArgumentNullException(nameof(type));
var properties = GetFilteredTypeProperties(type).Select(p => p.Name).Cast<object>();
WriteLine(properties);
}
/// <summary>
/// Writes the headings.
/// </summary>
/// <typeparam name="T">The type of object to extract headings.</typeparam>
public void WriteHeadings<T>() => WriteHeadings(typeof(T));
/// <summary>
/// Writes the headings.
/// </summary>
/// <param name="dictionary">The dictionary to extract headings.</param>
/// <exception cref="System.ArgumentNullException">dictionary.</exception>
public void WriteHeadings(IDictionary dictionary)
{
if (dictionary == null)
throw new ArgumentNullException(nameof(dictionary));
WriteLine(GetFilteredDictionary(dictionary, true));
}
/// <summary>
/// Writes the headings.
/// </summary>
/// <param name="obj">The object to extract headings.</param>
/// <exception cref="ArgumentNullException">obj.</exception>
public void WriteHeadings(object obj)
{
if (obj == null)
throw new ArgumentNullException(nameof(obj));
WriteHeadings(obj.GetType());
}
#endregion
#region IDisposable Support
/// <inheritdoc />
public void Dispose() => Dispose(true);
/// <summary>
/// Releases unmanaged and - optionally - managed resources.
/// </summary>
/// <param name="disposeAlsoManaged"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.</param>
protected virtual void Dispose(bool disposeAlsoManaged)
{
if (_isDisposing) return;
if (disposeAlsoManaged)
{
if (_leaveStreamOpen == false)
{
_outputStream.Dispose();
}
}
_isDisposing = true;
}
#endregion
#region Support Methods
private IEnumerable<string> GetFilteredDictionary(IDictionary dictionary, bool filterKeys = false)
=> dictionary
.Keys
.Cast<object>()
.Select(key => key == null ? string.Empty : key.ToStringInvariant())
.Where(stringKey => !IgnorePropertyNames.Contains(stringKey))
.Select(stringKey =>
filterKeys
? stringKey
: dictionary[stringKey] == null ? string.Empty : dictionary[stringKey].ToStringInvariant());
private IEnumerable<PropertyInfo> GetFilteredTypeProperties(Type type)
=> TypeCache.Retrieve(type, t =>
t.GetProperties(BindingFlags.Public | BindingFlags.Instance)
.Where(p => p.CanRead))
.Where(p => !IgnorePropertyNames.Contains(p.Name));
#endregion
}
/// <param name="outputStream">The output stream.</param>
/// <param name="leaveOpen">if set to <c>true</c> [leave open].</param>
/// <param name="encoding">The encoding.</param>
public CsvWriter(Stream outputStream, Boolean leaveOpen, Encoding encoding) {
this._outputStream = outputStream;
this._encoding = encoding;
this._leaveStreamOpen = leaveOpen;
}
/// <summary>
/// Initializes a new instance of the <see cref="CsvWriter"/> class.
/// It automatically closes the stream when disposing this writer.
/// </summary>
/// <param name="outputStream">The output stream.</param>
/// <param name="encoding">The encoding.</param>
public CsvWriter(Stream outputStream, Encoding encoding) : this(outputStream, false, encoding) {
// placeholder
}
/// <summary>
/// Initializes a new instance of the <see cref="CsvWriter"/> class.
/// It uses the Windows 1252 encoding and automatically closes
/// the stream upon disposing this writer.
/// </summary>
/// <param name="outputStream">The output stream.</param>
public CsvWriter(Stream outputStream) : this(outputStream, false, Definitions.Windows1252Encoding) {
// placeholder
}
/// <summary>
/// Initializes a new instance of the <see cref="CsvWriter"/> class.
/// It opens the file given file, automatically closes the stream upon
/// disposing of this writer, and uses the Windows 1252 encoding.
/// </summary>
/// <param name="filename">The filename.</param>
public CsvWriter(String filename) : this(File.OpenWrite(filename), false, Definitions.Windows1252Encoding) {
// placeholder
}
/// <summary>
/// Initializes a new instance of the <see cref="CsvWriter"/> class.
/// It opens the file given file, automatically closes the stream upon
/// disposing of this writer, and uses the given text encoding for output.
/// </summary>
/// <param name="filename">The filename.</param>
/// <param name="encoding">The encoding.</param>
public CsvWriter(String filename, Encoding encoding) : this(File.OpenWrite(filename), false, encoding) {
// placeholder
}
#endregion
#region Properties
/// <summary>
/// Gets or sets the field separator character.
/// </summary>
/// <value>
/// The separator character.
/// </value>
public Char SeparatorCharacter { get; set; } = ',';
/// <summary>
/// Gets or sets the escape character to use to escape field values.
/// </summary>
/// <value>
/// The escape character.
/// </value>
public Char EscapeCharacter { get; set; } = '"';
/// <summary>
/// Gets or sets the new line character sequence to use when writing a line.
/// </summary>
/// <value>
/// The new line sequence.
/// </value>
public String NewLineSequence { get; set; } = Environment.NewLine;
/// <summary>
/// Defines a list of properties to ignore when outputting CSV lines.
/// </summary>
/// <value>
/// The ignore property names.
/// </value>
public List<String> IgnorePropertyNames { get; } = new List<String>();
/// <summary>
/// Gets number of lines that have been written, including the headings line.
/// </summary>
/// <value>
/// The count.
/// </value>
public UInt64 Count {
get {
lock(this._syncLock) {
return this._mCount;
}
}
}
#endregion
#region Helpers
/// <summary>
/// Saves the items to a stream.
/// It uses the Windows 1252 text encoding for output.
/// </summary>
/// <typeparam name="T">The type of enumeration.</typeparam>
/// <param name="items">The items.</param>
/// <param name="stream">The stream.</param>
/// <param name="truncateData"><c>true</c> if stream is truncated, default <c>false</c>.</param>
/// <returns>Number of item saved.</returns>
public static Int32 SaveRecords<T>(IEnumerable<T> items, Stream stream, Boolean truncateData = false) {
// truncate the file if it had data
if(truncateData && stream.Length > 0) {
stream.SetLength(0);
}
using CsvWriter writer = new CsvWriter(stream);
writer.WriteHeadings<T>();
writer.WriteObjects(items);
return (Int32)writer.Count;
}
/// <summary>
/// Saves the items to a CSV file.
/// If the file exits, it overwrites it. If it does not, it creates it.
/// It uses the Windows 1252 text encoding for output.
/// </summary>
/// <typeparam name="T">The type of enumeration.</typeparam>
/// <param name="items">The items.</param>
/// <param name="filePath">The file path.</param>
/// <returns>Number of item saved.</returns>
public static Int32 SaveRecords<T>(IEnumerable<T> items, String filePath) => SaveRecords(items, File.OpenWrite(filePath), true);
#endregion
#region Generic, main Write Line Method
/// <summary>
/// Writes a line of CSV text. Items are converted to strings.
/// If items are found to be null, empty strings are written out.
/// If items are not string, the ToStringInvariant() method is called on them.
/// </summary>
/// <param name="items">The items.</param>
public void WriteLine(params Object[] items) => this.WriteLine(items.Select(x => x == null ? String.Empty : x.ToStringInvariant()));
/// <summary>
/// Writes a line of CSV text. Items are converted to strings.
/// If items are found to be null, empty strings are written out.
/// If items are not string, the ToStringInvariant() method is called on them.
/// </summary>
/// <param name="items">The items.</param>
public void WriteLine(IEnumerable<Object> items) => this.WriteLine(items.Select(x => x == null ? String.Empty : x.ToStringInvariant()));
/// <summary>
/// Writes a line of CSV text.
/// If items are found to be null, empty strings are written out.
/// </summary>
/// <param name="items">The items.</param>
public void WriteLine(params String[] items) => this.WriteLine((IEnumerable<String>)items);
/// <summary>
/// Writes a line of CSV text.
/// If items are found to be null, empty strings are written out.
/// </summary>
/// <param name="items">The items.</param>
public void WriteLine(IEnumerable<String> items) {
lock(this._syncLock) {
Int32 length = items.Count();
Byte[] separatorBytes = this._encoding.GetBytes(new[] { this.SeparatorCharacter });
Byte[] endOfLineBytes = this._encoding.GetBytes(this.NewLineSequence);
// Declare state variables here to avoid recreation, allocation and
// reassignment in every loop
Boolean needsEnclosing;
String textValue;
Byte[] output;
for(Int32 i = 0; i < length; i++) {
textValue = items.ElementAt(i);
// Determine if we need the string to be enclosed
// (it either contains an escape, new line, or separator char)
needsEnclosing = textValue.IndexOf(this.SeparatorCharacter) >= 0 || textValue.IndexOf(this.EscapeCharacter) >= 0 || textValue.IndexOf('\r') >= 0 || textValue.IndexOf('\n') >= 0;
// Escape the escape characters by repeating them twice for every instance
textValue = textValue.Replace($"{this.EscapeCharacter}", $"{this.EscapeCharacter}{this.EscapeCharacter}");
// Enclose the text value if we need to
if(needsEnclosing) {
textValue = String.Format($"{this.EscapeCharacter}{textValue}{this.EscapeCharacter}", textValue);
}
// Get the bytes to write to the stream and write them
output = this._encoding.GetBytes(textValue);
this._outputStream.Write(output, 0, output.Length);
// only write a separator if we are moving in between values.
// the last value should not be written.
if(i < length - 1) {
this._outputStream.Write(separatorBytes, 0, separatorBytes.Length);
}
}
// output the newline sequence
this._outputStream.Write(endOfLineBytes, 0, endOfLineBytes.Length);
this._mCount += 1;
}
}
#endregion
#region Write Object Method
/// <summary>
/// Writes a row of CSV text. It handles the special cases where the object is
/// a dynamic object or and array. It also handles non-collection objects fine.
/// If you do not like the way the output is handled, you can simply write an extension
/// method of this class and use the WriteLine method instead.
/// </summary>
/// <param name="item">The item.</param>
/// <exception cref="System.ArgumentNullException">item.</exception>
public void WriteObject(Object item) {
if(item == null) {
throw new ArgumentNullException(nameof(item));
}
lock(this._syncLock) {
switch(item) {
case IDictionary typedItem:
this.WriteLine(this.GetFilteredDictionary(typedItem));
return;
case ICollection typedItem:
this.WriteLine(typedItem.Cast<Object>());
return;
default:
this.WriteLine(this.GetFilteredTypeProperties(item.GetType()).Select(x => x.ToFormattedString(item)));
break;
}
}
}
/// <summary>
/// Writes a row of CSV text. It handles the special cases where the object is
/// a dynamic object or and array. It also handles non-collection objects fine.
/// If you do not like the way the output is handled, you can simply write an extension
/// method of this class and use the WriteLine method instead.
/// </summary>
/// <typeparam name="T">The type of object to write.</typeparam>
/// <param name="item">The item.</param>
public void WriteObject<T>(T item) => this.WriteObject(item as Object);
/// <summary>
/// Writes a set of items, one per line and atomically by repeatedly calling the
/// WriteObject method. For more info check out the description of the WriteObject
/// method.
/// </summary>
/// <typeparam name="T">The type of object to write.</typeparam>
/// <param name="items">The items.</param>
public void WriteObjects<T>(IEnumerable<T> items) {
lock(this._syncLock) {
foreach(T item in items) {
this.WriteObject(item);
}
}
}
#endregion
#region Write Headings Methods
/// <summary>
/// Writes the headings.
/// </summary>
/// <param name="type">The type of object to extract headings.</param>
/// <exception cref="System.ArgumentNullException">type.</exception>
public void WriteHeadings(Type type) {
if(type == null) {
throw new ArgumentNullException(nameof(type));
}
IEnumerable<Object> properties = this.GetFilteredTypeProperties(type).Select(p => p.Name).Cast<Object>();
this.WriteLine(properties);
}
/// <summary>
/// Writes the headings.
/// </summary>
/// <typeparam name="T">The type of object to extract headings.</typeparam>
public void WriteHeadings<T>() => this.WriteHeadings(typeof(T));
/// <summary>
/// Writes the headings.
/// </summary>
/// <param name="dictionary">The dictionary to extract headings.</param>
/// <exception cref="System.ArgumentNullException">dictionary.</exception>
public void WriteHeadings(IDictionary dictionary) {
if(dictionary == null) {
throw new ArgumentNullException(nameof(dictionary));
}
this.WriteLine(this.GetFilteredDictionary(dictionary, true));
}
/// <summary>
/// Writes the headings.
/// </summary>
/// <param name="obj">The object to extract headings.</param>
/// <exception cref="ArgumentNullException">obj.</exception>
public void WriteHeadings(Object obj) {
if(obj == null) {
throw new ArgumentNullException(nameof(obj));
}
this.WriteHeadings(obj.GetType());
}
#endregion
#region IDisposable Support
/// <inheritdoc />
public void Dispose() => this.Dispose(true);
/// <summary>
/// Releases unmanaged and - optionally - managed resources.
/// </summary>
/// <param name="disposeAlsoManaged"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.</param>
protected virtual void Dispose(Boolean disposeAlsoManaged) {
if(this._isDisposing) {
return;
}
if(disposeAlsoManaged) {
if(this._leaveStreamOpen == false) {
this._outputStream.Dispose();
}
}
this._isDisposing = true;
}
#endregion
#region Support Methods
private IEnumerable<String> GetFilteredDictionary(IDictionary dictionary, Boolean filterKeys = false) => dictionary.Keys.Cast<Object>()
.Select(key => key == null ? String.Empty : key.ToStringInvariant())
.Where(stringKey => !this.IgnorePropertyNames.Contains(stringKey))
.Select(stringKey => filterKeys ? stringKey : dictionary[stringKey] == null ? String.Empty : dictionary[stringKey].ToStringInvariant());
private IEnumerable<PropertyInfo> GetFilteredTypeProperties(Type type) => TypeCache.Retrieve(type, t => t.GetProperties(BindingFlags.Public | BindingFlags.Instance).Where(p => p.CanRead)).Where(p => !this.IgnorePropertyNames.Contains(p.Name));
#endregion
}
}

View File

@ -1,150 +1,126 @@
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System;
namespace Swan.Formatters
{
internal class HumanizeJson
{
private readonly StringBuilder _builder = new StringBuilder();
private readonly int _indent;
private readonly string _indentStr;
private readonly object _obj;
public HumanizeJson(object obj, int indent)
{
if (obj == null)
{
return;
}
_indent = indent;
_indentStr = new string(' ', indent * 4);
_obj = obj;
ParseObject();
}
public string GetResult() => _builder == null ? string.Empty : _builder.ToString().TrimEnd();
private void ParseObject()
{
switch (_obj)
{
case Dictionary<string, object> dictionary:
AppendDictionary(dictionary);
break;
case List<object> list:
AppendList(list);
break;
default:
AppendString();
break;
}
}
private void AppendDictionary(Dictionary<string, object> objects)
{
foreach (var kvp in objects)
{
if (kvp.Value == null) continue;
var writeOutput = false;
switch (kvp.Value)
{
case Dictionary<string, object> valueDictionary:
if (valueDictionary.Count > 0)
{
writeOutput = true;
_builder
.Append($"{_indentStr}{kvp.Key,-16}: object")
.AppendLine();
}
break;
case List<object> valueList:
if (valueList.Count > 0)
{
writeOutput = true;
_builder
.Append($"{_indentStr}{kvp.Key,-16}: array[{valueList.Count}]")
.AppendLine();
}
break;
default:
writeOutput = true;
_builder.Append($"{_indentStr}{kvp.Key,-16}: ");
break;
}
if (writeOutput)
_builder.AppendLine(new HumanizeJson(kvp.Value, _indent + 1).GetResult());
}
}
private void AppendList(List<object> objects)
{
var index = 0;
foreach (var value in objects)
{
var writeOutput = false;
switch (value)
{
case Dictionary<string, object> valueDictionary:
if (valueDictionary.Count > 0)
{
writeOutput = true;
_builder
.Append($"{_indentStr}[{index}]: object")
.AppendLine();
}
break;
case List<object> valueList:
if (valueList.Count > 0)
{
writeOutput = true;
_builder
.Append($"{_indentStr}[{index}]: array[{valueList.Count}]")
.AppendLine();
}
break;
default:
writeOutput = true;
_builder.Append($"{_indentStr}[{index}]: ");
break;
}
index++;
if (writeOutput)
_builder.AppendLine(new HumanizeJson(value, _indent + 1).GetResult());
}
}
private void AppendString()
{
var stringValue = _obj.ToString();
if (stringValue.Length + _indentStr.Length > 96 || stringValue.IndexOf('\r') >= 0 ||
stringValue.IndexOf('\n') >= 0)
{
_builder.AppendLine();
var stringLines = stringValue.ToLines().Select(l => l.Trim());
foreach (var line in stringLines)
{
_builder.AppendLine($"{_indentStr}{line}");
}
}
else
{
_builder.Append($"{stringValue}");
}
}
}
namespace Swan.Formatters {
internal class HumanizeJson {
private readonly StringBuilder _builder = new StringBuilder();
private readonly Int32 _indent;
private readonly String _indentStr;
private readonly Object _obj;
public HumanizeJson(Object obj, Int32 indent) {
if(obj == null) {
return;
}
this._indent = indent;
this._indentStr = new String(' ', indent * 4);
this._obj = obj;
this.ParseObject();
}
public String GetResult() => this._builder == null ? String.Empty : this._builder.ToString().TrimEnd();
private void ParseObject() {
switch(this._obj) {
case Dictionary<String, Object> dictionary:
this.AppendDictionary(dictionary);
break;
case List<Object> list:
this.AppendList(list);
break;
default:
this.AppendString();
break;
}
}
private void AppendDictionary(Dictionary<String, Object> objects) {
foreach(KeyValuePair<String, Object> kvp in objects) {
if(kvp.Value == null) {
continue;
}
Boolean writeOutput = false;
switch(kvp.Value) {
case Dictionary<String, Object> valueDictionary:
if(valueDictionary.Count > 0) {
writeOutput = true;
_ = this._builder.Append($"{this._indentStr}{kvp.Key,-16}: object").AppendLine();
}
break;
case List<Object> valueList:
if(valueList.Count > 0) {
writeOutput = true;
_ = this._builder.Append($"{this._indentStr}{kvp.Key,-16}: array[{valueList.Count}]").AppendLine();
}
break;
default:
writeOutput = true;
_ = this._builder.Append($"{this._indentStr}{kvp.Key,-16}: ");
break;
}
if(writeOutput) {
_ = this._builder.AppendLine(new HumanizeJson(kvp.Value, this._indent + 1).GetResult());
}
}
}
private void AppendList(List<Object> objects) {
Int32 index = 0;
foreach(Object value in objects) {
Boolean writeOutput = false;
switch(value) {
case Dictionary<String, Object> valueDictionary:
if(valueDictionary.Count > 0) {
writeOutput = true;
_ = this._builder.Append($"{this._indentStr}[{index}]: object").AppendLine();
}
break;
case List<Object> valueList:
if(valueList.Count > 0) {
writeOutput = true;
_ = this._builder.Append($"{this._indentStr}[{index}]: array[{valueList.Count}]").AppendLine();
}
break;
default:
writeOutput = true;
_ = this._builder.Append($"{this._indentStr}[{index}]: ");
break;
}
index++;
if(writeOutput) {
_ = this._builder.AppendLine(new HumanizeJson(value, this._indent + 1).GetResult());
}
}
}
private void AppendString() {
String stringValue = this._obj.ToString();
if(stringValue.Length + this._indentStr.Length > 96 || stringValue.IndexOf('\r') >= 0 ||
stringValue.IndexOf('\n') >= 0) {
_ = this._builder.AppendLine();
IEnumerable<String> stringLines = stringValue.ToLines().Select(l => l.Trim());
foreach(String line in stringLines) {
_ = this._builder.AppendLine($"{this._indentStr}{line}");
}
} else {
_ = this._builder.Append($"{stringValue}");
}
}
}
}

View File

@ -1,4 +1,5 @@
using System;
#nullable enable
using System;
using System.Collections;
using System.Collections.Concurrent;
using System.Collections.Generic;
@ -7,332 +8,252 @@ using System.Reflection;
using System.Text;
using Swan.Reflection;
namespace Swan.Formatters
{
/// <summary>
/// A very simple, light-weight JSON library written by Mario
/// to teach Geo how things are done
///
/// This is an useful helper for small tasks but it doesn't represent a full-featured
/// serializer such as the beloved Json.NET.
/// </summary>
public static partial class Json
{
private class Converter
{
private static readonly ConcurrentDictionary<MemberInfo, string> MemberInfoNameCache =
new ConcurrentDictionary<MemberInfo, string>();
private static readonly ConcurrentDictionary<Type, Type> ListAddMethodCache = new ConcurrentDictionary<Type, Type>();
private readonly object? _target;
private readonly Type _targetType;
private readonly bool _includeNonPublic;
private readonly JsonSerializerCase _jsonSerializerCase;
private Converter(
object? source,
Type targetType,
ref object? targetInstance,
bool includeNonPublic,
JsonSerializerCase jsonSerializerCase)
{
_targetType = targetInstance != null ? targetInstance.GetType() : targetType;
_includeNonPublic = includeNonPublic;
_jsonSerializerCase = jsonSerializerCase;
if (source == null)
{
return;
}
var sourceType = source.GetType();
if (_targetType == null || _targetType == typeof(object)) _targetType = sourceType;
if (sourceType == _targetType)
{
_target = source;
return;
}
if (!TrySetInstance(targetInstance, source, ref _target))
return;
ResolveObject(source, ref _target);
}
internal static object? FromJsonResult(
object? source,
JsonSerializerCase jsonSerializerCase,
Type? targetType = null,
bool includeNonPublic = false)
{
object? nullRef = null;
return new Converter(source, targetType ?? typeof(object), ref nullRef, includeNonPublic, jsonSerializerCase).GetResult();
}
private static object? FromJsonResult(object source,
Type targetType,
ref object? targetInstance,
bool includeNonPublic)
{
return new Converter(source, targetType, ref targetInstance, includeNonPublic, JsonSerializerCase.None).GetResult();
}
private static Type? GetAddMethodParameterType(Type targetType)
=> ListAddMethodCache.GetOrAdd(targetType,
x => x.GetMethods()
.FirstOrDefault(
m => m.Name == AddMethodName && m.IsPublic && m.GetParameters().Length == 1)?
.GetParameters()[0]
.ParameterType);
private static void GetByteArray(string sourceString, ref object? target)
{
try
{
target = Convert.FromBase64String(sourceString);
} // Try conversion from Base 64
catch (FormatException)
{
target = Encoding.UTF8.GetBytes(sourceString);
} // Get the string bytes in UTF8
}
private object GetSourcePropertyValue(
IDictionary<string, object> sourceProperties,
MemberInfo targetProperty)
{
var targetPropertyName = MemberInfoNameCache.GetOrAdd(
targetProperty,
x => AttributeCache.DefaultCache.Value.RetrieveOne<JsonPropertyAttribute>(x)?.PropertyName ?? x.Name.GetNameWithCase(_jsonSerializerCase));
return sourceProperties.GetValueOrDefault(targetPropertyName);
}
private bool TrySetInstance(object? targetInstance, object source, ref object? target)
{
if (targetInstance == null)
{
// Try to create a default instance
try
{
source.CreateTarget(_targetType, _includeNonPublic, ref target);
}
catch
{
return false;
}
}
else
{
target = targetInstance;
}
return true;
}
private object? GetResult() => _target ?? _targetType.GetDefault();
private void ResolveObject(object source, ref object? target)
{
switch (source)
{
// Case 0: Special Cases Handling (Source and Target are of specific convertible types)
// Case 0.1: Source is string, Target is byte[]
case string sourceString when _targetType == typeof(byte[]):
GetByteArray(sourceString, ref target);
break;
// Case 1.1: Source is Dictionary, Target is IDictionary
case Dictionary<string, object> sourceProperties when target is IDictionary targetDictionary:
PopulateDictionary(sourceProperties, targetDictionary);
break;
// Case 1.2: Source is Dictionary, Target is not IDictionary (i.e. it is a complex type)
case Dictionary<string, object> sourceProperties:
PopulateObject(sourceProperties);
break;
// Case 2.1: Source is List, Target is Array
case List<object> sourceList when target is Array targetArray:
PopulateArray(sourceList, targetArray);
break;
// Case 2.2: Source is List, Target is IList
case List<object> sourceList when target is IList targetList:
PopulateIList(sourceList, targetList);
break;
// Case 3: Source is a simple type; Attempt conversion
default:
var sourceStringValue = source.ToStringInvariant();
// Handle basic types or enumerations if not
if (!_targetType.TryParseBasicType(sourceStringValue, out target))
GetEnumValue(sourceStringValue, ref target);
break;
}
}
private void PopulateIList(IEnumerable<object> objects, IList list)
{
var parameterType = GetAddMethodParameterType(_targetType);
if (parameterType == null) return;
foreach (var item in objects)
{
try
{
list.Add(FromJsonResult(
item,
_jsonSerializerCase,
parameterType,
_includeNonPublic));
}
catch
{
// ignored
}
}
}
private void PopulateArray(IList<object> objects, Array array)
{
var elementType = _targetType.GetElementType();
for (var i = 0; i < objects.Count; i++)
{
try
{
var targetItem = FromJsonResult(
objects[i],
_jsonSerializerCase,
elementType,
_includeNonPublic);
array.SetValue(targetItem, i);
}
catch
{
// ignored
}
}
}
private void GetEnumValue(string sourceStringValue, ref object? target)
{
var enumType = Nullable.GetUnderlyingType(_targetType);
if (enumType == null && _targetType.IsEnum) enumType = _targetType;
if (enumType == null) return;
try
{
target = Enum.Parse(enumType, sourceStringValue);
}
catch
{
// ignored
}
}
private void PopulateDictionary(IDictionary<string, object> sourceProperties, IDictionary targetDictionary)
{
// find the add method of the target dictionary
var addMethod = _targetType.GetMethods()
.FirstOrDefault(
m => m.Name == AddMethodName && m.IsPublic && m.GetParameters().Length == 2);
// skip if we don't have a compatible add method
if (addMethod == null) return;
var addMethodParameters = addMethod.GetParameters();
if (addMethodParameters[0].ParameterType != typeof(string)) return;
// Retrieve the target entry type
var targetEntryType = addMethodParameters[1].ParameterType;
// Add the items to the target dictionary
foreach (var sourceProperty in sourceProperties)
{
try
{
var targetEntryValue = FromJsonResult(
sourceProperty.Value,
_jsonSerializerCase,
targetEntryType,
_includeNonPublic);
targetDictionary.Add(sourceProperty.Key, targetEntryValue);
}
catch
{
// ignored
}
}
}
private void PopulateObject(IDictionary<string, object> sourceProperties)
{
if (_targetType.IsValueType)
{
PopulateFields(sourceProperties);
}
PopulateProperties(sourceProperties);
}
private void PopulateProperties(IDictionary<string, object> sourceProperties)
{
var properties = PropertyTypeCache.DefaultCache.Value.RetrieveFilteredProperties(_targetType, false, p => p.CanWrite);
foreach (var property in properties)
{
var sourcePropertyValue = GetSourcePropertyValue(sourceProperties, property);
if (sourcePropertyValue == null) continue;
try
{
var currentPropertyValue = !property.PropertyType.IsArray
? property.GetCacheGetMethod(_includeNonPublic)(_target)
: null;
var targetPropertyValue = FromJsonResult(
sourcePropertyValue,
property.PropertyType,
ref currentPropertyValue,
_includeNonPublic);
property.GetCacheSetMethod(_includeNonPublic)(_target, new[] { targetPropertyValue });
}
catch
{
// ignored
}
}
}
private void PopulateFields(IDictionary<string, object> sourceProperties)
{
foreach (var field in FieldTypeCache.DefaultCache.Value.RetrieveAllFields(_targetType))
{
var sourcePropertyValue = GetSourcePropertyValue(sourceProperties, field);
if (sourcePropertyValue == null) continue;
var targetPropertyValue = FromJsonResult(
sourcePropertyValue,
_jsonSerializerCase,
field.FieldType,
_includeNonPublic);
try
{
field.SetValue(_target, targetPropertyValue);
}
catch
{
// ignored
}
}
}
}
}
namespace Swan.Formatters {
/// <summary>
/// A very simple, light-weight JSON library written by Mario
/// to teach Geo how things are done
///
/// This is an useful helper for small tasks but it doesn't represent a full-featured
/// serializer such as the beloved Json.NET.
/// </summary>
public static partial class Json {
private class Converter {
private static readonly ConcurrentDictionary<MemberInfo, String> MemberInfoNameCache = new ConcurrentDictionary<MemberInfo, global::System.String>();
private static readonly ConcurrentDictionary<Type, Type> ListAddMethodCache = new ConcurrentDictionary<Type, Type>();
private readonly Object? _target;
private readonly Type _targetType;
private readonly Boolean _includeNonPublic;
private readonly JsonSerializerCase _jsonSerializerCase;
private Converter(Object? source, Type targetType, ref Object? targetInstance, Boolean includeNonPublic, JsonSerializerCase jsonSerializerCase) {
this._targetType = targetInstance != null ? targetInstance.GetType() : targetType;
this._includeNonPublic = includeNonPublic;
this._jsonSerializerCase = jsonSerializerCase;
if(source == null) {
return;
}
Type sourceType = source.GetType();
if(this._targetType == null || this._targetType == typeof(Object)) {
this._targetType = sourceType;
}
if(sourceType == this._targetType) {
this._target = source;
return;
}
if(!this.TrySetInstance(targetInstance, source, ref this._target)) {
return;
}
this.ResolveObject(source, ref this._target);
}
internal static Object? FromJsonResult(Object? source, JsonSerializerCase jsonSerializerCase, Type? targetType = null, Boolean includeNonPublic = false) {
Object? nullRef = null;
return new Converter(source, targetType ?? typeof(Object), ref nullRef, includeNonPublic, jsonSerializerCase).GetResult();
}
private static Object? FromJsonResult(Object source, Type targetType, ref Object? targetInstance, Boolean includeNonPublic) => new Converter(source, targetType, ref targetInstance, includeNonPublic, JsonSerializerCase.None).GetResult();
private static Type? GetAddMethodParameterType(Type targetType) => ListAddMethodCache.GetOrAdd(targetType, x => x.GetMethods().FirstOrDefault(m => m.Name == AddMethodName && m.IsPublic && m.GetParameters().Length == 1)?.GetParameters()[0].ParameterType!);
private static void GetByteArray(String sourceString, ref Object? target) {
try {
target = Convert.FromBase64String(sourceString);
} // Try conversion from Base 64
catch(FormatException) {
target = Encoding.UTF8.GetBytes(sourceString);
} // Get the string bytes in UTF8
}
private Object GetSourcePropertyValue(IDictionary<String, Object> sourceProperties, MemberInfo targetProperty) {
String targetPropertyName = MemberInfoNameCache.GetOrAdd(targetProperty, x => AttributeCache.DefaultCache.Value.RetrieveOne<JsonPropertyAttribute>(x)?.PropertyName ?? x.Name.GetNameWithCase(this._jsonSerializerCase));
return sourceProperties.GetValueOrDefault(targetPropertyName);
}
private Boolean TrySetInstance(Object? targetInstance, Object source, ref Object? target) {
if(targetInstance == null) {
// Try to create a default instance
try {
source.CreateTarget(this._targetType, this._includeNonPublic, ref target);
} catch {
return false;
}
} else {
target = targetInstance;
}
return true;
}
private Object? GetResult() => this._target ?? this._targetType.GetDefault();
private void ResolveObject(Object source, ref Object? target) {
switch(source) {
// Case 0: Special Cases Handling (Source and Target are of specific convertible types)
// Case 0.1: Source is string, Target is byte[]
case String sourceString when this._targetType == typeof(Byte[]):
GetByteArray(sourceString, ref target);
break;
// Case 1.1: Source is Dictionary, Target is IDictionary
case Dictionary<String, Object> sourceProperties when target is IDictionary targetDictionary:
this.PopulateDictionary(sourceProperties, targetDictionary);
break;
// Case 1.2: Source is Dictionary, Target is not IDictionary (i.e. it is a complex type)
case Dictionary<String, Object> sourceProperties:
this.PopulateObject(sourceProperties);
break;
// Case 2.1: Source is List, Target is Array
case List<Object> sourceList when target is Array targetArray:
this.PopulateArray(sourceList, targetArray);
break;
// Case 2.2: Source is List, Target is IList
case List<Object> sourceList when target is IList targetList:
this.PopulateIList(sourceList, targetList);
break;
// Case 3: Source is a simple type; Attempt conversion
default:
String sourceStringValue = source.ToStringInvariant();
// Handle basic types or enumerations if not
if(!this._targetType.TryParseBasicType(sourceStringValue, out target)) {
this.GetEnumValue(sourceStringValue, ref target);
}
break;
}
}
private void PopulateIList(IEnumerable<Object> objects, IList list) {
Type? parameterType = GetAddMethodParameterType(this._targetType);
if(parameterType == null) {
return;
}
foreach(Object item in objects) {
try {
_ = list.Add(FromJsonResult(item, this._jsonSerializerCase, parameterType, this._includeNonPublic));
} catch {
// ignored
}
}
}
private void PopulateArray(IList<Object> objects, Array array) {
Type? elementType = this._targetType.GetElementType();
for(Int32 i = 0; i < objects.Count; i++) {
try {
Object? targetItem = FromJsonResult(objects[i], this._jsonSerializerCase, elementType, this._includeNonPublic);
array.SetValue(targetItem, i);
} catch {
// ignored
}
}
}
private void GetEnumValue(String sourceStringValue, ref Object? target) {
Type? enumType = Nullable.GetUnderlyingType(this._targetType);
if(enumType == null && this._targetType.IsEnum) {
enumType = this._targetType;
}
if(enumType == null) {
return;
}
try {
target = Enum.Parse(enumType, sourceStringValue);
} catch {
// ignored
}
}
private void PopulateDictionary(IDictionary<String, Object> sourceProperties, IDictionary targetDictionary) {
// find the add method of the target dictionary
MethodInfo addMethod = this._targetType.GetMethods().FirstOrDefault(m => m.Name == AddMethodName && m.IsPublic && m.GetParameters().Length == 2);
// skip if we don't have a compatible add method
if(addMethod == null) {
return;
}
global::System.Reflection.ParameterInfo[] addMethodParameters = addMethod.GetParameters();
if(addMethodParameters[0].ParameterType != typeof(String)) {
return;
}
// Retrieve the target entry type
Type targetEntryType = addMethodParameters[1].ParameterType;
// Add the items to the target dictionary
foreach(KeyValuePair<String, Object> sourceProperty in sourceProperties) {
try {
Object? targetEntryValue = FromJsonResult(sourceProperty.Value, this._jsonSerializerCase, targetEntryType, this._includeNonPublic);
targetDictionary.Add(sourceProperty.Key, targetEntryValue);
} catch {
// ignored
}
}
}
private void PopulateObject(IDictionary<String, Object> sourceProperties) {
if(this._targetType.IsValueType) {
this.PopulateFields(sourceProperties);
}
this.PopulateProperties(sourceProperties);
}
private void PopulateProperties(IDictionary<String, Object> sourceProperties) {
global::System.Collections.Generic.IEnumerable<global::System.Reflection.PropertyInfo> properties = PropertyTypeCache.DefaultCache.Value.RetrieveFilteredProperties(this._targetType, false, p => p.CanWrite);
foreach(PropertyInfo property in properties) {
Object sourcePropertyValue = this.GetSourcePropertyValue(sourceProperties, property);
if(sourcePropertyValue == null) {
continue;
}
try {
Object? currentPropertyValue = !property.PropertyType.IsArray ? property?.GetCacheGetMethod(this._includeNonPublic)!(this._target!) : null;
Object? targetPropertyValue = FromJsonResult(sourcePropertyValue, property.PropertyType, ref currentPropertyValue, this._includeNonPublic);
property?.GetCacheSetMethod(this._includeNonPublic)!(this._target!, new[] { targetPropertyValue }!);
} catch {
// ignored
}
}
}
private void PopulateFields(IDictionary<String, Object> sourceProperties) {
foreach(FieldInfo field in FieldTypeCache.DefaultCache.Value.RetrieveAllFields(this._targetType)) {
Object sourcePropertyValue = this.GetSourcePropertyValue(sourceProperties, field);
if(sourcePropertyValue == null) {
continue;
}
Object? targetPropertyValue = FromJsonResult(sourcePropertyValue, this._jsonSerializerCase, field.FieldType, this._includeNonPublic);
try {
field.SetValue(this._target, targetPropertyValue);
} catch {
// ignored
}
}
}
}
}
}

View File

@ -1,348 +1,332 @@
using System;
#nullable enable
using System;
using System.Collections.Generic;
using System.Text;
namespace Swan.Formatters
{
namespace Swan.Formatters {
/// <summary>
/// A very simple, light-weight JSON library written by Mario
/// to teach Geo how things are done
///
/// This is an useful helper for small tasks but it doesn't represent a full-featured
/// serializer such as the beloved Json.NET.
/// </summary>
public partial class Json {
/// <summary>
/// A very simple, light-weight JSON library written by Mario
/// to teach Geo how things are done
///
/// This is an useful helper for small tasks but it doesn't represent a full-featured
/// serializer such as the beloved Json.NET.
/// A simple JSON Deserializer.
/// </summary>
public partial class Json
{
/// <summary>
/// A simple JSON Deserializer.
/// </summary>
private class Deserializer
{
#region State Variables
private readonly object? _result;
private readonly string _json;
private Dictionary<string, object> _resultObject;
private List<object> _resultArray;
private ReadState _state = ReadState.WaitingForRootOpen;
private string? _currentFieldName;
private int _index;
#endregion
private Deserializer(string json, int startIndex)
{
_json = json;
for (_index = startIndex; _index < _json.Length; _index++)
{
switch (_state)
{
case ReadState.WaitingForRootOpen:
WaitForRootOpen();
continue;
case ReadState.WaitingForField when char.IsWhiteSpace(_json, _index):
continue;
case ReadState.WaitingForField when (_resultObject != null && _json[_index] == CloseObjectChar)
|| (_resultArray != null && _json[_index] == CloseArrayChar):
// Handle empty arrays and empty objects
_result = _resultObject ?? _resultArray as object;
return;
case ReadState.WaitingForField when _json[_index] != StringQuotedChar:
throw CreateParserException($"'{StringQuotedChar}'");
case ReadState.WaitingForField:
{
var charCount = GetFieldNameCount();
_currentFieldName = Unescape(_json.SliceLength(_index + 1, charCount));
_index += charCount + 1;
_state = ReadState.WaitingForColon;
continue;
}
case ReadState.WaitingForColon when char.IsWhiteSpace(_json, _index):
continue;
case ReadState.WaitingForColon when _json[_index] != ValueSeparatorChar:
throw CreateParserException($"'{ValueSeparatorChar}'");
case ReadState.WaitingForColon:
_state = ReadState.WaitingForValue;
continue;
case ReadState.WaitingForValue when char.IsWhiteSpace(_json, _index):
continue;
case ReadState.WaitingForValue when (_resultObject != null && _json[_index] == CloseObjectChar)
|| (_resultArray != null && _json[_index] == CloseArrayChar):
// Handle empty arrays and empty objects
_result = _resultObject ?? _resultArray as object;
return;
case ReadState.WaitingForValue:
ExtractValue();
continue;
}
if (_state != ReadState.WaitingForNextOrRootClose || char.IsWhiteSpace(_json, _index)) continue;
if (_json[_index] == FieldSeparatorChar)
{
if (_resultObject != null)
{
_state = ReadState.WaitingForField;
_currentFieldName = null;
continue;
}
_state = ReadState.WaitingForValue;
continue;
}
if ((_resultObject == null || _json[_index] != CloseObjectChar) &&
(_resultArray == null || _json[_index] != CloseArrayChar))
{
throw CreateParserException($"'{FieldSeparatorChar}' '{CloseObjectChar}' or '{CloseArrayChar}'");
}
_result = _resultObject ?? _resultArray as object;
return;
}
}
internal static object? DeserializeInternal(string json) => new Deserializer(json, 0)._result;
private void WaitForRootOpen()
{
if (char.IsWhiteSpace(_json, _index)) return;
switch (_json[_index])
{
case OpenObjectChar:
_resultObject = new Dictionary<string, object>();
_state = ReadState.WaitingForField;
return;
case OpenArrayChar:
_resultArray = new List<object>();
_state = ReadState.WaitingForValue;
return;
default:
throw CreateParserException($"'{OpenObjectChar}' or '{OpenArrayChar}'");
}
}
private void ExtractValue()
{
// determine the value based on what it starts with
switch (_json[_index])
{
case StringQuotedChar: // expect a string
ExtractStringQuoted();
break;
case OpenObjectChar: // expect object
case OpenArrayChar: // expect array
ExtractObject();
break;
case 't': // expect true
ExtractConstant(TrueLiteral, true);
break;
case 'f': // expect false
ExtractConstant(FalseLiteral, false);
break;
case 'n': // expect null
ExtractConstant(NullLiteral, null);
break;
default: // expect number
ExtractNumber();
break;
}
_currentFieldName = null;
_state = ReadState.WaitingForNextOrRootClose;
}
private static string Unescape(string str)
{
// check if we need to unescape at all
if (str.IndexOf(StringEscapeChar) < 0)
return str;
var builder = new StringBuilder(str.Length);
for (var i = 0; i < str.Length; i++)
{
if (str[i] != StringEscapeChar)
{
builder.Append(str[i]);
continue;
}
if (i + 1 > str.Length - 1)
break;
// escape sequence begins here
switch (str[i + 1])
{
case 'u':
i = ExtractEscapeSequence(str, i, builder);
break;
case 'b':
builder.Append('\b');
i += 1;
break;
case 't':
builder.Append('\t');
i += 1;
break;
case 'n':
builder.Append('\n');
i += 1;
break;
case 'f':
builder.Append('\f');
i += 1;
break;
case 'r':
builder.Append('\r');
i += 1;
break;
default:
builder.Append(str[i + 1]);
i += 1;
break;
}
}
return builder.ToString();
}
private static int ExtractEscapeSequence(string str, int i, StringBuilder builder)
{
var startIndex = i + 2;
var endIndex = i + 5;
if (endIndex > str.Length - 1)
{
builder.Append(str[i + 1]);
i += 1;
return i;
}
var hexCode = str.Slice(startIndex, endIndex).ConvertHexadecimalToBytes();
builder.Append(Encoding.BigEndianUnicode.GetChars(hexCode));
i += 5;
return i;
}
private int GetFieldNameCount()
{
var charCount = 0;
for (var j = _index + 1; j < _json.Length; j++)
{
if (_json[j] == StringQuotedChar && _json[j - 1] != StringEscapeChar)
break;
charCount++;
}
return charCount;
}
private void ExtractObject()
{
// Extract and set the value
var deserializer = new Deserializer(_json, _index);
if (_currentFieldName != null)
_resultObject[_currentFieldName] = deserializer._result;
else
_resultArray.Add(deserializer._result);
_index = deserializer._index;
}
private void ExtractNumber()
{
var charCount = 0;
for (var j = _index; j < _json.Length; j++)
{
if (char.IsWhiteSpace(_json[j]) || _json[j] == FieldSeparatorChar
|| (_resultObject != null && _json[j] == CloseObjectChar)
|| (_resultArray != null && _json[j] == CloseArrayChar))
break;
charCount++;
}
// Extract and set the value
var stringValue = _json.SliceLength(_index, charCount);
if (decimal.TryParse(stringValue, System.Globalization.NumberStyles.Number, System.Globalization.CultureInfo.InvariantCulture, out var value) == false)
throw CreateParserException("[number]");
if (_currentFieldName != null)
_resultObject[_currentFieldName] = value;
else
_resultArray.Add(value);
_index += charCount - 1;
}
private void ExtractConstant(string boolValue, bool? value)
{
if (_json.SliceLength(_index, boolValue.Length) != boolValue)
throw CreateParserException($"'{ValueSeparatorChar}'");
// Extract and set the value
if (_currentFieldName != null)
_resultObject[_currentFieldName] = value;
else
_resultArray.Add(value);
_index += boolValue.Length - 1;
}
private void ExtractStringQuoted()
{
var charCount = 0;
var escapeCharFound = false;
for (var j = _index + 1; j < _json.Length; j++)
{
if (_json[j] == StringQuotedChar && !escapeCharFound)
break;
escapeCharFound = _json[j] == StringEscapeChar && !escapeCharFound;
charCount++;
}
// Extract and set the value
var value = Unescape(_json.SliceLength(_index + 1, charCount));
if (_currentFieldName != null)
_resultObject[_currentFieldName] = value;
else
_resultArray.Add(value);
_index += charCount + 1;
}
private FormatException CreateParserException(string expected)
{
var textPosition = _json.TextPositionAt(_index);
return new FormatException(
$"Parser error (Line {textPosition.Item1}, Col {textPosition.Item2}, State {_state}): Expected {expected} but got '{_json[_index]}'.");
}
/// <summary>
/// Defines the different JSON read states.
/// </summary>
private enum ReadState
{
WaitingForRootOpen,
WaitingForField,
WaitingForColon,
WaitingForValue,
WaitingForNextOrRootClose,
}
}
}
private class Deserializer {
#region State Variables
private readonly Object? _result;
private readonly String _json;
private Dictionary<String, Object?>? _resultObject;
private List<Object?>? _resultArray;
private ReadState _state = ReadState.WaitingForRootOpen;
private String? _currentFieldName;
private Int32 _index;
#endregion
private Deserializer(String? json, Int32 startIndex) {
if(json == null) {
this._json = "";
return;
}
this._json = json;
for(this._index = startIndex; this._index < this._json.Length; this._index++) {
switch(this._state) {
case ReadState.WaitingForRootOpen:
this.WaitForRootOpen();
continue;
case ReadState.WaitingForField when Char.IsWhiteSpace(this._json, this._index):
continue;
case ReadState.WaitingForField when this._resultObject != null && this._json[this._index] == CloseObjectChar || this._resultArray != null && this._json[this._index] == CloseArrayChar:
// Handle empty arrays and empty objects
this._result = this._resultObject ?? this._resultArray as Object;
return;
case ReadState.WaitingForField when this._json[this._index] != StringQuotedChar:
throw this.CreateParserException($"'{StringQuotedChar}'");
case ReadState.WaitingForField: {
Int32 charCount = this.GetFieldNameCount();
this._currentFieldName = Unescape(this._json.SliceLength(this._index + 1, charCount));
this._index += charCount + 1;
this._state = ReadState.WaitingForColon;
continue;
}
case ReadState.WaitingForColon when Char.IsWhiteSpace(this._json, this._index):
continue;
case ReadState.WaitingForColon when this._json[this._index] != ValueSeparatorChar:
throw this.CreateParserException($"'{ValueSeparatorChar}'");
case ReadState.WaitingForColon:
this._state = ReadState.WaitingForValue;
continue;
case ReadState.WaitingForValue when Char.IsWhiteSpace(this._json, this._index):
continue;
case ReadState.WaitingForValue when this._resultObject != null && this._json[this._index] == CloseObjectChar || this._resultArray != null && this._json[this._index] == CloseArrayChar:
// Handle empty arrays and empty objects
this._result = this._resultObject ?? this._resultArray as Object;
return;
case ReadState.WaitingForValue:
this.ExtractValue();
continue;
}
if(this._state != ReadState.WaitingForNextOrRootClose || Char.IsWhiteSpace(this._json, this._index)) {
continue;
}
if(this._json[this._index] == FieldSeparatorChar) {
if(this._resultObject != null) {
this._state = ReadState.WaitingForField;
this._currentFieldName = null;
continue;
}
this._state = ReadState.WaitingForValue;
continue;
}
if((this._resultObject == null || this._json[this._index] != CloseObjectChar) && (this._resultArray == null || this._json[this._index] != CloseArrayChar)) {
throw this.CreateParserException($"'{FieldSeparatorChar}' '{CloseObjectChar}' or '{CloseArrayChar}'");
}
this._result = this._resultObject ?? this._resultArray as Object;
return;
}
}
internal static Object? DeserializeInternal(String? json) => new Deserializer(json, 0)._result;
private void WaitForRootOpen() {
if(Char.IsWhiteSpace(this._json, this._index)) {
return;
}
switch(this._json[this._index]) {
case OpenObjectChar:
this._resultObject = new Dictionary<String, Object?>();
this._state = ReadState.WaitingForField;
return;
case OpenArrayChar:
this._resultArray = new List<Object?>();
this._state = ReadState.WaitingForValue;
return;
default:
throw this.CreateParserException($"'{OpenObjectChar}' or '{OpenArrayChar}'");
}
}
private void ExtractValue() {
// determine the value based on what it starts with
switch(this._json[this._index]) {
case StringQuotedChar: // expect a string
this.ExtractStringQuoted();
break;
case OpenObjectChar: // expect object
case OpenArrayChar: // expect array
this.ExtractObject();
break;
case 't': // expect true
this.ExtractConstant(TrueLiteral, true);
break;
case 'f': // expect false
this.ExtractConstant(FalseLiteral, false);
break;
case 'n': // expect null
this.ExtractConstant(NullLiteral, null);
break;
default: // expect number
this.ExtractNumber();
break;
}
this._currentFieldName = null;
this._state = ReadState.WaitingForNextOrRootClose;
}
private static String Unescape(String str) {
// check if we need to unescape at all
if(str.IndexOf(StringEscapeChar) < 0) {
return str;
}
StringBuilder builder = new StringBuilder(str.Length);
for(Int32 i = 0; i < str.Length; i++) {
if(str[i] != StringEscapeChar) {
_ = builder.Append(str[i]);
continue;
}
if(i + 1 > str.Length - 1) {
break;
}
// escape sequence begins here
switch(str[i + 1]) {
case 'u':
i = ExtractEscapeSequence(str, i, builder);
break;
case 'b':
_ = builder.Append('\b');
i += 1;
break;
case 't':
_ = builder.Append('\t');
i += 1;
break;
case 'n':
_ = builder.Append('\n');
i += 1;
break;
case 'f':
_ = builder.Append('\f');
i += 1;
break;
case 'r':
_ = builder.Append('\r');
i += 1;
break;
default:
_ = builder.Append(str[i + 1]);
i += 1;
break;
}
}
return builder.ToString();
}
private static Int32 ExtractEscapeSequence(String str, Int32 i, StringBuilder builder) {
Int32 startIndex = i + 2;
Int32 endIndex = i + 5;
if(endIndex > str.Length - 1) {
_ = builder.Append(str[i + 1]);
i += 1;
return i;
}
Byte[] hexCode = str.Slice(startIndex, endIndex).ConvertHexadecimalToBytes();
_ = builder.Append(Encoding.BigEndianUnicode.GetChars(hexCode));
i += 5;
return i;
}
private Int32 GetFieldNameCount() {
Int32 charCount = 0;
for(Int32 j = this._index + 1; j < this._json.Length; j++) {
if(this._json[j] == StringQuotedChar && this._json[j - 1] != StringEscapeChar) {
break;
}
charCount++;
}
return charCount;
}
private void ExtractObject() {
// Extract and set the value
Deserializer deserializer = new Deserializer(this._json, this._index);
if(this._currentFieldName != null) {
this._resultObject![this._currentFieldName] = deserializer._result!;
} else {
this._resultArray!.Add(deserializer._result!);
}
this._index = deserializer._index;
}
private void ExtractNumber() {
Int32 charCount = 0;
for(Int32 j = this._index; j < this._json.Length; j++) {
if(Char.IsWhiteSpace(this._json[j]) || this._json[j] == FieldSeparatorChar || this._resultObject != null && this._json[j] == CloseObjectChar || this._resultArray != null && this._json[j] == CloseArrayChar) {
break;
}
charCount++;
}
// Extract and set the value
String stringValue = this._json.SliceLength(this._index, charCount);
if(Decimal.TryParse(stringValue, System.Globalization.NumberStyles.Number, System.Globalization.CultureInfo.InvariantCulture, out Decimal value) == false) {
throw this.CreateParserException("[number]");
}
if(this._currentFieldName != null) {
this._resultObject![this._currentFieldName] = value;
} else {
this._resultArray!.Add(value);
}
this._index += charCount - 1;
}
private void ExtractConstant(String boolValue, Boolean? value) {
if(this._json.SliceLength(this._index, boolValue.Length) != boolValue) {
throw this.CreateParserException($"'{ValueSeparatorChar}'");
}
// Extract and set the value
if(this._currentFieldName != null) {
this._resultObject![this._currentFieldName] = value;
} else {
this._resultArray!.Add(value);
}
this._index += boolValue.Length - 1;
}
private void ExtractStringQuoted() {
Int32 charCount = 0;
Boolean escapeCharFound = false;
for(Int32 j = this._index + 1; j < this._json.Length; j++) {
if(this._json[j] == StringQuotedChar && !escapeCharFound) {
break;
}
escapeCharFound = this._json[j] == StringEscapeChar && !escapeCharFound;
charCount++;
}
// Extract and set the value
String value = Unescape(this._json.SliceLength(this._index + 1, charCount));
if(this._currentFieldName != null) {
this._resultObject![this._currentFieldName] = value;
} else {
this._resultArray!.Add(value);
}
this._index += charCount + 1;
}
private FormatException CreateParserException(String expected) {
Tuple<Int32, Int32> textPosition = this._json.TextPositionAt(this._index);
return new FormatException($"Parser error (Line {textPosition.Item1}, Col {textPosition.Item2}, State {this._state}): Expected {expected} but got '{this._json[this._index]}'.");
}
/// <summary>
/// Defines the different JSON read states.
/// </summary>
private enum ReadState {
WaitingForRootOpen,
WaitingForField,
WaitingForColon,
WaitingForValue,
WaitingForNextOrRootClose,
}
}
}
}

View File

@ -1,348 +1,331 @@
using System;
#nullable enable
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
namespace Swan.Formatters
{
namespace Swan.Formatters {
/// <summary>
/// A very simple, light-weight JSON library written by Mario
/// to teach Geo how things are done
///
/// This is an useful helper for small tasks but it doesn't represent a full-featured
/// serializer such as the beloved Json.NET.
/// </summary>
public partial class Json {
/// <summary>
/// A very simple, light-weight JSON library written by Mario
/// to teach Geo how things are done
///
/// This is an useful helper for small tasks but it doesn't represent a full-featured
/// serializer such as the beloved Json.NET.
/// A simple JSON serializer.
/// </summary>
public partial class Json
{
/// <summary>
/// A simple JSON serializer.
/// </summary>
private class Serializer
{
#region Private Declarations
private static readonly Dictionary<int, string> IndentStrings = new Dictionary<int, string>();
private readonly SerializerOptions _options;
private readonly string _result;
private readonly StringBuilder _builder;
private readonly string _lastCommaSearch;
#endregion
#region Constructors
/// <summary>
/// Initializes a new instance of the <see cref="Serializer" /> class.
/// </summary>
/// <param name="obj">The object.</param>
/// <param name="depth">The depth.</param>
/// <param name="options">The options.</param>
private Serializer(object? obj, int depth, SerializerOptions options)
{
if (depth > 20)
{
throw new InvalidOperationException(
"The max depth (20) has been reached. Serializer can not continue.");
}
// Basic Type Handling (nulls, strings, number, date and bool)
_result = ResolveBasicType(obj);
if (!string.IsNullOrWhiteSpace(_result))
return;
_options = options;
// Handle circular references correctly and avoid them
if (options.IsObjectPresent(obj!))
{
_result = $"{{ \"$circref\": \"{Escape(obj!.GetHashCode().ToStringInvariant(), false)}\" }}";
return;
}
// At this point, we will need to construct the object with a StringBuilder.
_lastCommaSearch = FieldSeparatorChar + (_options.Format ? Environment.NewLine : string.Empty);
_builder = new StringBuilder();
_result = obj switch
{
IDictionary itemsZero when itemsZero.Count == 0 => EmptyObjectLiteral,
IDictionary items => ResolveDictionary(items, depth),
IEnumerable enumerableZero when !enumerableZero.Cast<object>().Any() => EmptyArrayLiteral,
IEnumerable enumerableBytes when enumerableBytes is byte[] bytes => Serialize(bytes.ToBase64(), depth, _options),
IEnumerable enumerable => ResolveEnumerable(enumerable, depth),
_ => ResolveObject(obj!, depth)
};
}
internal static string Serialize(object? obj, int depth, SerializerOptions options) => new Serializer(obj, depth, options)._result;
#endregion
#region Helper Methods
private static string ResolveBasicType(object? obj)
{
switch (obj)
{
case null:
return NullLiteral;
case string s:
return Escape(s, true);
case bool b:
return b ? TrueLiteral : FalseLiteral;
case Type _:
case Assembly _:
case MethodInfo _:
case PropertyInfo _:
case EventInfo _:
return Escape(obj.ToString(), true);
case DateTime d:
return $"{StringQuotedChar}{d:s}{StringQuotedChar}";
default:
var targetType = obj.GetType();
if (!Definitions.BasicTypesInfo.Value.ContainsKey(targetType))
return string.Empty;
var escapedValue = Escape(Definitions.BasicTypesInfo.Value[targetType].ToStringInvariant(obj), false);
return decimal.TryParse(escapedValue, out _)
? $"{escapedValue}"
: $"{StringQuotedChar}{escapedValue}{StringQuotedChar}";
}
}
private static bool IsNonEmptyJsonArrayOrObject(string serialized)
{
if (serialized == EmptyObjectLiteral || serialized == EmptyArrayLiteral) return false;
// find the first position the character is not a space
return serialized.Where(c => c != ' ').Select(c => c == OpenObjectChar || c == OpenArrayChar).FirstOrDefault();
}
private static string Escape(string str, bool quoted)
{
if (str == null)
return string.Empty;
var builder = new StringBuilder(str.Length * 2);
if (quoted) builder.Append(StringQuotedChar);
Escape(str, builder);
if (quoted) builder.Append(StringQuotedChar);
return builder.ToString();
}
private static void Escape(string str, StringBuilder builder)
{
foreach (var currentChar in str)
{
switch (currentChar)
{
case '\\':
case '"':
case '/':
builder
.Append('\\')
.Append(currentChar);
break;
case '\b':
builder.Append("\\b");
break;
case '\t':
builder.Append("\\t");
break;
case '\n':
builder.Append("\\n");
break;
case '\f':
builder.Append("\\f");
break;
case '\r':
builder.Append("\\r");
break;
default:
if (currentChar < ' ')
{
var escapeBytes = BitConverter.GetBytes((ushort)currentChar);
if (BitConverter.IsLittleEndian == false)
Array.Reverse(escapeBytes);
builder.Append("\\u")
private class Serializer {
#region Private Declarations
private static readonly Dictionary<Int32, String> IndentStrings = new Dictionary<global::System.Int32, global::System.String>();
private readonly SerializerOptions? _options;
private readonly String _result;
private readonly StringBuilder? _builder;
private readonly String? _lastCommaSearch;
#endregion
#region Constructors
/// <summary>
/// Initializes a new instance of the <see cref="Serializer" /> class.
/// </summary>
/// <param name="obj">The object.</param>
/// <param name="depth">The depth.</param>
/// <param name="options">The options.</param>
private Serializer(Object? obj, Int32 depth, SerializerOptions options) {
if(depth > 20) {
throw new InvalidOperationException("The max depth (20) has been reached. Serializer can not continue.");
}
// Basic Type Handling (nulls, strings, number, date and bool)
this._result = ResolveBasicType(obj);
if(!String.IsNullOrWhiteSpace(this._result)) {
return;
}
this._options = options;
// Handle circular references correctly and avoid them
if(options.IsObjectPresent(obj!)) {
this._result = $"{{ \"$circref\": \"{Escape(obj!.GetHashCode().ToStringInvariant(), false)}\" }}";
return;
}
// At this point, we will need to construct the object with a StringBuilder.
this._lastCommaSearch = FieldSeparatorChar + (this._options.Format ? Environment.NewLine : String.Empty);
this._builder = new StringBuilder();
this._result = obj switch
{
IDictionary itemsZero when itemsZero.Count == 0 => EmptyObjectLiteral,
IDictionary items => this.ResolveDictionary(items, depth),
IEnumerable enumerableZero when !enumerableZero.Cast<Object>().Any() => EmptyArrayLiteral,
IEnumerable enumerableBytes when enumerableBytes is Byte[] bytes => Serialize(bytes.ToBase64(), depth, this._options),
IEnumerable enumerable => this.ResolveEnumerable(enumerable, depth),
_ => this.ResolveObject(obj!, depth)
};
}
internal static String Serialize(Object? obj, Int32 depth, SerializerOptions options) => new Serializer(obj, depth, options)._result;
#endregion
#region Helper Methods
private static String ResolveBasicType(Object? obj) {
switch(obj) {
case null:
return NullLiteral;
case String s:
return Escape(s, true);
case Boolean b:
return b ? TrueLiteral : FalseLiteral;
case Type _:
case Assembly _:
case MethodInfo _:
case PropertyInfo _:
case EventInfo _:
return Escape(obj.ToString()!, true);
case DateTime d:
return $"{StringQuotedChar}{d:s}{StringQuotedChar}";
default:
Type targetType = obj.GetType();
if(!Definitions.BasicTypesInfo.Value.ContainsKey(targetType)) {
return String.Empty;
}
String escapedValue = Escape(Definitions.BasicTypesInfo.Value[targetType].ToStringInvariant(obj), false);
return Decimal.TryParse(escapedValue, out _) ? $"{escapedValue}" : $"{StringQuotedChar}{escapedValue}{StringQuotedChar}";
}
}
private static Boolean IsNonEmptyJsonArrayOrObject(String serialized) {
if(serialized == EmptyObjectLiteral || serialized == EmptyArrayLiteral) {
return false;
}
// find the first position the character is not a space
return serialized.Where(c => c != ' ').Select(c => c == OpenObjectChar || c == OpenArrayChar).FirstOrDefault();
}
private static String Escape(String str, Boolean quoted) {
if(str == null) {
return String.Empty;
}
StringBuilder builder = new StringBuilder(str.Length * 2);
if(quoted) {
_ = builder.Append(StringQuotedChar);
}
Escape(str, builder);
if(quoted) {
_ = builder.Append(StringQuotedChar);
}
return builder.ToString();
}
private static void Escape(String str, StringBuilder builder) {
foreach(Char currentChar in str) {
switch(currentChar) {
case '\\':
case '"':
case '/':
_ = builder
.Append('\\')
.Append(currentChar);
break;
case '\b':
_ = builder.Append("\\b");
break;
case '\t':
_ = builder.Append("\\t");
break;
case '\n':
_ = builder.Append("\\n");
break;
case '\f':
_ = builder.Append("\\f");
break;
case '\r':
_ = builder.Append("\\r");
break;
default:
if(currentChar < ' ') {
Byte[] escapeBytes = BitConverter.GetBytes((UInt16)currentChar);
if(BitConverter.IsLittleEndian == false) {
Array.Reverse(escapeBytes);
}
_ = builder.Append("\\u")
.Append(escapeBytes[1].ToString("X").PadLeft(2, '0'))
.Append(escapeBytes[0].ToString("X").PadLeft(2, '0'));
}
else
{
builder.Append(currentChar);
}
break;
}
}
}
private Dictionary<string, object?> CreateDictionary(
Dictionary<string, MemberInfo> fields,
string targetType,
object target)
{
// Create the dictionary and extract the properties
var objectDictionary = new Dictionary<string, object?>();
if (string.IsNullOrWhiteSpace(_options.TypeSpecifier) == false)
objectDictionary[_options.TypeSpecifier] = targetType;
foreach (var field in fields)
{
// Build the dictionary using property names and values
// Note: used to be: property.GetValue(target); but we would be reading private properties
try
{
objectDictionary[field.Key] = field.Value is PropertyInfo property
? property.GetCacheGetMethod(_options.IncludeNonPublic)?.Invoke(target)
: (field.Value as FieldInfo)?.GetValue(target);
}
catch
{
/* ignored */
}
}
return objectDictionary;
}
private string ResolveDictionary(IDictionary items, int depth)
{
Append(OpenObjectChar, depth);
AppendLine();
// Iterate through the elements and output recursively
var writeCount = 0;
foreach (var key in items.Keys)
{
// Serialize and append the key (first char indented)
Append(StringQuotedChar, depth + 1);
Escape(key.ToString(), _builder);
_builder
.Append(StringQuotedChar)
.Append(ValueSeparatorChar)
.Append(" ");
// Serialize and append the value
var serializedValue = Serialize(items[key], depth + 1, _options);
if (IsNonEmptyJsonArrayOrObject(serializedValue)) AppendLine();
Append(serializedValue, 0);
// Add a comma and start a new line -- We will remove the last one when we are done writing the elements
Append(FieldSeparatorChar, 0);
AppendLine();
writeCount++;
}
// Output the end of the object and set the result
RemoveLastComma();
Append(CloseObjectChar, writeCount > 0 ? depth : 0);
return _builder.ToString();
}
private string ResolveObject(object target, int depth)
{
var targetType = target.GetType();
if (targetType.IsEnum)
return Convert.ToInt64(target, System.Globalization.CultureInfo.InvariantCulture).ToString();
var fields = _options.GetProperties(targetType);
if (fields.Count == 0 && string.IsNullOrWhiteSpace(_options.TypeSpecifier))
return EmptyObjectLiteral;
// If we arrive here, then we convert the object into a
// dictionary of property names and values and call the serialization
// function again
var objectDictionary = CreateDictionary(fields, targetType.ToString(), target);
return Serialize(objectDictionary, depth, _options);
}
private string ResolveEnumerable(IEnumerable target, int depth)
{
// Cast the items as a generic object array
var items = target.Cast<object>();
Append(OpenArrayChar, depth);
AppendLine();
// Iterate through the elements and output recursively
var writeCount = 0;
foreach (var entry in items)
{
var serializedValue = Serialize(entry, depth + 1, _options);
if (IsNonEmptyJsonArrayOrObject(serializedValue))
Append(serializedValue, 0);
else
Append(serializedValue, depth + 1);
Append(FieldSeparatorChar, 0);
AppendLine();
writeCount++;
}
// Output the end of the array and set the result
RemoveLastComma();
Append(CloseArrayChar, writeCount > 0 ? depth : 0);
return _builder.ToString();
}
private void SetIndent(int depth)
{
if (_options.Format == false || depth <= 0) return;
_builder.Append(IndentStrings.GetOrAdd(depth, x => new string(' ', x * 4)));
}
/// <summary>
/// Removes the last comma in the current string builder.
/// </summary>
private void RemoveLastComma()
{
if (_builder.Length < _lastCommaSearch.Length)
return;
if (_lastCommaSearch.Where((t, i) => _builder[_builder.Length - _lastCommaSearch.Length + i] != t).Any())
{
return;
}
// If we got this far, we simply remove the comma character
_builder.Remove(_builder.Length - _lastCommaSearch.Length, 1);
}
private void Append(string text, int depth)
{
SetIndent(depth);
_builder.Append(text);
}
private void Append(char text, int depth)
{
SetIndent(depth);
_builder.Append(text);
}
private void AppendLine()
{
if (_options.Format == false) return;
_builder.Append(Environment.NewLine);
}
#endregion
}
}
.Append(escapeBytes[0].ToString("X").PadLeft(2, '0'));
} else {
_ = builder.Append(currentChar);
}
break;
}
}
}
private Dictionary<String, Object?> CreateDictionary(Dictionary<String, MemberInfo> fields, String targetType, Object target) {
// Create the dictionary and extract the properties
global::System.Collections.Generic.Dictionary<global::System.String, global::System.Object?> objectDictionary = new Dictionary<global::System.String, global::System.Object?>();
if(String.IsNullOrWhiteSpace(this._options?.TypeSpecifier) == false) {
objectDictionary[this._options?.TypeSpecifier!] = targetType;
}
foreach(global::System.Collections.Generic.KeyValuePair<global::System.String, global::System.Reflection.MemberInfo> field in fields) {
// Build the dictionary using property names and values
// Note: used to be: property.GetValue(target); but we would be reading private properties
try {
objectDictionary[field.Key] = field.Value is PropertyInfo property ? property.GetCacheGetMethod((Boolean)(this._options?.IncludeNonPublic)!)?.Invoke(target) : (field.Value as FieldInfo)?.GetValue(target);
} catch {
/* ignored */
}
}
return objectDictionary;
}
private String ResolveDictionary(IDictionary items, Int32 depth) {
this.Append(OpenObjectChar, depth);
this.AppendLine();
// Iterate through the elements and output recursively
Int32 writeCount = 0;
foreach(Object? key in items.Keys) {
// Serialize and append the key (first char indented)
this.Append(StringQuotedChar, depth + 1);
Escape(key?.ToString()!, this._builder!);
_ = this._builder?.Append(StringQuotedChar).Append(ValueSeparatorChar).Append(" ");
// Serialize and append the value
String serializedValue = Serialize(items[key!], depth + 1, this._options!);
if(IsNonEmptyJsonArrayOrObject(serializedValue)) {
this.AppendLine();
}
this.Append(serializedValue, 0);
// Add a comma and start a new line -- We will remove the last one when we are done writing the elements
this.Append(FieldSeparatorChar, 0);
this.AppendLine();
writeCount++;
}
// Output the end of the object and set the result
this.RemoveLastComma();
this.Append(CloseObjectChar, writeCount > 0 ? depth : 0);
return this._builder!.ToString();
}
private String ResolveObject(Object target, Int32 depth) {
Type targetType = target.GetType();
if(targetType.IsEnum) {
return Convert.ToInt64(target, System.Globalization.CultureInfo.InvariantCulture).ToString();
}
global::System.Collections.Generic.Dictionary<global::System.String, global::System.Reflection.MemberInfo> fields = this._options!.GetProperties(targetType);
if(fields.Count == 0 && String.IsNullOrWhiteSpace(this._options.TypeSpecifier)) {
return EmptyObjectLiteral;
}
// If we arrive here, then we convert the object into a
// dictionary of property names and values and call the serialization
// function again
global::System.Collections.Generic.Dictionary<global::System.String, global::System.Object?> objectDictionary = this.CreateDictionary(fields, targetType.ToString(), target);
return Serialize(objectDictionary, depth, this._options);
}
private String ResolveEnumerable(IEnumerable target, Int32 depth) {
// Cast the items as a generic object array
global::System.Collections.Generic.IEnumerable<global::System.Object> items = target.Cast<global::System.Object>();
this.Append(OpenArrayChar, depth);
this.AppendLine();
// Iterate through the elements and output recursively
Int32 writeCount = 0;
foreach(Object entry in items) {
String serializedValue = Serialize(entry, depth + 1, this._options!);
if(IsNonEmptyJsonArrayOrObject(serializedValue)) {
this.Append(serializedValue, 0);
} else {
this.Append(serializedValue, depth + 1);
}
this.Append(FieldSeparatorChar, 0);
this.AppendLine();
writeCount++;
}
// Output the end of the array and set the result
this.RemoveLastComma();
this.Append(CloseArrayChar, writeCount > 0 ? depth : 0);
return this._builder!.ToString();
}
private void SetIndent(Int32 depth) {
if(this._options!.Format == false || depth <= 0) {
return;
}
_ = this._builder!.Append(IndentStrings.GetOrAdd(depth, x => new String(' ', x * 4)));
}
/// <summary>
/// Removes the last comma in the current string builder.
/// </summary>
private void RemoveLastComma() {
if(this._builder!.Length < this._lastCommaSearch!.Length) {
return;
}
if(this._lastCommaSearch.Where((t, i) => this._builder[this._builder.Length - this._lastCommaSearch.Length + i] != t).Any()) {
return;
}
// If we got this far, we simply remove the comma character
_ = this._builder.Remove(this._builder.Length - this._lastCommaSearch.Length, 1);
}
private void Append(String text, Int32 depth) {
this.SetIndent(depth);
_ = this._builder!.Append(text);
}
private void Append(Char text, Int32 depth) {
this.SetIndent(depth);
_ = this._builder!.Append(text);
}
private void AppendLine() {
if(this._options!.Format == false) {
return;
}
_ = this._builder!.Append(Environment.NewLine);
}
#endregion
}
}
}

View File

@ -1,144 +1,133 @@
using System;
#nullable enable
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using Swan.Reflection;
namespace Swan.Formatters
{
namespace Swan.Formatters {
/// <summary>
/// A very simple, light-weight JSON library written by Mario
/// to teach Geo how things are done
///
/// This is an useful helper for small tasks but it doesn't represent a full-featured
/// serializer such as the beloved Json.NET.
/// </summary>
public class SerializerOptions {
private static readonly ConcurrentDictionary<Type, Dictionary<Tuple<String, String>, MemberInfo>>
TypeCache = new ConcurrentDictionary<Type, Dictionary<Tuple<String, String>, MemberInfo>>();
private readonly String[]? _includeProperties;
private readonly String[]? _excludeProperties;
private readonly Dictionary<Int32, List<WeakReference>> _parentReferences = new Dictionary<Int32, List<WeakReference>>();
/// <summary>
/// A very simple, light-weight JSON library written by Mario
/// to teach Geo how things are done
///
/// This is an useful helper for small tasks but it doesn't represent a full-featured
/// serializer such as the beloved Json.NET.
/// Initializes a new instance of the <see cref="SerializerOptions"/> class.
/// </summary>
public class SerializerOptions
{
private static readonly ConcurrentDictionary<Type, Dictionary<Tuple<string, string>, MemberInfo>>
TypeCache = new ConcurrentDictionary<Type, Dictionary<Tuple<string, string>, MemberInfo>>();
private readonly string[]? _includeProperties;
private readonly string[]? _excludeProperties;
private readonly Dictionary<int, List<WeakReference>> _parentReferences = new Dictionary<int, List<WeakReference>>();
/// <summary>
/// Initializes a new instance of the <see cref="SerializerOptions"/> class.
/// </summary>
/// <param name="format">if set to <c>true</c> [format].</param>
/// <param name="typeSpecifier">The type specifier.</param>
/// <param name="includeProperties">The include properties.</param>
/// <param name="excludeProperties">The exclude properties.</param>
/// <param name="includeNonPublic">if set to <c>true</c> [include non public].</param>
/// <param name="parentReferences">The parent references.</param>
/// <param name="jsonSerializerCase">The json serializer case.</param>
public SerializerOptions(
bool format,
string? typeSpecifier,
string[]? includeProperties,
string[]? excludeProperties = null,
bool includeNonPublic = true,
IReadOnlyCollection<WeakReference>? parentReferences = null,
JsonSerializerCase jsonSerializerCase = JsonSerializerCase.None)
{
_includeProperties = includeProperties;
_excludeProperties = excludeProperties;
IncludeNonPublic = includeNonPublic;
Format = format;
TypeSpecifier = typeSpecifier;
JsonSerializerCase = jsonSerializerCase;
if (parentReferences == null)
return;
foreach (var parentReference in parentReferences.Where(x => x.IsAlive))
{
IsObjectPresent(parentReference.Target);
}
}
/// <summary>
/// Gets a value indicating whether this <see cref="SerializerOptions"/> is format.
/// </summary>
/// <value>
/// <c>true</c> if format; otherwise, <c>false</c>.
/// </value>
public bool Format { get; }
/// <summary>
/// Gets the type specifier.
/// </summary>
/// <value>
/// The type specifier.
/// </value>
public string? TypeSpecifier { get; }
/// <summary>
/// Gets a value indicating whether [include non public].
/// </summary>
/// <value>
/// <c>true</c> if [include non public]; otherwise, <c>false</c>.
/// </value>
public bool IncludeNonPublic { get; }
/// <summary>
/// Gets the json serializer case.
/// </summary>
/// <value>
/// The json serializer case.
/// </value>
public JsonSerializerCase JsonSerializerCase { get; }
internal bool IsObjectPresent(object target)
{
var hashCode = target.GetHashCode();
if (_parentReferences.ContainsKey(hashCode))
{
if (_parentReferences[hashCode].Any(p => ReferenceEquals(p.Target, target)))
return true;
_parentReferences[hashCode].Add(new WeakReference(target));
return false;
}
_parentReferences.Add(hashCode, new List<WeakReference> { new WeakReference(target) });
return false;
}
internal Dictionary<string, MemberInfo> GetProperties(Type targetType)
=> GetPropertiesCache(targetType)
.When(() => _includeProperties?.Length > 0,
query => query.Where(p => _includeProperties.Contains(p.Key.Item1)))
.When(() => _excludeProperties?.Length > 0,
query => query.Where(p => !_excludeProperties.Contains(p.Key.Item1)))
.ToDictionary(x => x.Key.Item2, x => x.Value);
private Dictionary<Tuple<string, string>, MemberInfo> GetPropertiesCache(Type targetType)
{
if (TypeCache.TryGetValue(targetType, out var current))
return current;
var fields =
new List<MemberInfo>(PropertyTypeCache.DefaultCache.Value.RetrieveAllProperties(targetType).Where(p => p.CanRead));
// If the target is a struct (value type) navigate the fields.
if (targetType.IsValueType)
{
fields.AddRange(FieldTypeCache.DefaultCache.Value.RetrieveAllFields(targetType));
}
var value = fields
.ToDictionary(
x => Tuple.Create(x.Name,
x.GetCustomAttribute<JsonPropertyAttribute>()?.PropertyName ?? x.Name.GetNameWithCase(JsonSerializerCase)),
x => x);
TypeCache.TryAdd(targetType, value);
return value;
}
}
/// <param name="format">if set to <c>true</c> [format].</param>
/// <param name="typeSpecifier">The type specifier.</param>
/// <param name="includeProperties">The include properties.</param>
/// <param name="excludeProperties">The exclude properties.</param>
/// <param name="includeNonPublic">if set to <c>true</c> [include non public].</param>
/// <param name="parentReferences">The parent references.</param>
/// <param name="jsonSerializerCase">The json serializer case.</param>
public SerializerOptions(Boolean format, String? typeSpecifier, String[]? includeProperties, String[]? excludeProperties = null, Boolean includeNonPublic = true, IReadOnlyCollection<WeakReference>? parentReferences = null, JsonSerializerCase jsonSerializerCase = JsonSerializerCase.None) {
this._includeProperties = includeProperties;
this._excludeProperties = excludeProperties;
this.IncludeNonPublic = includeNonPublic;
this.Format = format;
this.TypeSpecifier = typeSpecifier;
this.JsonSerializerCase = jsonSerializerCase;
if(parentReferences == null) {
return;
}
foreach(WeakReference parentReference in parentReferences.Where(x => x.IsAlive)) {
_ = this.IsObjectPresent(parentReference.Target);
}
}
/// <summary>
/// Gets a value indicating whether this <see cref="SerializerOptions"/> is format.
/// </summary>
/// <value>
/// <c>true</c> if format; otherwise, <c>false</c>.
/// </value>
public Boolean Format {
get;
}
/// <summary>
/// Gets the type specifier.
/// </summary>
/// <value>
/// The type specifier.
/// </value>
public String? TypeSpecifier {
get;
}
/// <summary>
/// Gets a value indicating whether [include non public].
/// </summary>
/// <value>
/// <c>true</c> if [include non public]; otherwise, <c>false</c>.
/// </value>
public Boolean IncludeNonPublic {
get;
}
/// <summary>
/// Gets the json serializer case.
/// </summary>
/// <value>
/// The json serializer case.
/// </value>
public JsonSerializerCase JsonSerializerCase {
get;
}
internal Boolean IsObjectPresent(Object? target) {
if(target == null) {
return false;
}
Int32 hashCode = target.GetHashCode();
if(this._parentReferences.ContainsKey(hashCode)) {
if(this._parentReferences[hashCode].Any(p => ReferenceEquals(p.Target, target))) {
return true;
}
this._parentReferences[hashCode].Add(new WeakReference(target));
return false;
}
this._parentReferences.Add(hashCode, new List<WeakReference> { new WeakReference(target) });
return false;
}
internal Dictionary<String, MemberInfo> GetProperties(Type targetType) => this.GetPropertiesCache(targetType).When(() => this._includeProperties?.Length > 0, query => query.Where(p => this._includeProperties.Contains(p.Key.Item1))).When(() => this._excludeProperties?.Length > 0, query => query.Where(p => !this._excludeProperties.Contains(p.Key.Item1))).ToDictionary(x => x.Key.Item2, x => x.Value);
private Dictionary<Tuple<String, String>, MemberInfo> GetPropertiesCache(Type targetType) {
if(TypeCache.TryGetValue(targetType, out Dictionary<Tuple<String, String>, MemberInfo>? current)) {
return current;
}
List<MemberInfo> fields = new List<MemberInfo>(PropertyTypeCache.DefaultCache.Value.RetrieveAllProperties(targetType).Where(p => p.CanRead));
// If the target is a struct (value type) navigate the fields.
if(targetType.IsValueType) {
fields.AddRange(FieldTypeCache.DefaultCache.Value.RetrieveAllFields(targetType));
}
Dictionary<Tuple<String, String>, MemberInfo> value = fields.ToDictionary(x => Tuple.Create(x.Name, x.GetCustomAttribute<JsonPropertyAttribute>()?.PropertyName ?? x.Name.GetNameWithCase(this.JsonSerializerCase)), x => x);
TypeCache.TryAdd(targetType, value);
return value;
}
}
}

View File

@ -1,379 +1,340 @@
using System;
#nullable enable
using System;
using System.Collections.Generic;
using System.Linq;
using Swan.Collections;
using Swan.Reflection;
namespace Swan.Formatters
{
namespace Swan.Formatters {
/// <summary>
/// A very simple, light-weight JSON library written by Mario
/// to teach Geo how things are done
///
/// This is an useful helper for small tasks but it doesn't represent a full-featured
/// serializer such as the beloved Json.NET.
/// </summary>
public static partial class Json {
#region Constants
internal const String AddMethodName = "Add";
private const Char OpenObjectChar = '{';
private const Char CloseObjectChar = '}';
private const Char OpenArrayChar = '[';
private const Char CloseArrayChar = ']';
private const Char FieldSeparatorChar = ',';
private const Char ValueSeparatorChar = ':';
private const Char StringEscapeChar = '\\';
private const Char StringQuotedChar = '"';
private const String EmptyObjectLiteral = "{ }";
private const String EmptyArrayLiteral = "[ ]";
private const String TrueLiteral = "true";
private const String FalseLiteral = "false";
private const String NullLiteral = "null";
#endregion
private static readonly CollectionCacheRepository<String> IgnoredPropertiesCache = new CollectionCacheRepository<global::System.String>();
#region Public API
/// <summary>
/// A very simple, light-weight JSON library written by Mario
/// to teach Geo how things are done
///
/// This is an useful helper for small tasks but it doesn't represent a full-featured
/// serializer such as the beloved Json.NET.
/// Serializes the specified object into a JSON string.
/// </summary>
public static partial class Json
{
#region Constants
internal const string AddMethodName = "Add";
private const char OpenObjectChar = '{';
private const char CloseObjectChar = '}';
private const char OpenArrayChar = '[';
private const char CloseArrayChar = ']';
private const char FieldSeparatorChar = ',';
private const char ValueSeparatorChar = ':';
private const char StringEscapeChar = '\\';
private const char StringQuotedChar = '"';
private const string EmptyObjectLiteral = "{ }";
private const string EmptyArrayLiteral = "[ ]";
private const string TrueLiteral = "true";
private const string FalseLiteral = "false";
private const string NullLiteral = "null";
#endregion
private static readonly CollectionCacheRepository<string> IgnoredPropertiesCache = new CollectionCacheRepository<string>();
#region Public API
/// <summary>
/// Serializes the specified object into a JSON string.
/// </summary>
/// <param name="obj">The object.</param>
/// <param name="format">if set to <c>true</c> it formats and indents the output.</param>
/// <param name="typeSpecifier">The type specifier. Leave null or empty to avoid setting.</param>
/// <param name="includeNonPublic">if set to <c>true</c> non-public getters will be also read.</param>
/// <param name="includedNames">The included property names.</param>
/// <param name="excludedNames">The excluded property names.</param>
/// <returns>
/// A <see cref="System.String" /> that represents the current object.
/// </returns>
/// <example>
/// The following example describes how to serialize a simple object.
/// <code>
/// using Swan.Formatters;
///
/// class Example
/// {
/// static void Main()
/// {
/// var obj = new { One = "One", Two = "Two" };
///
/// var serial = Json.Serialize(obj); // {"One": "One","Two": "Two"}
/// }
/// }
/// </code>
///
/// The following example details how to serialize an object using the <see cref="JsonPropertyAttribute"/>.
///
/// <code>
/// using Swan.Attributes;
/// using Swan.Formatters;
///
/// class Example
/// {
/// class JsonPropertyExample
/// {
/// [JsonProperty("data")]
/// public string Data { get; set; }
///
/// [JsonProperty("ignoredData", true)]
/// public string IgnoredData { get; set; }
/// }
///
/// static void Main()
/// {
/// var obj = new JsonPropertyExample() { Data = "OK", IgnoredData = "OK" };
///
/// // {"data": "OK"}
/// var serializedObj = Json.Serialize(obj);
/// }
/// }
/// </code>
/// </example>
public static string Serialize(
object? obj,
bool format = false,
string? typeSpecifier = null,
bool includeNonPublic = false,
string[]? includedNames = null,
params string[] excludedNames)
{
return Serialize(obj, format, typeSpecifier, includeNonPublic, includedNames, excludedNames, null, JsonSerializerCase.None);
}
/// <summary>
/// Serializes the specified object into a JSON string.
/// </summary>
/// <param name="obj">The object.</param>
/// <param name="jsonSerializerCase">The json serializer case.</param>
/// <param name="format">if set to <c>true</c> [format].</param>
/// <param name="typeSpecifier">The type specifier.</param>
/// <returns>
/// A <see cref="System.String" /> that represents the current object.
/// </returns>
public static string Serialize(
object? obj,
JsonSerializerCase jsonSerializerCase,
bool format = false,
string? typeSpecifier = null) => Serialize(obj, format, typeSpecifier, false, null, null, null, jsonSerializerCase);
/// <summary>
/// Serializes the specified object into a JSON string.
/// </summary>
/// <param name="obj">The object.</param>
/// <param name="format">if set to <c>true</c> it formats and indents the output.</param>
/// <param name="typeSpecifier">The type specifier. Leave null or empty to avoid setting.</param>
/// <param name="includeNonPublic">if set to <c>true</c> non-public getters will be also read.</param>
/// <param name="includedNames">The included property names.</param>
/// <param name="excludedNames">The excluded property names.</param>
/// <param name="parentReferences">The parent references.</param>
/// <param name="jsonSerializerCase">The json serializer case.</param>
/// <returns>
/// A <see cref="System.String" /> that represents the current object.
/// </returns>
public static string Serialize(
object? obj,
bool format,
string? typeSpecifier,
bool includeNonPublic,
string[]? includedNames,
string[]? excludedNames,
List<WeakReference>? parentReferences,
JsonSerializerCase jsonSerializerCase)
{
if (obj != null && (obj is string || Definitions.AllBasicValueTypes.Contains(obj.GetType())))
{
return SerializePrimitiveValue(obj);
}
var options = new SerializerOptions(
format,
typeSpecifier,
includedNames,
GetExcludedNames(obj?.GetType(), excludedNames),
includeNonPublic,
parentReferences,
jsonSerializerCase);
return Serialize(obj, options);
}
/// <summary>
/// Serializes the specified object using the SerializerOptions provided.
/// </summary>
/// <param name="obj">The object.</param>
/// <param name="options">The options.</param>
/// <returns>
/// A <see cref="string" /> that represents the current object.
/// </returns>
public static string Serialize(object? obj, SerializerOptions options) => Serializer.Serialize(obj, 0, options);
/// <summary>
/// Serializes the specified object only including the specified property names.
/// </summary>
/// <param name="obj">The object.</param>
/// <param name="format">if set to <c>true</c> it formats and indents the output.</param>
/// <param name="includeNames">The include names.</param>
/// <returns>A <see cref="string" /> that represents the current object.</returns>
/// <example>
/// The following example shows how to serialize a simple object including the specified properties.
/// <code>
/// using Swan.Formatters;
///
/// class Example
/// {
/// static void Main()
/// {
/// // object to serialize
/// var obj = new { One = "One", Two = "Two", Three = "Three" };
///
/// // the included names
/// var includedNames = new[] { "Two", "Three" };
///
/// // serialize only the included names
/// var data = Json.SerializeOnly(basicObject, true, includedNames);
/// // {"Two": "Two","Three": "Three" }
/// }
/// }
/// </code>
/// </example>
public static string SerializeOnly(object? obj, bool format, params string[] includeNames)
=> Serialize(obj, new SerializerOptions(format, null, includeNames));
/// <summary>
/// Serializes the specified object excluding the specified property names.
/// </summary>
/// <param name="obj">The object.</param>
/// <param name="format">if set to <c>true</c> it formats and indents the output.</param>
/// <param name="excludeNames">The exclude names.</param>
/// <returns>A <see cref="string" /> that represents the current object.</returns>
/// <example>
/// The following code shows how to serialize a simple object excluding the specified properties.
/// <code>
/// using Swan.Formatters;
///
/// class Example
/// {
/// static void Main()
/// {
/// // object to serialize
/// var obj = new { One = "One", Two = "Two", Three = "Three" };
///
/// // the excluded names
/// var excludeNames = new[] { "Two", "Three" };
///
/// // serialize excluding
/// var data = Json.SerializeExcluding(basicObject, false, includedNames);
/// // {"One": "One"}
/// }
/// }
/// </code>
/// </example>
public static string SerializeExcluding(object? obj, bool format, params string[] excludeNames)
=> Serialize(obj, new SerializerOptions(format, null, null, excludeNames));
/// <summary>
/// Deserializes the specified json string as either a Dictionary[string, object] or as a List[object]
/// depending on the syntax of the JSON string.
/// </summary>
/// <param name="json">The JSON string.</param>
/// <param name="jsonSerializerCase">The json serializer case.</param>
/// <returns>
/// Type of the current deserializes.
/// </returns>
/// <example>
/// The following code shows how to deserialize a JSON string into a Dictionary.
/// <code>
/// using Swan.Formatters;
/// class Example
/// {
/// static void Main()
/// {
/// // json to deserialize
/// var basicJson = "{\"One\":\"One\",\"Two\":\"Two\",\"Three\":\"Three\"}";
/// // deserializes the specified json into a Dictionary&lt;string, object&gt;.
/// var data = Json.Deserialize(basicJson, JsonSerializerCase.None);
/// }
/// }
/// </code></example>
public static object? Deserialize(
string json,
JsonSerializerCase jsonSerializerCase)
=> Converter.FromJsonResult(Deserializer.DeserializeInternal(json), jsonSerializerCase);
/// <summary>
/// Deserializes the specified json string as either a Dictionary[string, object] or as a List[object]
/// depending on the syntax of the JSON string.
/// </summary>
/// <param name="json">The JSON string.</param>
/// <returns>
/// Type of the current deserializes.
/// </returns>
/// <example>
/// The following code shows how to deserialize a JSON string into a Dictionary.
/// <code>
/// using Swan.Formatters;
/// class Example
/// {
/// static void Main()
/// {
/// // json to deserialize
/// var basicJson = "{\"One\":\"One\",\"Two\":\"Two\",\"Three\":\"Three\"}";
/// // deserializes the specified json into a Dictionary&lt;string, object&gt;.
/// var data = Json.Deserialize(basicJson);
/// }
/// }
/// </code></example>
public static object? Deserialize(string json)
=> Deserialize(json, JsonSerializerCase.None);
/// <summary>
/// Deserializes the specified JSON string and converts it to the specified object type.
/// Non-public constructors and property setters are ignored.
/// </summary>
/// <typeparam name="T">The type of object to deserialize.</typeparam>
/// <param name="json">The JSON string.</param>
/// <param name="jsonSerializerCase">The JSON serializer case.</param>
/// <returns>
/// The deserialized specified type object.
/// </returns>
/// <example>
/// The following code describes how to deserialize a JSON string into an object of type T.
/// <code>
/// using Swan.Formatters;
/// class Example
/// {
/// static void Main()
/// {
/// // json type BasicJson to serialize
/// var basicJson = "{\"One\":\"One\",\"Two\":\"Two\",\"Three\":\"Three\"}";
/// // deserializes the specified string in a new instance of the type BasicJson.
/// var data = Json.Deserialize&lt;BasicJson&gt;(basicJson);
/// }
/// }
/// </code></example>
public static T Deserialize<T>(string json, JsonSerializerCase jsonSerializerCase = JsonSerializerCase.None)
=> (T)Deserialize(json, typeof(T), jsonSerializerCase: jsonSerializerCase);
/// <summary>
/// Deserializes the specified JSON string and converts it to the specified object type.
/// </summary>
/// <typeparam name="T">The type of object to deserialize.</typeparam>
/// <param name="json">The JSON string.</param>
/// <param name="includeNonPublic">if set to true, it also uses the non-public constructors and property setters.</param>
/// <returns>The deserialized specified type object.</returns>
public static T Deserialize<T>(string json, bool includeNonPublic) => (T)Deserialize(json, typeof(T), includeNonPublic);
/// <summary>
/// Deserializes the specified JSON string and converts it to the specified object type.
/// </summary>
/// <param name="json">The JSON string.</param>
/// <param name="resultType">Type of the result.</param>
/// <param name="includeNonPublic">if set to true, it also uses the non-public constructors and property setters.</param>
/// <param name="jsonSerializerCase">The json serializer case.</param>
/// <returns>
/// Type of the current conversion from json result.
/// </returns>
public static object? Deserialize(string json, Type resultType, bool includeNonPublic = false, JsonSerializerCase jsonSerializerCase = JsonSerializerCase.None)
=> Converter.FromJsonResult(Deserializer.DeserializeInternal(json), jsonSerializerCase, resultType, includeNonPublic);
#endregion
#region Private API
private static string[]? GetExcludedNames(Type? type, string[]? excludedNames)
{
if (type == null)
return excludedNames;
var excludedByAttr = IgnoredPropertiesCache.Retrieve(type, t => t.GetProperties()
/// <param name="obj">The object.</param>
/// <param name="format">if set to <c>true</c> it formats and indents the output.</param>
/// <param name="typeSpecifier">The type specifier. Leave null or empty to avoid setting.</param>
/// <param name="includeNonPublic">if set to <c>true</c> non-public getters will be also read.</param>
/// <param name="includedNames">The included property names.</param>
/// <param name="excludedNames">The excluded property names.</param>
/// <returns>
/// A <see cref="System.String" /> that represents the current object.
/// </returns>
/// <example>
/// The following example describes how to serialize a simple object.
/// <code>
/// using Swan.Formatters;
///
/// class Example
/// {
/// static void Main()
/// {
/// var obj = new { One = "One", Two = "Two" };
///
/// var serial = Json.Serialize(obj); // {"One": "One","Two": "Two"}
/// }
/// }
/// </code>
///
/// The following example details how to serialize an object using the <see cref="JsonPropertyAttribute"/>.
///
/// <code>
/// using Swan.Attributes;
/// using Swan.Formatters;
///
/// class Example
/// {
/// class JsonPropertyExample
/// {
/// [JsonProperty("data")]
/// public string Data { get; set; }
///
/// [JsonProperty("ignoredData", true)]
/// public string IgnoredData { get; set; }
/// }
///
/// static void Main()
/// {
/// var obj = new JsonPropertyExample() { Data = "OK", IgnoredData = "OK" };
///
/// // {"data": "OK"}
/// var serializedObj = Json.Serialize(obj);
/// }
/// }
/// </code>
/// </example>
public static String Serialize(Object? obj, Boolean format = false, String? typeSpecifier = null, Boolean includeNonPublic = false, String[]? includedNames = null, params String[] excludedNames) => Serialize(obj, format, typeSpecifier, includeNonPublic, includedNames, excludedNames, null, JsonSerializerCase.None);
/// <summary>
/// Serializes the specified object into a JSON string.
/// </summary>
/// <param name="obj">The object.</param>
/// <param name="jsonSerializerCase">The json serializer case.</param>
/// <param name="format">if set to <c>true</c> [format].</param>
/// <param name="typeSpecifier">The type specifier.</param>
/// <returns>
/// A <see cref="System.String" /> that represents the current object.
/// </returns>
public static String Serialize(Object? obj, JsonSerializerCase jsonSerializerCase, Boolean format = false, String? typeSpecifier = null) => Serialize(obj, format, typeSpecifier, false, null, null, null, jsonSerializerCase);
/// <summary>
/// Serializes the specified object into a JSON string.
/// </summary>
/// <param name="obj">The object.</param>
/// <param name="format">if set to <c>true</c> it formats and indents the output.</param>
/// <param name="typeSpecifier">The type specifier. Leave null or empty to avoid setting.</param>
/// <param name="includeNonPublic">if set to <c>true</c> non-public getters will be also read.</param>
/// <param name="includedNames">The included property names.</param>
/// <param name="excludedNames">The excluded property names.</param>
/// <param name="parentReferences">The parent references.</param>
/// <param name="jsonSerializerCase">The json serializer case.</param>
/// <returns>
/// A <see cref="System.String" /> that represents the current object.
/// </returns>
public static String Serialize(Object? obj, Boolean format, String? typeSpecifier, Boolean includeNonPublic, String[]? includedNames, String[]? excludedNames, List<WeakReference>? parentReferences, JsonSerializerCase jsonSerializerCase) {
if(obj != null && (obj is String || Definitions.AllBasicValueTypes.Contains(obj.GetType()))) {
return SerializePrimitiveValue(obj);
}
SerializerOptions options = new SerializerOptions(format, typeSpecifier, includedNames, GetExcludedNames(obj?.GetType(), excludedNames), includeNonPublic, parentReferences, jsonSerializerCase);
return Serialize(obj, options);
}
/// <summary>
/// Serializes the specified object using the SerializerOptions provided.
/// </summary>
/// <param name="obj">The object.</param>
/// <param name="options">The options.</param>
/// <returns>
/// A <see cref="String" /> that represents the current object.
/// </returns>
public static String Serialize(Object? obj, SerializerOptions options) => Serializer.Serialize(obj, 0, options);
/// <summary>
/// Serializes the specified object only including the specified property names.
/// </summary>
/// <param name="obj">The object.</param>
/// <param name="format">if set to <c>true</c> it formats and indents the output.</param>
/// <param name="includeNames">The include names.</param>
/// <returns>A <see cref="String" /> that represents the current object.</returns>
/// <example>
/// The following example shows how to serialize a simple object including the specified properties.
/// <code>
/// using Swan.Formatters;
///
/// class Example
/// {
/// static void Main()
/// {
/// // object to serialize
/// var obj = new { One = "One", Two = "Two", Three = "Three" };
///
/// // the included names
/// var includedNames = new[] { "Two", "Three" };
///
/// // serialize only the included names
/// var data = Json.SerializeOnly(basicObject, true, includedNames);
/// // {"Two": "Two","Three": "Three" }
/// }
/// }
/// </code>
/// </example>
public static String SerializeOnly(Object? obj, Boolean format, params String[] includeNames) => Serialize(obj, new SerializerOptions(format, null, includeNames));
/// <summary>
/// Serializes the specified object excluding the specified property names.
/// </summary>
/// <param name="obj">The object.</param>
/// <param name="format">if set to <c>true</c> it formats and indents the output.</param>
/// <param name="excludeNames">The exclude names.</param>
/// <returns>A <see cref="String" /> that represents the current object.</returns>
/// <example>
/// The following code shows how to serialize a simple object excluding the specified properties.
/// <code>
/// using Swan.Formatters;
///
/// class Example
/// {
/// static void Main()
/// {
/// // object to serialize
/// var obj = new { One = "One", Two = "Two", Three = "Three" };
///
/// // the excluded names
/// var excludeNames = new[] { "Two", "Three" };
///
/// // serialize excluding
/// var data = Json.SerializeExcluding(basicObject, false, includedNames);
/// // {"One": "One"}
/// }
/// }
/// </code>
/// </example>
public static String SerializeExcluding(Object? obj, Boolean format, params String[] excludeNames) => Serialize(obj, new SerializerOptions(format, null, null, excludeNames));
/// <summary>
/// Deserializes the specified json string as either a Dictionary[string, object] or as a List[object]
/// depending on the syntax of the JSON string.
/// </summary>
/// <param name="json">The JSON string.</param>
/// <param name="jsonSerializerCase">The json serializer case.</param>
/// <returns>
/// Type of the current deserializes.
/// </returns>
/// <example>
/// The following code shows how to deserialize a JSON string into a Dictionary.
/// <code>
/// using Swan.Formatters;
/// class Example
/// {
/// static void Main()
/// {
/// // json to deserialize
/// var basicJson = "{\"One\":\"One\",\"Two\":\"Two\",\"Three\":\"Three\"}";
/// // deserializes the specified json into a Dictionary&lt;string, object&gt;.
/// var data = Json.Deserialize(basicJson, JsonSerializerCase.None);
/// }
/// }
/// </code></example>
public static Object? Deserialize(String? json, JsonSerializerCase jsonSerializerCase) => Converter.FromJsonResult(Deserializer.DeserializeInternal(json), jsonSerializerCase);
/// <summary>
/// Deserializes the specified json string as either a Dictionary[string, object] or as a List[object]
/// depending on the syntax of the JSON string.
/// </summary>
/// <param name="json">The JSON string.</param>
/// <returns>
/// Type of the current deserializes.
/// </returns>
/// <example>
/// The following code shows how to deserialize a JSON string into a Dictionary.
/// <code>
/// using Swan.Formatters;
/// class Example
/// {
/// static void Main()
/// {
/// // json to deserialize
/// var basicJson = "{\"One\":\"One\",\"Two\":\"Two\",\"Three\":\"Three\"}";
/// // deserializes the specified json into a Dictionary&lt;string, object&gt;.
/// var data = Json.Deserialize(basicJson);
/// }
/// }
/// </code></example>
public static Object? Deserialize(String? json) => Deserialize(json, JsonSerializerCase.None);
/// <summary>
/// Deserializes the specified JSON string and converts it to the specified object type.
/// Non-public constructors and property setters are ignored.
/// </summary>
/// <typeparam name="T">The type of object to deserialize.</typeparam>
/// <param name="json">The JSON string.</param>
/// <param name="jsonSerializerCase">The JSON serializer case.</param>
/// <returns>
/// The deserialized specified type object.
/// </returns>
/// <example>
/// The following code describes how to deserialize a JSON string into an object of type T.
/// <code>
/// using Swan.Formatters;
/// class Example
/// {
/// static void Main()
/// {
/// // json type BasicJson to serialize
/// var basicJson = "{\"One\":\"One\",\"Two\":\"Two\",\"Three\":\"Three\"}";
/// // deserializes the specified string in a new instance of the type BasicJson.
/// var data = Json.Deserialize&lt;BasicJson&gt;(basicJson);
/// }
/// }
/// </code></example>
public static T Deserialize<T>(String json, JsonSerializerCase jsonSerializerCase = JsonSerializerCase.None) where T : notnull => (T)Deserialize(json, typeof(T), jsonSerializerCase: jsonSerializerCase)!;
/// <summary>
/// Deserializes the specified JSON string and converts it to the specified object type.
/// </summary>
/// <typeparam name="T">The type of object to deserialize.</typeparam>
/// <param name="json">The JSON string.</param>
/// <param name="includeNonPublic">if set to true, it also uses the non-public constructors and property setters.</param>
/// <returns>The deserialized specified type object.</returns>
public static T Deserialize<T>(String json, Boolean includeNonPublic) where T : notnull => (T)Deserialize(json, typeof(T), includeNonPublic)!;
/// <summary>
/// Deserializes the specified JSON string and converts it to the specified object type.
/// </summary>
/// <param name="json">The JSON string.</param>
/// <param name="resultType">Type of the result.</param>
/// <param name="includeNonPublic">if set to true, it also uses the non-public constructors and property setters.</param>
/// <param name="jsonSerializerCase">The json serializer case.</param>
/// <returns>
/// Type of the current conversion from json result.
/// </returns>
public static Object? Deserialize(String json, Type resultType, Boolean includeNonPublic = false, JsonSerializerCase jsonSerializerCase = JsonSerializerCase.None) => Converter.FromJsonResult(Deserializer.DeserializeInternal(json), jsonSerializerCase, resultType, includeNonPublic);
#endregion
#region Private API
private static String[]? GetExcludedNames(Type? type, String[]? excludedNames) {
if(type == null) {
return excludedNames;
}
global::System.Collections.Generic.IEnumerable<global::System.String> excludedByAttr = IgnoredPropertiesCache.Retrieve(type, t => t.GetProperties()
.Where(x => AttributeCache.DefaultCache.Value.RetrieveOne<JsonPropertyAttribute>(x)?.Ignored == true)
.Select(x => x.Name));
if (excludedByAttr?.Any() != true)
return excludedNames;
return excludedNames?.Any(string.IsNullOrWhiteSpace) == true
? excludedByAttr.Intersect(excludedNames.Where(y => !string.IsNullOrWhiteSpace(y))).ToArray()
: excludedByAttr.ToArray();
}
private static string SerializePrimitiveValue(object obj) =>
obj switch
{
string stringValue => stringValue,
bool boolValue => boolValue ? TrueLiteral : FalseLiteral,
_ => obj.ToString()
};
#endregion
}
.Select(x => x.Name));
if(excludedByAttr?.Any() != true) {
return excludedNames;
}
return excludedNames?.Any(String.IsNullOrWhiteSpace) == true
? excludedByAttr.Intersect(excludedNames.Where(y => !String.IsNullOrWhiteSpace(y))).ToArray()
: excludedByAttr.ToArray();
}
private static String SerializePrimitiveValue(Object obj) => obj switch
{
String stringValue => stringValue,
Boolean boolValue => boolValue ? TrueLiteral : FalseLiteral,
_ => obj.ToString()!
};
#endregion
}
}

View File

@ -1,39 +1,40 @@
using System;
namespace Swan.Formatters
{
namespace Swan.Formatters {
/// <summary>
/// An attribute used to help setup a property behavior when serialize/deserialize JSON.
/// </summary>
/// <seealso cref="Attribute" />
[AttributeUsage(AttributeTargets.Property)]
public sealed class JsonPropertyAttribute : Attribute {
/// <summary>
/// An attribute used to help setup a property behavior when serialize/deserialize JSON.
/// Initializes a new instance of the <see cref="JsonPropertyAttribute" /> class.
/// </summary>
/// <seealso cref="Attribute" />
[AttributeUsage(AttributeTargets.Property)]
public sealed class JsonPropertyAttribute : Attribute
{
/// <summary>
/// Initializes a new instance of the <see cref="JsonPropertyAttribute" /> class.
/// </summary>
/// <param name="propertyName">Name of the property.</param>
/// <param name="ignored">if set to <c>true</c> [ignored].</param>
public JsonPropertyAttribute(string propertyName, bool ignored = false)
{
PropertyName = propertyName ?? throw new ArgumentNullException(nameof(propertyName));
Ignored = ignored;
}
/// <summary>
/// Gets or sets the name of the property.
/// </summary>
/// <value>
/// The name of the property.
/// </value>
public string PropertyName { get; }
/// <summary>
/// Gets or sets a value indicating whether this <see cref="JsonPropertyAttribute" /> is ignored.
/// </summary>
/// <value>
/// <c>true</c> if ignored; otherwise, <c>false</c>.
/// </value>
public bool Ignored { get; }
}
/// <param name="propertyName">Name of the property.</param>
/// <param name="ignored">if set to <c>true</c> [ignored].</param>
public JsonPropertyAttribute(String propertyName, Boolean ignored = false) {
this.PropertyName = propertyName ?? throw new ArgumentNullException(nameof(propertyName));
this.Ignored = ignored;
}
/// <summary>
/// Gets or sets the name of the property.
/// </summary>
/// <value>
/// The name of the property.
/// </value>
public String PropertyName {
get;
}
/// <summary>
/// Gets or sets a value indicating whether this <see cref="JsonPropertyAttribute" /> is ignored.
/// </summary>
/// <value>
/// <c>true</c> if ignored; otherwise, <c>false</c>.
/// </value>
public Boolean Ignored {
get;
}
}
}

View File

@ -1,344 +1,302 @@
using System;
#nullable enable
using System;
using System.Collections.Concurrent;
using System.ComponentModel;
using System.Linq.Expressions;
using System.Reflection;
namespace Swan
{
namespace Swan {
/// <summary>
/// Provides a standard way to convert strings to different types.
/// </summary>
public static class FromString {
// It doesn't matter which converter we get here: ConvertFromInvariantString is not virtual.
private static readonly MethodInfo ConvertFromInvariantStringMethod = new Func<String, Object>(TypeDescriptor.GetConverter(typeof(Int32)).ConvertFromInvariantString).Method;
private static readonly MethodInfo? TryConvertToInternalMethod = typeof(FromString).GetMethod(nameof(TryConvertToInternal), BindingFlags.Static | BindingFlags.NonPublic);
private static readonly MethodInfo? ConvertToInternalMethod = typeof(FromString).GetMethod(nameof(ConvertToInternal), BindingFlags.Static | BindingFlags.NonPublic);
private static readonly ConcurrentDictionary<Type, Func<String[], (Boolean Success, Object Result)>> GenericTryConvertToMethods = new ConcurrentDictionary<Type, Func<String[], (Boolean Success, Object Result)>>();
private static readonly ConcurrentDictionary<Type, Func<String[], Object>> GenericConvertToMethods = new ConcurrentDictionary<Type, Func<String[], Object>>();
/// <summary>
/// Provides a standard way to convert strings to different types.
/// Determines whether a string can be converted to the specified type.
/// </summary>
public static class FromString
{
// It doesn't matter which converter we get here: ConvertFromInvariantString is not virtual.
private static readonly MethodInfo ConvertFromInvariantStringMethod
= new Func<string, object>(TypeDescriptor.GetConverter(typeof(int)).ConvertFromInvariantString).Method;
private static readonly MethodInfo TryConvertToInternalMethod
= typeof(FromString).GetMethod(nameof(TryConvertToInternal), BindingFlags.Static | BindingFlags.NonPublic);
private static readonly MethodInfo ConvertToInternalMethod
= typeof(FromString).GetMethod(nameof(ConvertToInternal), BindingFlags.Static | BindingFlags.NonPublic);
private static readonly ConcurrentDictionary<Type, Func<string[], (bool Success, object Result)>> GenericTryConvertToMethods
= new ConcurrentDictionary<Type, Func<string[], (bool Success, object Result)>>();
private static readonly ConcurrentDictionary<Type, Func<string[], object>> GenericConvertToMethods
= new ConcurrentDictionary<Type, Func<string[], object>>();
/// <summary>
/// Determines whether a string can be converted to the specified type.
/// </summary>
/// <param name="type">The type resulting from the conversion.</param>
/// <returns><see langword="true" /> if the conversion is possible;
/// otherwise, <see langword="false" />.</returns>
/// <exception cref="ArgumentNullException"><paramref name="type" /> is <see langword="null" />.</exception>
public static bool CanConvertTo(Type type)
=> TypeDescriptor.GetConverter(type).CanConvertFrom(typeof(string));
/// <summary>
/// Determines whether a string can be converted to the specified type.
/// </summary>
/// <typeparam name="TResult">The type resulting from the conversion.</typeparam>
/// <returns><see langword="true" /> if the conversion is possible;
/// otherwise, <see langword="false" />.</returns>
public static bool CanConvertTo<TResult>()
=> TypeDescriptor.GetConverter(typeof(TResult)).CanConvertFrom(typeof(string));
/// <summary>
/// Attempts to convert a string to the specified type.
/// </summary>
/// <param name="type">The type resulting from the conversion.</param>
/// <param name="str">The string to convert.</param>
/// <param name="result">When this method returns <see langword="true" />,
/// the result of the conversion. This parameter is passed uninitialized.</param>
/// <returns><see langword="true" /> if the conversion is successful;
/// otherwise, <see langword="false" />.</returns>
/// <exception cref="ArgumentNullException"><paramref name="type" /> is <see langword="null" />.</exception>
public static bool TryConvertTo(Type type, string str, out object? result)
{
var converter = TypeDescriptor.GetConverter(type);
if (!converter.CanConvertFrom(typeof(string)))
{
result = null;
return false;
}
try
{
result = converter.ConvertFromInvariantString(str);
return true;
}
catch (Exception e) when (!e.IsCriticalException())
{
result = null;
return false;
}
}
/// <summary>
/// Attempts to convert a string to the specified type.
/// </summary>
/// <typeparam name="TResult">The type resulting from the conversion.</typeparam>
/// <param name="str">The string to convert.</param>
/// <param name="result">When this method returns <see langword="true" />,
/// the result of the conversion. This parameter is passed uninitialized.</param>
/// <returns><see langword="true" /> if the conversion is successful;
/// otherwise, <see langword="false" />.</returns>
public static bool TryConvertTo<TResult>(string str, out TResult result)
{
var converter = TypeDescriptor.GetConverter(typeof(TResult));
if (!converter.CanConvertFrom(typeof(string)))
{
result = default;
return false;
}
try
{
result = (TResult)converter.ConvertFromInvariantString(str);
return true;
}
catch (Exception e) when (!e.IsCriticalException())
{
result = default;
return false;
}
}
/// <summary>
/// Converts a string to the specified type.
/// </summary>
/// <param name="type">The type resulting from the conversion.</param>
/// <param name="str">The string to convert.</param>
/// <returns>An instance of <paramref name="type" />.</returns>
/// <exception cref="ArgumentNullException"><paramref name="type" /> is <see langword="null" />.</exception>
/// <exception cref="StringConversionException">The conversion was not successful.</exception>
public static object ConvertTo(Type type, string str)
{
if (type == null)
throw new ArgumentNullException(nameof(type));
try
{
return TypeDescriptor.GetConverter(type).ConvertFromInvariantString(str);
}
catch (Exception e) when (!e.IsCriticalException())
{
throw new StringConversionException(type, e);
}
}
/// <summary>
/// Converts a string to the specified type.
/// </summary>
/// <typeparam name="TResult">The type resulting from the conversion.</typeparam>
/// <param name="str">The string to convert.</param>
/// <returns>An instance of <typeparamref name="TResult" />.</returns>
/// <exception cref="StringConversionException">
/// The conversion was not successful.
/// </exception>
public static TResult ConvertTo<TResult>(string str)
{
try
{
return (TResult)TypeDescriptor.GetConverter(typeof(TResult)).ConvertFromInvariantString(str);
}
catch (Exception e) when (!e.IsCriticalException())
{
throw new StringConversionException(typeof(TResult), e);
}
}
/// <summary>
/// Attempts to convert an array of strings to an array of the specified type.
/// </summary>
/// <param name="type">The type resulting from the conversion of each
/// element of <paramref name="strings"/>.</param>
/// <param name="strings">The array to convert.</param>
/// <param name="result">When this method returns <see langword="true" />,
/// the result of the conversion. This parameter is passed uninitialized.</param>
/// <returns><see langword="true" /> if the conversion is successful;
/// otherwise, <see langword="false" />.</returns>
/// <exception cref="ArgumentNullException"><paramref name="type" /> is <see langword="null" />.</exception>
public static bool TryConvertTo(Type type, string[] strings, out object? result)
{
if (strings == null)
{
result = null;
return false;
}
var method = GenericTryConvertToMethods.GetOrAdd(type, BuildNonGenericTryConvertLambda);
var (success, methodResult) = method(strings);
result = methodResult;
return success;
}
/// <summary>
/// Attempts to convert an array of strings to an array of the specified type.
/// </summary>
/// <typeparam name="TResult">The type resulting from the conversion of each
/// element of <paramref name="strings"/>.</typeparam>
/// <param name="strings">The array to convert.</param>
/// <param name="result">When this method returns <see langword="true" />,
/// the result of the conversion. This parameter is passed uninitialized.</param>
/// <returns><see langword="true" /> if the conversion is successful;
/// otherwise, <see langword="false" />.</returns>
public static bool TryConvertTo<TResult>(string[] strings, out TResult[]? result)
{
if (strings == null)
{
result = null;
return false;
}
var converter = TypeDescriptor.GetConverter(typeof(TResult));
if (!converter.CanConvertFrom(typeof(string)))
{
result = null;
return false;
}
try
{
result = new TResult[strings.Length];
var i = 0;
foreach (var str in strings)
result[i++] = (TResult)converter.ConvertFromInvariantString(str);
return true;
}
catch (Exception e) when (!e.IsCriticalException())
{
result = null;
return false;
}
}
/// <summary>
/// Converts an array of strings to an array of the specified type.
/// </summary>
/// <param name="type">The type resulting from the conversion of each
/// element of <paramref name="strings"/>.</param>
/// <param name="strings">The array to convert.</param>
/// <returns>An array of <paramref name="type" />.</returns>
/// <exception cref="ArgumentNullException"><paramref name="type" /> is <see langword="null" />.</exception>
/// <exception cref="StringConversionException">The conversion of at least one
/// of the elements of <paramref name="strings"/>was not successful.</exception>
public static object? ConvertTo(Type type, string[] strings)
{
if (strings == null)
return null;
var method = GenericConvertToMethods.GetOrAdd(type, BuildNonGenericConvertLambda);
return method(strings);
}
/// <summary>
/// Converts an array of strings to an array of the specified type.
/// </summary>
/// <typeparam name="TResult">The type resulting from the conversion of each
/// element of <paramref name="strings"/>.</typeparam>
/// <param name="strings">The array to convert.</param>
/// <returns>An array of <typeparamref name="TResult" />.</returns>
/// <exception cref="StringConversionException">The conversion of at least one
/// of the elements of <paramref name="strings"/>was not successful.</exception>
public static TResult[]? ConvertTo<TResult>(string[] strings)
{
if (strings == null)
return null;
var converter = TypeDescriptor.GetConverter(typeof(TResult));
var result = new TResult[strings.Length];
var i = 0;
try
{
foreach (var str in strings)
result[i++] = (TResult)converter.ConvertFromInvariantString(str);
}
catch (Exception e) when (!e.IsCriticalException())
{
throw new StringConversionException(typeof(TResult), e);
}
return result;
}
/// <summary>
/// Converts a expression, if the type can be converted to string, to a new expression including
/// the conversion to string.
/// </summary>
/// <param name="type">The type.</param>
/// <param name="str">The string.</param>
/// <returns>A new expression where the previous expression is converted to string.</returns>
public static Expression? ConvertExpressionTo(Type type, Expression str)
{
var converter = TypeDescriptor.GetConverter(type);
return converter.CanConvertFrom(typeof(string))
? Expression.Convert(
Expression.Call(Expression.Constant(converter), ConvertFromInvariantStringMethod, str),
type)
: null;
}
private static Func<string[], (bool Success, object Result)> BuildNonGenericTryConvertLambda(Type type)
{
var methodInfo = TryConvertToInternalMethod.MakeGenericMethod(type);
var parameter = Expression.Parameter(typeof(string[]));
var body = Expression.Call(methodInfo, parameter);
var lambda = Expression.Lambda<Func<string[], (bool Success, object Result)>>(body, parameter);
return lambda.Compile();
}
private static (bool Success, object? Result) TryConvertToInternal<TResult>(string[] strings)
{
var converter = TypeDescriptor.GetConverter(typeof(TResult));
if (!converter.CanConvertFrom(typeof(string)))
return (false, null);
var result = new TResult[strings.Length];
var i = 0;
try
{
foreach (var str in strings)
result[i++] = (TResult)converter.ConvertFromInvariantString(str);
return (true, result);
}
catch (Exception e) when (!e.IsCriticalException())
{
return (false, null);
}
}
private static Func<string[], object> BuildNonGenericConvertLambda(Type type)
{
var methodInfo = ConvertToInternalMethod.MakeGenericMethod(type);
var parameter = Expression.Parameter(typeof(string[]));
var body = Expression.Call(methodInfo, parameter);
var lambda = Expression.Lambda<Func<string[], object>>(body, parameter);
return lambda.Compile();
}
private static object ConvertToInternal<TResult>(string[] strings)
{
var converter = TypeDescriptor.GetConverter(typeof(TResult));
var result = new TResult[strings.Length];
var i = 0;
try
{
foreach (var str in strings)
result[i++] = (TResult)converter.ConvertFromInvariantString(str);
return result;
}
catch (Exception e) when (!e.IsCriticalException())
{
throw new StringConversionException(typeof(TResult), e);
}
}
}
/// <param name="type">The type resulting from the conversion.</param>
/// <returns><see langword="true" /> if the conversion is possible;
/// otherwise, <see langword="false" />.</returns>
/// <exception cref="ArgumentNullException"><paramref name="type" /> is <see langword="null" />.</exception>
public static Boolean CanConvertTo(Type type) => TypeDescriptor.GetConverter(type).CanConvertFrom(typeof(String));
/// <summary>
/// Determines whether a string can be converted to the specified type.
/// </summary>
/// <typeparam name="TResult">The type resulting from the conversion.</typeparam>
/// <returns><see langword="true" /> if the conversion is possible;
/// otherwise, <see langword="false" />.</returns>
public static Boolean CanConvertTo<TResult>() => TypeDescriptor.GetConverter(typeof(TResult)).CanConvertFrom(typeof(String));
/// <summary>
/// Attempts to convert a string to the specified type.
/// </summary>
/// <param name="type">The type resulting from the conversion.</param>
/// <param name="str">The string to convert.</param>
/// <param name="result">When this method returns <see langword="true" />,
/// the result of the conversion. This parameter is passed uninitialized.</param>
/// <returns><see langword="true" /> if the conversion is successful;
/// otherwise, <see langword="false" />.</returns>
/// <exception cref="ArgumentNullException"><paramref name="type" /> is <see langword="null" />.</exception>
public static Boolean TryConvertTo(Type type, String str, out Object? result) {
TypeConverter converter = TypeDescriptor.GetConverter(type);
if(!converter.CanConvertFrom(typeof(String))) {
result = null;
return false;
}
try {
result = converter.ConvertFromInvariantString(str);
return true;
} catch(Exception e) when(!e.IsCriticalException()) {
result = null;
return false;
}
}
/// <summary>
/// Attempts to convert a string to the specified type.
/// </summary>
/// <typeparam name="TResult">The type resulting from the conversion.</typeparam>
/// <param name="str">The string to convert.</param>
/// <param name="result">When this method returns <see langword="true" />,
/// the result of the conversion. This parameter is passed uninitialized.</param>
/// <returns><see langword="true" /> if the conversion is successful;
/// otherwise, <see langword="false" />.</returns>
public static Boolean TryConvertTo<TResult>(String str, out TResult result) where TResult : notnull {
TypeConverter converter = TypeDescriptor.GetConverter(typeof(TResult));
if(!converter.CanConvertFrom(typeof(String))) {
result = default!;
return false;
}
try {
result = (TResult)converter.ConvertFromInvariantString(str);
return true;
} catch(Exception e) when(!e.IsCriticalException()) {
result = default!;
return false;
}
}
/// <summary>
/// Converts a string to the specified type.
/// </summary>
/// <param name="type">The type resulting from the conversion.</param>
/// <param name="str">The string to convert.</param>
/// <returns>An instance of <paramref name="type" />.</returns>
/// <exception cref="ArgumentNullException"><paramref name="type" /> is <see langword="null" />.</exception>
/// <exception cref="StringConversionException">The conversion was not successful.</exception>
public static Object ConvertTo(Type type, String str) {
if(type == null) {
throw new ArgumentNullException(nameof(type));
}
try {
return TypeDescriptor.GetConverter(type).ConvertFromInvariantString(str);
} catch(Exception e) when(!e.IsCriticalException()) {
throw new StringConversionException(type, e);
}
}
/// <summary>
/// Converts a string to the specified type.
/// </summary>
/// <typeparam name="TResult">The type resulting from the conversion.</typeparam>
/// <param name="str">The string to convert.</param>
/// <returns>An instance of <typeparamref name="TResult" />.</returns>
/// <exception cref="StringConversionException">
/// The conversion was not successful.
/// </exception>
public static TResult ConvertTo<TResult>(String str) {
try {
return (TResult)TypeDescriptor.GetConverter(typeof(TResult)).ConvertFromInvariantString(str);
} catch(Exception e) when(!e.IsCriticalException()) {
throw new StringConversionException(typeof(TResult), e);
}
}
/// <summary>
/// Attempts to convert an array of strings to an array of the specified type.
/// </summary>
/// <param name="type">The type resulting from the conversion of each
/// element of <paramref name="strings"/>.</param>
/// <param name="strings">The array to convert.</param>
/// <param name="result">When this method returns <see langword="true" />,
/// the result of the conversion. This parameter is passed uninitialized.</param>
/// <returns><see langword="true" /> if the conversion is successful;
/// otherwise, <see langword="false" />.</returns>
/// <exception cref="ArgumentNullException"><paramref name="type" /> is <see langword="null" />.</exception>
public static Boolean TryConvertTo(Type type, String[] strings, out Object? result) {
if(strings == null) {
result = null;
return false;
}
Func<String[], (Boolean Success, Object Result)> method = GenericTryConvertToMethods.GetOrAdd(type, BuildNonGenericTryConvertLambda);
(Boolean success, Object methodResult) = method(strings);
result = methodResult;
return success;
}
/// <summary>
/// Attempts to convert an array of strings to an array of the specified type.
/// </summary>
/// <typeparam name="TResult">The type resulting from the conversion of each
/// element of <paramref name="strings"/>.</typeparam>
/// <param name="strings">The array to convert.</param>
/// <param name="result">When this method returns <see langword="true" />,
/// the result of the conversion. This parameter is passed uninitialized.</param>
/// <returns><see langword="true" /> if the conversion is successful;
/// otherwise, <see langword="false" />.</returns>
public static Boolean TryConvertTo<TResult>(String[] strings, out TResult[]? result) {
if(strings == null) {
result = null;
return false;
}
TypeConverter converter = TypeDescriptor.GetConverter(typeof(TResult));
if(!converter.CanConvertFrom(typeof(String))) {
result = null;
return false;
}
try {
result = new TResult[strings.Length];
Int32 i = 0;
foreach(String str in strings) {
result[i++] = (TResult)converter.ConvertFromInvariantString(str);
}
return true;
} catch(Exception e) when(!e.IsCriticalException()) {
result = null;
return false;
}
}
/// <summary>
/// Converts an array of strings to an array of the specified type.
/// </summary>
/// <param name="type">The type resulting from the conversion of each
/// element of <paramref name="strings"/>.</param>
/// <param name="strings">The array to convert.</param>
/// <returns>An array of <paramref name="type" />.</returns>
/// <exception cref="ArgumentNullException"><paramref name="type" /> is <see langword="null" />.</exception>
/// <exception cref="StringConversionException">The conversion of at least one
/// of the elements of <paramref name="strings"/>was not successful.</exception>
public static Object? ConvertTo(Type type, String[] strings) {
if(strings == null) {
return null;
}
Func<String[], Object> method = GenericConvertToMethods.GetOrAdd(type, BuildNonGenericConvertLambda);
return method(strings);
}
/// <summary>
/// Converts an array of strings to an array of the specified type.
/// </summary>
/// <typeparam name="TResult">The type resulting from the conversion of each
/// element of <paramref name="strings"/>.</typeparam>
/// <param name="strings">The array to convert.</param>
/// <returns>An array of <typeparamref name="TResult" />.</returns>
/// <exception cref="StringConversionException">The conversion of at least one
/// of the elements of <paramref name="strings"/>was not successful.</exception>
public static TResult[]? ConvertTo<TResult>(String[] strings) {
if(strings == null) {
return null;
}
TypeConverter converter = TypeDescriptor.GetConverter(typeof(TResult));
TResult[] result = new TResult[strings.Length];
Int32 i = 0;
try {
foreach(String str in strings) {
result[i++] = (TResult)converter.ConvertFromInvariantString(str);
}
} catch(Exception e) when(!e.IsCriticalException()) {
throw new StringConversionException(typeof(TResult), e);
}
return result;
}
/// <summary>
/// Converts a expression, if the type can be converted to string, to a new expression including
/// the conversion to string.
/// </summary>
/// <param name="type">The type.</param>
/// <param name="str">The string.</param>
/// <returns>A new expression where the previous expression is converted to string.</returns>
public static Expression? ConvertExpressionTo(Type type, Expression str) {
TypeConverter converter = TypeDescriptor.GetConverter(type);
return converter.CanConvertFrom(typeof(String))
? Expression.Convert(
Expression.Call(Expression.Constant(converter), ConvertFromInvariantStringMethod, str),
type)
: null;
}
private static Func<String[], (Boolean Success, Object Result)> BuildNonGenericTryConvertLambda(Type type) {
MethodInfo? methodInfo = TryConvertToInternalMethod?.MakeGenericMethod(type);
ParameterExpression parameter = Expression.Parameter(typeof(String[]));
MethodCallExpression body = Expression.Call(methodInfo, parameter);
Expression<Func<String[], (Boolean Success, Object Result)>> lambda = Expression.Lambda<Func<String[], (Boolean Success, Object Result)>>(body, parameter);
return lambda.Compile();
}
private static (Boolean Success, Object? Result) TryConvertToInternal<TResult>(String[] strings) {
TypeConverter converter = TypeDescriptor.GetConverter(typeof(TResult));
if(!converter.CanConvertFrom(typeof(String))) {
return (false, null);
}
TResult[] result = new TResult[strings.Length];
Int32 i = 0;
try {
foreach(String str in strings) {
result[i++] = (TResult)converter.ConvertFromInvariantString(str);
}
return (true, result);
} catch(Exception e) when(!e.IsCriticalException()) {
return (false, null);
}
}
private static Func<String[], Object> BuildNonGenericConvertLambda(Type type) {
MethodInfo? methodInfo = ConvertToInternalMethod?.MakeGenericMethod(type);
ParameterExpression parameter = Expression.Parameter(typeof(String[]));
MethodCallExpression body = Expression.Call(methodInfo, parameter);
Expression<Func<String[], Object>> lambda = Expression.Lambda<Func<String[], Object>>(body, parameter);
return lambda.Compile();
}
private static Object ConvertToInternal<TResult>(String[] strings) {
TypeConverter converter = TypeDescriptor.GetConverter(typeof(TResult));
TResult[] result = new TResult[strings.Length];
Int32 i = 0;
try {
foreach(String str in strings) {
result[i++] = (TResult)converter.ConvertFromInvariantString(str);
}
return result;
} catch(Exception e) when(!e.IsCriticalException()) {
throw new StringConversionException(typeof(TResult), e);
}
}
}
}

View File

@ -1,146 +1,139 @@
using System;
using Swan.Lite.Logging;
namespace Swan.Logging
{
namespace Swan.Logging {
/// <summary>
/// Represents a Console implementation of <c>ILogger</c>.
/// </summary>
/// <seealso cref="ILogger" />
public class ConsoleLogger : TextLogger, ILogger {
/// <summary>
/// Represents a Console implementation of <c>ILogger</c>.
/// Initializes a new instance of the <see cref="ConsoleLogger"/> class.
/// </summary>
/// <seealso cref="ILogger" />
public class ConsoleLogger : TextLogger, ILogger
{
/// <summary>
/// Initializes a new instance of the <see cref="ConsoleLogger"/> class.
/// </summary>
protected ConsoleLogger()
{
// Empty
}
/// <summary>
/// Gets the current instance of ConsoleLogger.
/// </summary>
/// <value>
/// The instance.
/// </value>
public static ConsoleLogger Instance { get; } = new ConsoleLogger();
/// <summary>
/// Gets or sets the debug logging prefix.
/// </summary>
/// <value>
/// The debug prefix.
/// </value>
public static string DebugPrefix { get; set; } = "DBG";
/// <summary>
/// Gets or sets the trace logging prefix.
/// </summary>
/// <value>
/// The trace prefix.
/// </value>
public static string TracePrefix { get; set; } = "TRC";
/// <summary>
/// Gets or sets the warning logging prefix.
/// </summary>
/// <value>
/// The warn prefix.
/// </value>
public static string WarnPrefix { get; set; } = "WRN";
/// <summary>
/// Gets or sets the fatal logging prefix.
/// </summary>
/// <value>
/// The fatal prefix.
/// </value>
public static string FatalPrefix { get; set; } = "FAT";
/// <summary>
/// Gets or sets the error logging prefix.
/// </summary>
/// <value>
/// The error prefix.
/// </value>
public static string ErrorPrefix { get; set; } = "ERR";
/// <summary>
/// Gets or sets the information logging prefix.
/// </summary>
/// <value>
/// The information prefix.
/// </value>
public static string InfoPrefix { get; set; } = "INF";
/// <summary>
/// Gets or sets the color of the information output logging.
/// </summary>
/// <value>
/// The color of the information.
/// </value>
public static ConsoleColor InfoColor { get; set; } = ConsoleColor.Cyan;
/// <summary>
/// Gets or sets the color of the debug output logging.
/// </summary>
/// <value>
/// The color of the debug.
/// </value>
public static ConsoleColor DebugColor { get; set; } = ConsoleColor.Gray;
/// <summary>
/// Gets or sets the color of the trace output logging.
/// </summary>
/// <value>
/// The color of the trace.
/// </value>
public static ConsoleColor TraceColor { get; set; } = ConsoleColor.DarkGray;
/// <summary>
/// Gets or sets the color of the warning logging.
/// </summary>
/// <value>
/// The color of the warn.
/// </value>
public static ConsoleColor WarnColor { get; set; } = ConsoleColor.Yellow;
/// <summary>
/// Gets or sets the color of the error logging.
/// </summary>
/// <value>
/// The color of the error.
/// </value>
public static ConsoleColor ErrorColor { get; set; } = ConsoleColor.DarkRed;
/// <summary>
/// Gets or sets the color of the error logging.
/// </summary>
/// <value>
/// The color of the error.
/// </value>
public static ConsoleColor FatalColor { get; set; } = ConsoleColor.Red;
/// <inheritdoc />
public LogLevel LogLevel { get; set; } = DebugLogger.IsDebuggerAttached ? LogLevel.Trace : LogLevel.Info;
/// <inheritdoc />
public void Log(LogMessageReceivedEventArgs logEvent)
{
// Select the writer based on the message type
var writer = logEvent.MessageType == LogLevel.Error
? TerminalWriters.StandardError
: TerminalWriters.StandardOutput;
var (outputMessage, color) = GetOutputAndColor(logEvent);
Terminal.Write(outputMessage, color, writer);
}
/// <inheritdoc />
public void Dispose()
{
// Do nothing
}
}
protected ConsoleLogger() {
// Empty
}
/// <summary>
/// Gets the current instance of ConsoleLogger.
/// </summary>
/// <value>
/// The instance.
/// </value>
public static ConsoleLogger Instance { get; } = new ConsoleLogger();
/// <summary>
/// Gets or sets the debug logging prefix.
/// </summary>
/// <value>
/// The debug prefix.
/// </value>
public static String DebugPrefix { get; set; } = "DBG";
/// <summary>
/// Gets or sets the trace logging prefix.
/// </summary>
/// <value>
/// The trace prefix.
/// </value>
public static String TracePrefix { get; set; } = "TRC";
/// <summary>
/// Gets or sets the warning logging prefix.
/// </summary>
/// <value>
/// The warn prefix.
/// </value>
public static String WarnPrefix { get; set; } = "WRN";
/// <summary>
/// Gets or sets the fatal logging prefix.
/// </summary>
/// <value>
/// The fatal prefix.
/// </value>
public static String FatalPrefix { get; set; } = "FAT";
/// <summary>
/// Gets or sets the error logging prefix.
/// </summary>
/// <value>
/// The error prefix.
/// </value>
public static String ErrorPrefix { get; set; } = "ERR";
/// <summary>
/// Gets or sets the information logging prefix.
/// </summary>
/// <value>
/// The information prefix.
/// </value>
public static String InfoPrefix { get; set; } = "INF";
/// <summary>
/// Gets or sets the color of the information output logging.
/// </summary>
/// <value>
/// The color of the information.
/// </value>
public static ConsoleColor InfoColor { get; set; } = ConsoleColor.Cyan;
/// <summary>
/// Gets or sets the color of the debug output logging.
/// </summary>
/// <value>
/// The color of the debug.
/// </value>
public static ConsoleColor DebugColor { get; set; } = ConsoleColor.Gray;
/// <summary>
/// Gets or sets the color of the trace output logging.
/// </summary>
/// <value>
/// The color of the trace.
/// </value>
public static ConsoleColor TraceColor { get; set; } = ConsoleColor.DarkGray;
/// <summary>
/// Gets or sets the color of the warning logging.
/// </summary>
/// <value>
/// The color of the warn.
/// </value>
public static ConsoleColor WarnColor { get; set; } = ConsoleColor.Yellow;
/// <summary>
/// Gets or sets the color of the error logging.
/// </summary>
/// <value>
/// The color of the error.
/// </value>
public static ConsoleColor ErrorColor { get; set; } = ConsoleColor.DarkRed;
/// <summary>
/// Gets or sets the color of the error logging.
/// </summary>
/// <value>
/// The color of the error.
/// </value>
public static ConsoleColor FatalColor { get; set; } = ConsoleColor.Red;
/// <inheritdoc />
public LogLevel LogLevel { get; set; } = DebugLogger.IsDebuggerAttached ? LogLevel.Trace : LogLevel.Info;
/// <inheritdoc />
public void Log(LogMessageReceivedEventArgs logEvent) {
// Select the writer based on the message type
TerminalWriters writer = logEvent.MessageType == LogLevel.Error ? TerminalWriters.StandardError : TerminalWriters.StandardOutput;
(String outputMessage, ConsoleColor color) = this.GetOutputAndColor(logEvent);
Terminal.Write(outputMessage, color, writer);
}
/// <inheritdoc />
public void Dispose() {
// Do nothing
}
}
}

View File

@ -1,53 +1,49 @@
using Swan.Lite.Logging;
using System;
using Swan.Lite.Logging;
namespace Swan.Logging
{
namespace Swan.Logging {
/// <summary>
/// Represents a logger target. This target will write to the
/// Debug console using System.Diagnostics.Debug.
/// </summary>
/// <seealso cref="ILogger" />
public class DebugLogger : TextLogger, ILogger {
/// <summary>
/// Represents a logger target. This target will write to the
/// Debug console using System.Diagnostics.Debug.
/// Initializes a new instance of the <see cref="DebugLogger"/> class.
/// </summary>
/// <seealso cref="ILogger" />
public class DebugLogger : TextLogger, ILogger
{
/// <summary>
/// Initializes a new instance of the <see cref="DebugLogger"/> class.
/// </summary>
protected DebugLogger()
{
// Empty
}
/// <summary>
/// Gets the current instance of DebugLogger.
/// </summary>
/// <value>
/// The instance.
/// </value>
public static DebugLogger Instance { get; } = new DebugLogger();
/// <summary>
/// Gets a value indicating whether a debugger is attached.
/// </summary>
/// <value>
/// <c>true</c> if this instance is debugger attached; otherwise, <c>false</c>.
/// </value>
public static bool IsDebuggerAttached => System.Diagnostics.Debugger.IsAttached;
/// <inheritdoc/>
public LogLevel LogLevel { get; set; } = IsDebuggerAttached ? LogLevel.Trace : LogLevel.None;
/// <inheritdoc/>
public void Log(LogMessageReceivedEventArgs logEvent)
{
var (outputMessage, _) = GetOutputAndColor(logEvent);
System.Diagnostics.Debug.Write(outputMessage);
}
/// <inheritdoc/>
public void Dispose()
{
// do nothing
}
}
protected DebugLogger() {
// Empty
}
/// <summary>
/// Gets the current instance of DebugLogger.
/// </summary>
/// <value>
/// The instance.
/// </value>
public static DebugLogger Instance { get; } = new DebugLogger();
/// <summary>
/// Gets a value indicating whether a debugger is attached.
/// </summary>
/// <value>
/// <c>true</c> if this instance is debugger attached; otherwise, <c>false</c>.
/// </value>
public static Boolean IsDebuggerAttached => System.Diagnostics.Debugger.IsAttached;
/// <inheritdoc/>
public LogLevel LogLevel { get; set; } = IsDebuggerAttached ? LogLevel.Trace : LogLevel.None;
/// <inheritdoc/>
public void Log(LogMessageReceivedEventArgs logEvent) {
(String outputMessage, ConsoleColor _) = this.GetOutputAndColor(logEvent);
System.Diagnostics.Debug.Write(outputMessage);
}
/// <inheritdoc/>
public void Dispose() {
// do nothing
}
}
}

View File

@ -6,129 +6,118 @@ using System.Threading.Tasks;
using Swan.Lite.Logging;
using Swan.Threading;
namespace Swan.Logging
{
namespace Swan.Logging {
/// <summary>
/// A helper class to write into files the messages sent by the <see cref="Terminal" />.
/// </summary>
/// <seealso cref="ILogger" />
public class FileLogger : TextLogger, ILogger {
private readonly ManualResetEventSlim _doneEvent = new ManualResetEventSlim(true);
private readonly ConcurrentQueue<String> _logQueue = new ConcurrentQueue<String>();
private readonly ExclusiveTimer _timer;
private readonly String _filePath;
private Boolean _disposedValue; // To detect redundant calls
/// <summary>
/// A helper class to write into files the messages sent by the <see cref="Terminal" />.
/// Initializes a new instance of the <see cref="FileLogger"/> class.
/// </summary>
/// <seealso cref="ILogger" />
public class FileLogger : TextLogger, ILogger
{
private readonly ManualResetEventSlim _doneEvent = new ManualResetEventSlim(true);
private readonly ConcurrentQueue<string> _logQueue = new ConcurrentQueue<string>();
private readonly ExclusiveTimer _timer;
private readonly string _filePath;
private bool _disposedValue; // To detect redundant calls
/// <summary>
/// Initializes a new instance of the <see cref="FileLogger"/> class.
/// </summary>
public FileLogger()
: this(SwanRuntime.EntryAssemblyDirectory, true)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="FileLogger"/> class.
/// </summary>
/// <param name="filePath">The filePath.</param>
/// <param name="dailyFile">if set to <c>true</c> [daily file].</param>
public FileLogger(string filePath, bool dailyFile)
{
_filePath = filePath;
DailyFile = dailyFile;
_timer = new ExclusiveTimer(
async () => await WriteLogEntries().ConfigureAwait(false),
TimeSpan.Zero,
TimeSpan.FromSeconds(5));
}
/// <inheritdoc />
public LogLevel LogLevel { get; set; }
/// <summary>
/// Gets the file path.
/// </summary>
/// <value>
/// The file path.
/// </value>
public string FilePath => DailyFile
? Path.Combine(Path.GetDirectoryName(_filePath), Path.GetFileNameWithoutExtension(_filePath) + $"_{DateTime.UtcNow:yyyyMMdd}" + Path.GetExtension(_filePath))
: _filePath;
/// <summary>
/// Gets a value indicating whether [daily file].
/// </summary>
/// <value>
/// <c>true</c> if [daily file]; otherwise, <c>false</c>.
/// </value>
public bool DailyFile { get; }
/// <inheritdoc />
public void Log(LogMessageReceivedEventArgs logEvent)
{
var (outputMessage, _) = GetOutputAndColor(logEvent);
_logQueue.Enqueue(outputMessage);
}
/// <inheritdoc />
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
/// <summary>
/// Releases unmanaged and - optionally - managed resources.
/// </summary>
/// <param name="disposing"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.</param>
protected virtual void Dispose(bool disposing)
{
if (_disposedValue) return;
if (disposing)
{
_timer.Pause();
_timer.Dispose();
_doneEvent.Wait();
_doneEvent.Reset();
WriteLogEntries(true).Await();
_doneEvent.Dispose();
}
_disposedValue = true;
}
private async Task WriteLogEntries(bool finalCall = false)
{
if (_logQueue.IsEmpty)
return;
if (!finalCall && !_doneEvent.IsSet)
return;
_doneEvent.Reset();
try
{
using (var file = File.AppendText(FilePath))
{
while (!_logQueue.IsEmpty)
{
if (_logQueue.TryDequeue(out var entry))
await file.WriteAsync(entry).ConfigureAwait(false);
}
}
}
finally
{
if (!finalCall)
_doneEvent.Set();
}
}
}
public FileLogger() : this(SwanRuntime.EntryAssemblyDirectory, true) {
}
/// <summary>
/// Initializes a new instance of the <see cref="FileLogger"/> class.
/// </summary>
/// <param name="filePath">The filePath.</param>
/// <param name="dailyFile">if set to <c>true</c> [daily file].</param>
public FileLogger(String filePath, Boolean dailyFile) {
this._filePath = filePath;
this.DailyFile = dailyFile;
this._timer = new ExclusiveTimer(async () => await this.WriteLogEntries().ConfigureAwait(false), TimeSpan.Zero, TimeSpan.FromSeconds(5));
}
/// <inheritdoc />
public LogLevel LogLevel {
get; set;
}
/// <summary>
/// Gets the file path.
/// </summary>
/// <value>
/// The file path.
/// </value>
public String FilePath => this.DailyFile ? Path.Combine(Path.GetDirectoryName(this._filePath), Path.GetFileNameWithoutExtension(this._filePath) + $"_{DateTime.UtcNow:yyyyMMdd}" + Path.GetExtension(this._filePath)) : this._filePath;
/// <summary>
/// Gets a value indicating whether [daily file].
/// </summary>
/// <value>
/// <c>true</c> if [daily file]; otherwise, <c>false</c>.
/// </value>
public Boolean DailyFile {
get;
}
/// <inheritdoc />
public void Log(LogMessageReceivedEventArgs logEvent) {
(String outputMessage, ConsoleColor _) = this.GetOutputAndColor(logEvent);
this._logQueue.Enqueue(outputMessage);
}
/// <inheritdoc />
public void Dispose() {
this.Dispose(true);
GC.SuppressFinalize(this);
}
/// <summary>
/// Releases unmanaged and - optionally - managed resources.
/// </summary>
/// <param name="disposing"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.</param>
protected virtual void Dispose(Boolean disposing) {
if(this._disposedValue) {
return;
}
if(disposing) {
this._timer.Pause();
this._timer.Dispose();
this._doneEvent.Wait();
this._doneEvent.Reset();
this.WriteLogEntries(true).Await();
this._doneEvent.Dispose();
}
this._disposedValue = true;
}
private async Task WriteLogEntries(Boolean finalCall = false) {
if(this._logQueue.IsEmpty) {
return;
}
if(!finalCall && !this._doneEvent.IsSet) {
return;
}
this._doneEvent.Reset();
try {
using StreamWriter file = File.AppendText(this.FilePath);
while(!this._logQueue.IsEmpty) {
if(this._logQueue.TryDequeue(out String entry)) {
await file.WriteAsync(entry).ConfigureAwait(false);
}
}
} finally {
if(!finalCall) {
this._doneEvent.Set();
}
}
}
}
}

View File

@ -1,24 +1,24 @@
namespace Swan.Logging
{
using System;
using System;
namespace Swan.Logging {
/// <summary>
/// Interface for a logger implementation.
/// </summary>
public interface ILogger : IDisposable {
/// <summary>
/// Interface for a logger implementation.
/// Gets the log level.
/// </summary>
public interface ILogger : IDisposable
{
/// <summary>
/// Gets the log level.
/// </summary>
/// <value>
/// The log level.
/// </value>
LogLevel LogLevel { get; }
/// <summary>
/// Logs the specified log event.
/// </summary>
/// <param name="logEvent">The <see cref="LogMessageReceivedEventArgs"/> instance containing the event data.</param>
void Log(LogMessageReceivedEventArgs logEvent);
}
/// <value>
/// The log level.
/// </value>
LogLevel LogLevel {
get;
}
/// <summary>
/// Logs the specified log event.
/// </summary>
/// <param name="logEvent">The <see cref="LogMessageReceivedEventArgs"/> instance containing the event data.</param>
void Log(LogMessageReceivedEventArgs logEvent);
}
}

View File

@ -1,43 +1,41 @@
namespace Swan
{
namespace Swan {
/// <summary>
/// Defines the log levels.
/// </summary>
public enum LogLevel {
/// <summary>
/// Defines the log levels.
/// The none message type
/// </summary>
public enum LogLevel
{
/// <summary>
/// The none message type
/// </summary>
None,
/// <summary>
/// The trace message type
/// </summary>
Trace,
/// <summary>
/// The debug message type
/// </summary>
Debug,
/// <summary>
/// The information message type
/// </summary>
Info,
/// <summary>
/// The warning message type
/// </summary>
Warning,
/// <summary>
/// The error message type
/// </summary>
Error,
/// <summary>
/// The fatal message type
/// </summary>
Fatal,
}
None,
/// <summary>
/// The trace message type
/// </summary>
Trace,
/// <summary>
/// The debug message type
/// </summary>
Debug,
/// <summary>
/// The information message type
/// </summary>
Info,
/// <summary>
/// The warning message type
/// </summary>
Warning,
/// <summary>
/// The error message type
/// </summary>
Error,
/// <summary>
/// The fatal message type
/// </summary>
Fatal,
}
}

View File

@ -1,131 +1,147 @@
using System;
#nullable enable
using System;
namespace Swan
{
namespace Swan {
/// <summary>
/// Event arguments representing the message that is logged
/// on to the terminal. Use the properties to forward the data to
/// your logger of choice.
/// </summary>
/// <seealso cref="System.EventArgs" />
public class LogMessageReceivedEventArgs : EventArgs {
/// <summary>
/// Event arguments representing the message that is logged
/// on to the terminal. Use the properties to forward the data to
/// your logger of choice.
/// Initializes a new instance of the <see cref="LogMessageReceivedEventArgs" /> class.
/// </summary>
/// <seealso cref="System.EventArgs" />
public class LogMessageReceivedEventArgs : EventArgs
{
/// <summary>
/// Initializes a new instance of the <see cref="LogMessageReceivedEventArgs" /> class.
/// </summary>
/// <param name="sequence">The sequence.</param>
/// <param name="messageType">Type of the message.</param>
/// <param name="utcDate">The UTC date.</param>
/// <param name="source">The source.</param>
/// <param name="message">The message.</param>
/// <param name="extendedData">The extended data.</param>
/// <param name="callerMemberName">Name of the caller member.</param>
/// <param name="callerFilePath">The caller file path.</param>
/// <param name="callerLineNumber">The caller line number.</param>
public LogMessageReceivedEventArgs(
ulong sequence,
LogLevel messageType,
DateTime utcDate,
string source,
string message,
object? extendedData,
string callerMemberName,
string callerFilePath,
int callerLineNumber)
{
Sequence = sequence;
MessageType = messageType;
UtcDate = utcDate;
Source = source;
Message = message;
CallerMemberName = callerMemberName;
CallerFilePath = callerFilePath;
CallerLineNumber = callerLineNumber;
ExtendedData = extendedData;
}
/// <summary>
/// Gets logging message sequence.
/// </summary>
/// <value>
/// The sequence.
/// </value>
public ulong Sequence { get; }
/// <summary>
/// Gets the type of the message.
/// It can be a combination as the enumeration is a set of bitwise flags.
/// </summary>
/// <value>
/// The type of the message.
/// </value>
public LogLevel MessageType { get; }
/// <summary>
/// Gets the UTC date at which the event at which the message was logged.
/// </summary>
/// <value>
/// The UTC date.
/// </value>
public DateTime UtcDate { get; }
/// <summary>
/// Gets the name of the source where the logging message
/// came from. This can come empty if the logger did not set it.
/// </summary>
/// <value>
/// The source.
/// </value>
public string Source { get; }
/// <summary>
/// Gets the body of the message.
/// </summary>
/// <value>
/// The message.
/// </value>
public string Message { get; }
/// <summary>
/// Gets the name of the caller member.
/// </summary>
/// <value>
/// The name of the caller member.
/// </value>
public string CallerMemberName { get; }
/// <summary>
/// Gets the caller file path.
/// </summary>
/// <value>
/// The caller file path.
/// </value>
public string CallerFilePath { get; }
/// <summary>
/// Gets the caller line number.
/// </summary>
/// <value>
/// The caller line number.
/// </value>
public int CallerLineNumber { get; }
/// <summary>
/// Gets an object representing extended data.
/// It could be an exception or anything else.
/// </summary>
/// <value>
/// The extended data.
/// </value>
public object? ExtendedData { get; }
/// <summary>
/// Gets the Extended Data properties cast as an Exception (if possible)
/// Otherwise, it return null.
/// </summary>
/// <value>
/// The exception.
/// </value>
public Exception? Exception => ExtendedData as Exception;
}
/// <param name="sequence">The sequence.</param>
/// <param name="messageType">Type of the message.</param>
/// <param name="utcDate">The UTC date.</param>
/// <param name="source">The source.</param>
/// <param name="message">The message.</param>
/// <param name="extendedData">The extended data.</param>
/// <param name="callerMemberName">Name of the caller member.</param>
/// <param name="callerFilePath">The caller file path.</param>
/// <param name="callerLineNumber">The caller line number.</param>
public LogMessageReceivedEventArgs(
UInt64 sequence,
LogLevel messageType,
DateTime utcDate,
String source,
String message,
Object? extendedData,
String callerMemberName,
String callerFilePath,
Int32 callerLineNumber) {
this.Sequence = sequence;
this.MessageType = messageType;
this.UtcDate = utcDate;
this.Source = source;
this.Message = message;
this.CallerMemberName = callerMemberName;
this.CallerFilePath = callerFilePath;
this.CallerLineNumber = callerLineNumber;
this.ExtendedData = extendedData;
}
/// <summary>
/// Gets logging message sequence.
/// </summary>
/// <value>
/// The sequence.
/// </value>
public UInt64 Sequence {
get;
}
/// <summary>
/// Gets the type of the message.
/// It can be a combination as the enumeration is a set of bitwise flags.
/// </summary>
/// <value>
/// The type of the message.
/// </value>
public LogLevel MessageType {
get;
}
/// <summary>
/// Gets the UTC date at which the event at which the message was logged.
/// </summary>
/// <value>
/// The UTC date.
/// </value>
public DateTime UtcDate {
get;
}
/// <summary>
/// Gets the name of the source where the logging message
/// came from. This can come empty if the logger did not set it.
/// </summary>
/// <value>
/// The source.
/// </value>
public String Source {
get;
}
/// <summary>
/// Gets the body of the message.
/// </summary>
/// <value>
/// The message.
/// </value>
public String Message {
get;
}
/// <summary>
/// Gets the name of the caller member.
/// </summary>
/// <value>
/// The name of the caller member.
/// </value>
public String CallerMemberName {
get;
}
/// <summary>
/// Gets the caller file path.
/// </summary>
/// <value>
/// The caller file path.
/// </value>
public String CallerFilePath {
get;
}
/// <summary>
/// Gets the caller line number.
/// </summary>
/// <value>
/// The caller line number.
/// </value>
public Int32 CallerLineNumber {
get;
}
/// <summary>
/// Gets an object representing extended data.
/// It could be an exception or anything else.
/// </summary>
/// <value>
/// The extended data.
/// </value>
public Object? ExtendedData {
get;
}
/// <summary>
/// Gets the Extended Data properties cast as an Exception (if possible)
/// Otherwise, it return null.
/// </summary>
/// <value>
/// The exception.
/// </value>
public Exception? Exception => this.ExtendedData as Exception;
}
}

File diff suppressed because it is too large Load Diff

View File

@ -1,89 +1,63 @@
using Swan.Logging;
using System;
namespace Swan.Lite.Logging
{
namespace Swan.Lite.Logging {
/// <summary>
/// Use this class for text-based logger.
/// </summary>
public abstract class TextLogger {
/// <summary>
/// Use this class for text-based logger.
/// Gets or sets the logging time format.
/// set to null or empty to prevent output.
/// </summary>
public abstract class TextLogger
{
/// <summary>
/// Gets or sets the logging time format.
/// set to null or empty to prevent output.
/// </summary>
/// <value>
/// The logging time format.
/// </value>
public static string LoggingTimeFormat { get; set; } = "HH:mm:ss.fff";
/// <summary>
/// Gets the color of the output of the message (the output message has a new line char in the end).
/// </summary>
/// <param name="logEvent">The <see cref="LogMessageReceivedEventArgs" /> instance containing the event data.</param>
/// <returns>
/// The output message formatted and the color of the console to be used.
/// </returns>
protected (string outputMessage, ConsoleColor color) GetOutputAndColor(LogMessageReceivedEventArgs logEvent)
{
var (prefix , color) = GetConsoleColorAndPrefix(logEvent.MessageType);
var loggerMessage = string.IsNullOrWhiteSpace(logEvent.Message)
? string.Empty
: logEvent.Message.RemoveControlCharsExcept('\n');
var outputMessage = CreateOutputMessage(logEvent.Source, loggerMessage, prefix, logEvent.UtcDate);
// Further format the output in the case there is an exception being logged
if (logEvent.MessageType == LogLevel.Error && logEvent.Exception != null)
{
try
{
outputMessage += $"{logEvent.Exception.Stringify().Indent()}{Environment.NewLine}";
}
catch
{
// Ignore
}
}
return (outputMessage, color);
}
private static (string Prefix, ConsoleColor color) GetConsoleColorAndPrefix(LogLevel messageType)
{
switch (messageType)
{
case LogLevel.Debug:
return (ConsoleLogger.DebugPrefix, ConsoleLogger.DebugColor);
case LogLevel.Error:
return (ConsoleLogger.ErrorPrefix, ConsoleLogger.ErrorColor);
case LogLevel.Info:
return (ConsoleLogger.InfoPrefix, ConsoleLogger.InfoColor);
case LogLevel.Trace:
return (ConsoleLogger.TracePrefix, ConsoleLogger.TraceColor);
case LogLevel.Warning:
return (ConsoleLogger.WarnPrefix, ConsoleLogger.WarnColor);
case LogLevel.Fatal:
return (ConsoleLogger.FatalPrefix, ConsoleLogger.FatalColor);
default:
return (new string(' ', ConsoleLogger.InfoPrefix.Length), Terminal.Settings.DefaultColor);
}
}
private static string CreateOutputMessage(string sourceName, string loggerMessage, string prefix, DateTime date)
{
var friendlySourceName = string.IsNullOrWhiteSpace(sourceName)
? string.Empty
: sourceName.SliceLength(sourceName.LastIndexOf('.') + 1, sourceName.Length);
var outputMessage = string.IsNullOrWhiteSpace(sourceName)
? loggerMessage
: $"[{friendlySourceName}] {loggerMessage}";
return string.IsNullOrWhiteSpace(LoggingTimeFormat)
? $" {prefix} >> {outputMessage}{Environment.NewLine}"
: $" {date.ToLocalTime().ToString(LoggingTimeFormat)} {prefix} >> {outputMessage}{Environment.NewLine}";
}
}
/// <value>
/// The logging time format.
/// </value>
public static String LoggingTimeFormat { get; set; } = "HH:mm:ss.fff";
/// <summary>
/// Gets the color of the output of the message (the output message has a new line char in the end).
/// </summary>
/// <param name="logEvent">The <see cref="LogMessageReceivedEventArgs" /> instance containing the event data.</param>
/// <returns>
/// The output message formatted and the color of the console to be used.
/// </returns>
protected (String outputMessage, ConsoleColor color) GetOutputAndColor(LogMessageReceivedEventArgs logEvent) {
(String prefix, ConsoleColor color) = GetConsoleColorAndPrefix(logEvent.MessageType);
String loggerMessage = String.IsNullOrWhiteSpace(logEvent.Message) ? String.Empty : logEvent.Message.RemoveControlCharsExcept('\n');
String outputMessage = CreateOutputMessage(logEvent.Source, loggerMessage, prefix, logEvent.UtcDate);
// Further format the output in the case there is an exception being logged
if(logEvent.MessageType == LogLevel.Error && logEvent.Exception != null) {
try {
outputMessage += $"{logEvent.Exception.Stringify().Indent()}{Environment.NewLine}";
} catch {
// Ignore
}
}
return (outputMessage, color);
}
private static (String Prefix, ConsoleColor color) GetConsoleColorAndPrefix(LogLevel messageType) => messageType switch
{
LogLevel.Debug => (ConsoleLogger.DebugPrefix, ConsoleLogger.DebugColor),
LogLevel.Error => (ConsoleLogger.ErrorPrefix, ConsoleLogger.ErrorColor),
LogLevel.Info => (ConsoleLogger.InfoPrefix, ConsoleLogger.InfoColor),
LogLevel.Trace => (ConsoleLogger.TracePrefix, ConsoleLogger.TraceColor),
LogLevel.Warning => (ConsoleLogger.WarnPrefix, ConsoleLogger.WarnColor),
LogLevel.Fatal => (ConsoleLogger.FatalPrefix, ConsoleLogger.FatalColor),
_ => (new String(' ', ConsoleLogger.InfoPrefix.Length), Terminal.Settings.DefaultColor),
};
private static String CreateOutputMessage(String sourceName, String loggerMessage, String prefix, DateTime date) {
String friendlySourceName = String.IsNullOrWhiteSpace(sourceName) ? String.Empty : sourceName.SliceLength(sourceName.LastIndexOf('.') + 1, sourceName.Length);
String outputMessage = String.IsNullOrWhiteSpace(sourceName) ? loggerMessage : $"[{friendlySourceName}] {loggerMessage}";
return String.IsNullOrWhiteSpace(LoggingTimeFormat) ? $" {prefix} >> {outputMessage}{Environment.NewLine}" : $" {date.ToLocalTime().ToString(LoggingTimeFormat)} {prefix} >> {outputMessage}{Environment.NewLine}";
}
}
}

View File

@ -1,13 +1,11 @@
using System;
namespace Swan.Mappers
{
/// <summary>
/// Represents an attribute to select which properties are copyable between objects.
/// </summary>
/// <seealso cref="Attribute" />
[AttributeUsage(AttributeTargets.Property)]
public class CopyableAttribute : Attribute
{
}
namespace Swan.Mappers {
/// <summary>
/// Represents an attribute to select which properties are copyable between objects.
/// </summary>
/// <seealso cref="Attribute" />
[AttributeUsage(AttributeTargets.Property)]
public class CopyableAttribute : Attribute {
}
}

View File

@ -2,26 +2,30 @@
using System.Collections.Generic;
using System.Reflection;
namespace Swan.Mappers
{
namespace Swan.Mappers {
/// <summary>
/// Interface object map.
/// </summary>
public interface IObjectMap {
/// <summary>
/// Interface object map.
/// Gets or sets the map.
/// </summary>
public interface IObjectMap
{
/// <summary>
/// Gets or sets the map.
/// </summary>
Dictionary<PropertyInfo, List<PropertyInfo>> Map { get; }
/// <summary>
/// Gets or sets the type of the source.
/// </summary>
Type SourceType { get; }
/// <summary>
/// Gets or sets the type of the destination.
/// </summary>
Type DestinationType { get; }
}
Dictionary<PropertyInfo, List<PropertyInfo>> Map {
get;
}
/// <summary>
/// Gets or sets the type of the source.
/// </summary>
Type SourceType {
get;
}
/// <summary>
/// Gets or sets the type of the destination.
/// </summary>
Type DestinationType {
get;
}
}
}

View File

@ -4,112 +4,106 @@ using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
namespace Swan.Mappers
{
namespace Swan.Mappers {
/// <summary>
/// Represents an object map.
/// </summary>
/// <typeparam name="TSource">The type of the source.</typeparam>
/// <typeparam name="TDestination">The type of the destination.</typeparam>
/// <seealso cref="IObjectMap" />
public class ObjectMap<TSource, TDestination> : IObjectMap {
internal ObjectMap(IEnumerable<PropertyInfo> intersect) {
this.SourceType = typeof(TSource);
this.DestinationType = typeof(TDestination);
this.Map = intersect.ToDictionary(property => this.DestinationType.GetProperty(property.Name), property => new List<PropertyInfo> { this.SourceType.GetProperty(property.Name) });
}
/// <inheritdoc/>
public Dictionary<PropertyInfo, List<PropertyInfo>> Map {
get;
}
/// <inheritdoc/>
public Type SourceType {
get;
}
/// <inheritdoc/>
public Type DestinationType {
get;
}
/// <summary>
/// Represents an object map.
/// Maps the property.
/// </summary>
/// <typeparam name="TSource">The type of the source.</typeparam>
/// <typeparam name="TDestination">The type of the destination.</typeparam>
/// <seealso cref="IObjectMap" />
public class ObjectMap<TSource, TDestination> : IObjectMap
{
internal ObjectMap(IEnumerable<PropertyInfo> intersect)
{
SourceType = typeof(TSource);
DestinationType = typeof(TDestination);
Map = intersect.ToDictionary(
property => DestinationType.GetProperty(property.Name),
property => new List<PropertyInfo> {SourceType.GetProperty(property.Name)});
}
/// <inheritdoc/>
public Dictionary<PropertyInfo, List<PropertyInfo>> Map { get; }
/// <inheritdoc/>
public Type SourceType { get; }
/// <inheritdoc/>
public Type DestinationType { get; }
/// <summary>
/// Maps the property.
/// </summary>
/// <typeparam name="TDestinationProperty">The type of the destination property.</typeparam>
/// <typeparam name="TSourceProperty">The type of the source property.</typeparam>
/// <param name="destinationProperty">The destination property.</param>
/// <param name="sourceProperty">The source property.</param>
/// <returns>
/// An object map representation of type of the destination property
/// and type of the source property.
/// </returns>
public ObjectMap<TSource, TDestination> MapProperty
<TDestinationProperty, TSourceProperty>(
Expression<Func<TDestination, TDestinationProperty>> destinationProperty,
Expression<Func<TSource, TSourceProperty>> sourceProperty)
{
var propertyDestinationInfo = (destinationProperty.Body as MemberExpression)?.Member as PropertyInfo;
if (propertyDestinationInfo == null)
{
throw new ArgumentException("Invalid destination expression", nameof(destinationProperty));
}
var sourceMembers = GetSourceMembers(sourceProperty);
if (sourceMembers.Any() == false)
{
throw new ArgumentException("Invalid source expression", nameof(sourceProperty));
}
// reverse order
sourceMembers.Reverse();
Map[propertyDestinationInfo] = sourceMembers;
return this;
}
/// <summary>
/// Removes the map property.
/// </summary>
/// <typeparam name="TDestinationProperty">The type of the destination property.</typeparam>
/// <param name="destinationProperty">The destination property.</param>
/// <returns>
/// An object map representation of type of the destination property
/// and type of the source property.
/// </returns>
/// <exception cref="System.Exception">Invalid destination expression.</exception>
public ObjectMap<TSource, TDestination> RemoveMapProperty<TDestinationProperty>(
Expression<Func<TDestination, TDestinationProperty>> destinationProperty)
{
var propertyDestinationInfo = (destinationProperty.Body as MemberExpression)?.Member as PropertyInfo;
if (propertyDestinationInfo == null)
throw new ArgumentException("Invalid destination expression", nameof(destinationProperty));
if (Map.ContainsKey(propertyDestinationInfo))
{
Map.Remove(propertyDestinationInfo);
}
return this;
}
private static List<PropertyInfo> GetSourceMembers<TSourceProperty>(Expression<Func<TSource, TSourceProperty>> sourceProperty)
{
var sourceMembers = new List<PropertyInfo>();
var initialExpression = sourceProperty.Body as MemberExpression;
while (true)
{
var propertySourceInfo = initialExpression?.Member as PropertyInfo;
if (propertySourceInfo == null) break;
sourceMembers.Add(propertySourceInfo);
initialExpression = initialExpression.Expression as MemberExpression;
}
return sourceMembers;
}
}
/// <typeparam name="TDestinationProperty">The type of the destination property.</typeparam>
/// <typeparam name="TSourceProperty">The type of the source property.</typeparam>
/// <param name="destinationProperty">The destination property.</param>
/// <param name="sourceProperty">The source property.</param>
/// <returns>
/// An object map representation of type of the destination property
/// and type of the source property.
/// </returns>
public ObjectMap<TSource, TDestination> MapProperty<TDestinationProperty, TSourceProperty>(Expression<Func<TDestination, TDestinationProperty>> destinationProperty, Expression<Func<TSource, TSourceProperty>> sourceProperty) {
PropertyInfo propertyDestinationInfo = (destinationProperty.Body as MemberExpression)?.Member as PropertyInfo;
if(propertyDestinationInfo == null) {
throw new ArgumentException("Invalid destination expression", nameof(destinationProperty));
}
List<PropertyInfo> sourceMembers = GetSourceMembers(sourceProperty);
if(sourceMembers.Any() == false) {
throw new ArgumentException("Invalid source expression", nameof(sourceProperty));
}
// reverse order
sourceMembers.Reverse();
this.Map[propertyDestinationInfo] = sourceMembers;
return this;
}
/// <summary>
/// Removes the map property.
/// </summary>
/// <typeparam name="TDestinationProperty">The type of the destination property.</typeparam>
/// <param name="destinationProperty">The destination property.</param>
/// <returns>
/// An object map representation of type of the destination property
/// and type of the source property.
/// </returns>
/// <exception cref="System.Exception">Invalid destination expression.</exception>
public ObjectMap<TSource, TDestination> RemoveMapProperty<TDestinationProperty>(Expression<Func<TDestination, TDestinationProperty>> destinationProperty) {
PropertyInfo propertyDestinationInfo = (destinationProperty.Body as MemberExpression)?.Member as PropertyInfo;
if(propertyDestinationInfo == null) {
throw new ArgumentException("Invalid destination expression", nameof(destinationProperty));
}
if(this.Map.ContainsKey(propertyDestinationInfo)) {
_ = this.Map.Remove(propertyDestinationInfo);
}
return this;
}
private static List<PropertyInfo> GetSourceMembers<TSourceProperty>(Expression<Func<TSource, TSourceProperty>> sourceProperty) {
List<PropertyInfo> sourceMembers = new List<PropertyInfo>();
MemberExpression initialExpression = sourceProperty.Body as MemberExpression;
while(true) {
PropertyInfo propertySourceInfo = initialExpression?.Member as PropertyInfo;
if(propertySourceInfo == null) {
break;
}
sourceMembers.Add(propertySourceInfo);
initialExpression = initialExpression.Expression as MemberExpression;
}
return sourceMembers;
}
}
}

View File

@ -1,24 +1,20 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using System.Reflection;
namespace Swan.Mappers
{
/// <summary>
/// Represents an AutoMapper-like object to map from one object type
/// to another using defined properties map or using the default behaviour
/// to copy same named properties from one object to another.
///
/// The extension methods like CopyPropertiesTo use the default behaviour.
/// </summary>
public partial class ObjectMapper
{
internal class PropertyInfoComparer : IEqualityComparer<PropertyInfo>
{
public bool Equals(PropertyInfo x, PropertyInfo y)
=> x != null && y != null && x.Name == y.Name && x.PropertyType == y.PropertyType;
public int GetHashCode(PropertyInfo obj)
=> obj.Name.GetHashCode() + obj.PropertyType.Name.GetHashCode();
}
}
namespace Swan.Mappers {
/// <summary>
/// Represents an AutoMapper-like object to map from one object type
/// to another using defined properties map or using the default behaviour
/// to copy same named properties from one object to another.
///
/// The extension methods like CopyPropertiesTo use the default behaviour.
/// </summary>
public partial class ObjectMapper {
internal class PropertyInfoComparer : IEqualityComparer<PropertyInfo> {
public Boolean Equals(PropertyInfo x, PropertyInfo y) => x != null && y != null && x.Name == y.Name && x.PropertyType == y.PropertyType;
public Int32 GetHashCode(PropertyInfo obj) => obj.Name.GetHashCode() + obj.PropertyType.Name.GetHashCode();
}
}
}

View File

@ -1,372 +1,309 @@
using System;
#nullable enable
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using Swan.Reflection;
namespace Swan.Mappers
{
namespace Swan.Mappers {
/// <summary>
/// Represents an AutoMapper-like object to map from one object type
/// to another using defined properties map or using the default behaviour
/// to copy same named properties from one object to another.
///
/// The extension methods like CopyPropertiesTo use the default behaviour.
/// </summary>
/// <example>
/// The following code explains how to map an object's properties into an instance of type T.
/// <code>
/// using Swan.Mappers;
///
/// class Example
/// {
/// class Person
/// {
/// public string Name { get; set; }
/// public int Age { get; set; }
/// }
///
/// static void Main()
/// {
/// var obj = new { Name = "John", Age = 42 };
///
/// var person = Runtime.ObjectMapper.Map&lt;Person&gt;(obj);
/// }
/// }
/// </code>
///
/// The following code explains how to explicitly map certain properties.
/// <code>
/// using Swan.Mappers;
///
/// class Example
/// {
/// class User
/// {
/// public string Name { get; set; }
/// public Role Role { get; set; }
/// }
///
/// public class Role
/// {
/// public string Name { get; set; }
/// }
///
/// class UserDto
/// {
/// public string Name { get; set; }
/// public string Role { get; set; }
/// }
///
/// static void Main()
/// {
/// // create a User object
/// var person =
/// new User { Name = "Phillip", Role = new Role { Name = "Admin" } };
///
/// // create an Object Mapper
/// var mapper = new ObjectMapper();
///
/// // map the User's Role.Name to UserDto's Role
/// mapper.CreateMap&lt;User, UserDto&gt;()
/// .MapProperty(d => d.Role, x => x.Role.Name);
///
/// // apply the previous map and retrieve a UserDto object
/// var destination = mapper.Map&lt;UserDto&gt;(person);
/// }
/// }
/// </code>
/// </example>
public partial class ObjectMapper {
private static readonly Lazy<ObjectMapper> LazyInstance = new Lazy<ObjectMapper>(() => new ObjectMapper());
private readonly List<IObjectMap> _maps = new List<IObjectMap>();
/// <summary>
/// Represents an AutoMapper-like object to map from one object type
/// to another using defined properties map or using the default behaviour
/// to copy same named properties from one object to another.
///
/// The extension methods like CopyPropertiesTo use the default behaviour.
/// Gets the current.
/// </summary>
/// <example>
/// The following code explains how to map an object's properties into an instance of type T.
/// <code>
/// using Swan.Mappers;
///
/// class Example
/// {
/// class Person
/// {
/// public string Name { get; set; }
/// public int Age { get; set; }
/// }
///
/// static void Main()
/// {
/// var obj = new { Name = "John", Age = 42 };
///
/// var person = Runtime.ObjectMapper.Map&lt;Person&gt;(obj);
/// }
/// }
/// </code>
///
/// The following code explains how to explicitly map certain properties.
/// <code>
/// using Swan.Mappers;
///
/// class Example
/// {
/// class User
/// {
/// public string Name { get; set; }
/// public Role Role { get; set; }
/// }
///
/// public class Role
/// {
/// public string Name { get; set; }
/// }
///
/// class UserDto
/// {
/// public string Name { get; set; }
/// public string Role { get; set; }
/// }
///
/// static void Main()
/// {
/// // create a User object
/// var person =
/// new User { Name = "Phillip", Role = new Role { Name = "Admin" } };
///
/// // create an Object Mapper
/// var mapper = new ObjectMapper();
///
/// // map the User's Role.Name to UserDto's Role
/// mapper.CreateMap&lt;User, UserDto&gt;()
/// .MapProperty(d => d.Role, x => x.Role.Name);
///
/// // apply the previous map and retrieve a UserDto object
/// var destination = mapper.Map&lt;UserDto&gt;(person);
/// }
/// }
/// </code>
/// </example>
public partial class ObjectMapper
{
private static readonly Lazy<ObjectMapper> LazyInstance = new Lazy<ObjectMapper>(() => new ObjectMapper());
private readonly List<IObjectMap> _maps = new List<IObjectMap>();
/// <summary>
/// Gets the current.
/// </summary>
/// <value>
/// The current.
/// </value>
public static ObjectMapper Current => LazyInstance.Value;
/// <summary>
/// Copies the specified source.
/// </summary>
/// <param name="source">The source.</param>
/// <param name="target">The target.</param>
/// <param name="propertiesToCopy">The properties to copy.</param>
/// <param name="ignoreProperties">The ignore properties.</param>
/// <returns>
/// Copied properties count.
/// </returns>
/// <exception cref="ArgumentNullException">
/// source
/// or
/// target.
/// </exception>
public static int Copy(
object source,
object target,
IEnumerable<string>? propertiesToCopy = null,
params string[]? ignoreProperties)
{
if (source == null)
throw new ArgumentNullException(nameof(source));
if (target == null)
throw new ArgumentNullException(nameof(target));
return CopyInternal(
target,
GetSourceMap(source),
propertiesToCopy,
ignoreProperties);
}
/// <summary>
/// Copies the specified source.
/// </summary>
/// <param name="source">The source.</param>
/// <param name="target">The target.</param>
/// <param name="propertiesToCopy">The properties to copy.</param>
/// <param name="ignoreProperties">The ignore properties.</param>
/// <returns>
/// Copied properties count.
/// </returns>
/// <exception cref="ArgumentNullException">
/// source
/// or
/// target.
/// </exception>
public static int Copy(
IDictionary<string, object> source,
object target,
IEnumerable<string>? propertiesToCopy = null,
params string[] ignoreProperties)
{
if (source == null)
throw new ArgumentNullException(nameof(source));
if (target == null)
throw new ArgumentNullException(nameof(target));
return CopyInternal(
target,
source.ToDictionary(
x => x.Key.ToLowerInvariant(),
x => Tuple.Create(typeof(object), x.Value)),
propertiesToCopy,
ignoreProperties);
}
/// <summary>
/// Creates the map.
/// </summary>
/// <typeparam name="TSource">The type of the source.</typeparam>
/// <typeparam name="TDestination">The type of the destination.</typeparam>
/// <returns>
/// An object map representation of type of the destination property
/// and type of the source property.
/// </returns>
/// <exception cref="InvalidOperationException">
/// You can't create an existing map
/// or
/// Types doesn't match.
/// </exception>
public ObjectMap<TSource, TDestination> CreateMap<TSource, TDestination>()
{
if (_maps.Any(x => x.SourceType == typeof(TSource) && x.DestinationType == typeof(TDestination)))
throw new InvalidOperationException("You can't create an existing map");
var sourceType = PropertyTypeCache.DefaultCache.Value.RetrieveAllProperties<TSource>(true);
var destinationType = PropertyTypeCache.DefaultCache.Value.RetrieveAllProperties<TDestination>(true);
var intersect = sourceType.Intersect(destinationType, new PropertyInfoComparer()).ToArray();
if (!intersect.Any())
throw new InvalidOperationException("Types doesn't match");
var map = new ObjectMap<TSource, TDestination>(intersect);
_maps.Add(map);
return map;
}
/// <summary>
/// Maps the specified source.
/// </summary>
/// <typeparam name="TDestination">The type of the destination.</typeparam>
/// <param name="source">The source.</param>
/// <param name="autoResolve">if set to <c>true</c> [automatic resolve].</param>
/// <returns>
/// A new instance of the map.
/// </returns>
/// <exception cref="ArgumentNullException">source.</exception>
/// <exception cref="InvalidOperationException">You can't map from type {source.GetType().Name} to {typeof(TDestination).Name}.</exception>
public TDestination Map<TDestination>(object source, bool autoResolve = true)
{
if (source == null)
{
throw new ArgumentNullException(nameof(source));
}
var destination = Activator.CreateInstance<TDestination>();
var map = _maps
.FirstOrDefault(x => x.SourceType == source.GetType() && x.DestinationType == typeof(TDestination));
if (map != null)
{
foreach (var property in map.Map)
{
var finalSource = property.Value.Aggregate(source,
(current, sourceProperty) => sourceProperty.GetValue(current));
property.Key.SetValue(destination, finalSource);
}
}
else
{
if (!autoResolve)
{
throw new InvalidOperationException(
$"You can't map from type {source.GetType().Name} to {typeof(TDestination).Name}");
}
// Missing mapping, try to use default behavior
Copy(source, destination);
}
return destination;
}
private static int CopyInternal(
object target,
Dictionary<string, Tuple<Type, object>> sourceProperties,
IEnumerable<string>? propertiesToCopy,
IEnumerable<string>? ignoreProperties)
{
// Filter properties
var requiredProperties = propertiesToCopy?
.Where(p => !string.IsNullOrWhiteSpace(p))
.Select(p => p.ToLowerInvariant());
var ignoredProperties = ignoreProperties?
.Where(p => !string.IsNullOrWhiteSpace(p))
.Select(p => p.ToLowerInvariant());
var properties = PropertyTypeCache.DefaultCache.Value
.RetrieveFilteredProperties(target.GetType(), true, x => x.CanWrite);
return properties
.Select(x => x.Name)
.Distinct()
.ToDictionary(x => x.ToLowerInvariant(), x => properties.First(y => y.Name == x))
.Where(x => sourceProperties.Keys.Contains(x.Key))
.When(() => requiredProperties != null, q => q.Where(y => requiredProperties.Contains(y.Key)))
.When(() => ignoredProperties != null, q => q.Where(y => !ignoredProperties.Contains(y.Key)))
.ToDictionary(x => x.Value, x => sourceProperties[x.Key])
.Sum(x => TrySetValue(x.Key, x.Value, target) ? 1 : 0);
}
private static bool TrySetValue(PropertyInfo propertyInfo, Tuple<Type, object> property, object target)
{
try
{
var (type, value) = property;
if (type.IsEnum)
{
propertyInfo.SetValue(target,
Enum.ToObject(propertyInfo.PropertyType, value));
return true;
}
if (type.IsValueType || propertyInfo.PropertyType != type)
return propertyInfo.TrySetBasicType(value, target);
if (propertyInfo.PropertyType.IsArray)
{
propertyInfo.TrySetArray(value as IEnumerable<object>, target);
return true;
}
propertyInfo.SetValue(target, GetValue(value, propertyInfo.PropertyType));
return true;
}
catch
{
// swallow
}
return false;
}
private static object? GetValue(object source, Type targetType)
{
if (source == null)
return null;
object? target = null;
source.CreateTarget(targetType, false, ref target);
switch (source)
{
case string _:
target = source;
break;
case IList sourceList when target is IList targetList:
var addMethod = targetType.GetMethods()
.FirstOrDefault(
m => m.Name == Formatters.Json.AddMethodName && m.IsPublic && m.GetParameters().Length == 1);
if (addMethod == null) return target;
var isItemValueType = targetList.GetType().GetElementType().IsValueType;
foreach (var item in sourceList)
{
try
{
targetList.Add(isItemValueType
? item
: item.CopyPropertiesToNew<object>());
}
catch
{
// ignored
}
}
break;
default:
source.CopyPropertiesTo(target);
break;
}
return target;
}
private static Dictionary<string, Tuple<Type, object>> GetSourceMap(object source)
{
// select distinct properties because they can be duplicated by inheritance
var sourceProperties = PropertyTypeCache.DefaultCache.Value
.RetrieveFilteredProperties(source.GetType(), true, x => x.CanRead)
.ToArray();
return sourceProperties
.Select(x => x.Name)
.Distinct()
.ToDictionary(
x => x.ToLowerInvariant(),
x => Tuple.Create(sourceProperties.First(y => y.Name == x).PropertyType,
sourceProperties.First(y => y.Name == x).GetValue(source)));
}
}
/// <value>
/// The current.
/// </value>
public static ObjectMapper Current => LazyInstance.Value;
/// <summary>
/// Copies the specified source.
/// </summary>
/// <param name="source">The source.</param>
/// <param name="target">The target.</param>
/// <param name="propertiesToCopy">The properties to copy.</param>
/// <param name="ignoreProperties">The ignore properties.</param>
/// <returns>
/// Copied properties count.
/// </returns>
/// <exception cref="ArgumentNullException">
/// source
/// or
/// target.
/// </exception>
public static Int32 Copy(Object source, Object? target, IEnumerable<String>? propertiesToCopy = null, params String[]? ignoreProperties) {
if(source == null) {
throw new ArgumentNullException(nameof(source));
}
if(target == null) {
throw new ArgumentNullException(nameof(target));
}
return CopyInternal(target, GetSourceMap(source), propertiesToCopy, ignoreProperties);
}
/// <summary>
/// Copies the specified source.
/// </summary>
/// <param name="source">The source.</param>
/// <param name="target">The target.</param>
/// <param name="propertiesToCopy">The properties to copy.</param>
/// <param name="ignoreProperties">The ignore properties.</param>
/// <returns>
/// Copied properties count.
/// </returns>
/// <exception cref="ArgumentNullException">
/// source
/// or
/// target.
/// </exception>
public static Int32 Copy(IDictionary<String, Object> source, Object? target, IEnumerable<String>? propertiesToCopy = null, params String[] ignoreProperties) {
if(source == null) {
throw new ArgumentNullException(nameof(source));
}
if(target == null) {
throw new ArgumentNullException(nameof(target));
}
return CopyInternal(target, source.ToDictionary(x => x.Key.ToLowerInvariant(), x => Tuple.Create(typeof(Object), x.Value)), propertiesToCopy, ignoreProperties);
}
/// <summary>
/// Creates the map.
/// </summary>
/// <typeparam name="TSource">The type of the source.</typeparam>
/// <typeparam name="TDestination">The type of the destination.</typeparam>
/// <returns>
/// An object map representation of type of the destination property
/// and type of the source property.
/// </returns>
/// <exception cref="InvalidOperationException">
/// You can't create an existing map
/// or
/// Types doesn't match.
/// </exception>
public ObjectMap<TSource, TDestination> CreateMap<TSource, TDestination>() {
if(this._maps.Any(x => x.SourceType == typeof(TSource) && x.DestinationType == typeof(TDestination))) {
throw new InvalidOperationException("You can't create an existing map");
}
IEnumerable<PropertyInfo> sourceType = PropertyTypeCache.DefaultCache.Value.RetrieveAllProperties<TSource>(true);
IEnumerable<PropertyInfo> destinationType = PropertyTypeCache.DefaultCache.Value.RetrieveAllProperties<TDestination>(true);
PropertyInfo[] intersect = sourceType.Intersect(destinationType, new PropertyInfoComparer()).ToArray();
if(!intersect.Any()) {
throw new InvalidOperationException("Types doesn't match");
}
ObjectMap<TSource, TDestination> map = new ObjectMap<TSource, TDestination>(intersect);
this._maps.Add(map);
return map;
}
/// <summary>
/// Maps the specified source.
/// </summary>
/// <typeparam name="TDestination">The type of the destination.</typeparam>
/// <param name="source">The source.</param>
/// <param name="autoResolve">if set to <c>true</c> [automatic resolve].</param>
/// <returns>
/// A new instance of the map.
/// </returns>
/// <exception cref="ArgumentNullException">source.</exception>
/// <exception cref="InvalidOperationException">You can't map from type {source.GetType().Name} to {typeof(TDestination).Name}.</exception>
public TDestination Map<TDestination>(Object source, Boolean autoResolve = true) {
if(source == null) {
throw new ArgumentNullException(nameof(source));
}
TDestination destination = Activator.CreateInstance<TDestination>();
IObjectMap map = this._maps.FirstOrDefault(x => x.SourceType == source.GetType() && x.DestinationType == typeof(TDestination));
if(map != null) {
foreach(KeyValuePair<PropertyInfo, List<PropertyInfo>> property in map.Map) {
Object finalSource = property.Value.Aggregate(source, (current, sourceProperty) => sourceProperty.GetValue(current)!);
property.Key.SetValue(destination, finalSource);
}
} else {
if(!autoResolve) {
throw new InvalidOperationException($"You can't map from type {source.GetType().Name} to {typeof(TDestination).Name}");
}
// Missing mapping, try to use default behavior
_ = Copy(source, destination);
}
return destination;
}
private static Int32 CopyInternal(Object target, Dictionary<String, Tuple<Type, Object>> sourceProperties, IEnumerable<String>? propertiesToCopy, IEnumerable<String>? ignoreProperties) {
// Filter properties
IEnumerable<String>? requiredProperties = propertiesToCopy?.Where(p => !String.IsNullOrWhiteSpace(p)).Select(p => p.ToLowerInvariant());
IEnumerable<String>? ignoredProperties = ignoreProperties?.Where(p => !String.IsNullOrWhiteSpace(p)).Select(p => p.ToLowerInvariant());
IEnumerable<PropertyInfo> properties = PropertyTypeCache.DefaultCache.Value.RetrieveFilteredProperties(target.GetType(), true, x => x.CanWrite);
return properties.Select(x => x.Name).Distinct().ToDictionary(x => x.ToLowerInvariant(), x => properties.First(y => y.Name == x)).Where(x => sourceProperties.Keys.Contains(x.Key)).When(() => requiredProperties != null, q => q.Where(y => requiredProperties.Contains(y.Key))).When(() => ignoredProperties != null, q => q.Where(y => !ignoredProperties.Contains(y.Key))).ToDictionary(x => x.Value, x => sourceProperties[x.Key]).Sum(x => TrySetValue(x.Key, x.Value, target) ? 1 : 0);
}
private static Boolean TrySetValue(PropertyInfo propertyInfo, Tuple<Type, Object> property, Object target) {
try {
(Type type, Object value) = property;
if(type.IsEnum) {
propertyInfo.SetValue(target,
Enum.ToObject(propertyInfo.PropertyType, value));
return true;
}
if(type.IsValueType || propertyInfo.PropertyType != type) {
return propertyInfo.TrySetBasicType(value, target);
}
if(propertyInfo.PropertyType.IsArray) {
_ = propertyInfo.TrySetArray(value as IEnumerable<Object>, target);
return true;
}
propertyInfo.SetValue(target, GetValue(value, propertyInfo.PropertyType));
return true;
} catch {
// swallow
}
return false;
}
private static Object? GetValue(Object source, Type targetType) {
if(source == null) {
return null;
}
Object? target = null;
source.CreateTarget(targetType, false, ref target);
switch(source) {
case String _:
target = source;
break;
case IList sourceList when target is IList targetList:
MethodInfo addMethod = targetType.GetMethods().FirstOrDefault(m => m.Name == Formatters.Json.AddMethodName && m.IsPublic && m.GetParameters().Length == 1);
if(addMethod == null) {
return target;
}
Boolean? isItemValueType = targetList.GetType().GetElementType()?.IsValueType;
foreach(Object? item in sourceList) {
try {
if(isItemValueType != null) {
_ = targetList.Add((Boolean)isItemValueType ? item : item?.CopyPropertiesToNew<Object>());
}
} catch {
// ignored
}
}
break;
default:
_ = source.CopyPropertiesTo(target);
break;
}
return target;
}
private static Dictionary<String, Tuple<Type, Object>> GetSourceMap(Object source) {
// select distinct properties because they can be duplicated by inheritance
PropertyInfo[] sourceProperties = PropertyTypeCache.DefaultCache.Value.RetrieveFilteredProperties(source.GetType(), true, x => x.CanRead).ToArray();
return sourceProperties.Select(x => x.Name).Distinct().ToDictionary(x => x.ToLowerInvariant(), x => Tuple.Create(sourceProperties.First(y => y.Name == x).PropertyType, sourceProperties.First(y => y.Name == x).GetValue(source)))!;
}
}
}

View File

@ -5,189 +5,176 @@ using System.Linq;
using System.Reflection;
using Swan.Reflection;
namespace Swan
{
namespace Swan {
/// <summary>
/// Represents a quick object comparer using the public properties of an object
/// or the public members in a structure.
/// </summary>
public static class ObjectComparer {
/// <summary>
/// Represents a quick object comparer using the public properties of an object
/// or the public members in a structure.
/// Compare if two variables of the same type are equal.
/// </summary>
public static class ObjectComparer
{
/// <summary>
/// Compare if two variables of the same type are equal.
/// </summary>
/// <typeparam name="T">The type of objects to compare.</typeparam>
/// <param name="left">The left.</param>
/// <param name="right">The right.</param>
/// <returns><c>true</c> if the variables are equal; otherwise, <c>false</c>.</returns>
public static bool AreEqual<T>(T left, T right) => AreEqual(left, right, typeof(T));
/// <summary>
/// Compare if two variables of the same type are equal.
/// </summary>
/// <param name="left">The left.</param>
/// <param name="right">The right.</param>
/// <param name="targetType">Type of the target.</param>
/// <returns>
/// <c>true</c> if the variables are equal; otherwise, <c>false</c>.
/// </returns>
/// <exception cref="ArgumentNullException">targetType.</exception>
public static bool AreEqual(object left, object right, Type targetType)
{
if (targetType == null)
throw new ArgumentNullException(nameof(targetType));
if (Definitions.BasicTypesInfo.Value.ContainsKey(targetType))
return Equals(left, right);
return targetType.IsValueType || targetType.IsArray
? AreStructsEqual(left, right, targetType)
: AreObjectsEqual(left, right, targetType);
}
/// <summary>
/// Compare if two objects of the same type are equal.
/// </summary>
/// <typeparam name="T">The type of objects to compare.</typeparam>
/// <param name="left">The left.</param>
/// <param name="right">The right.</param>
/// <returns><c>true</c> if the objects are equal; otherwise, <c>false</c>.</returns>
public static bool AreObjectsEqual<T>(T left, T right)
where T : class
{
return AreObjectsEqual(left, right, typeof(T));
}
/// <summary>
/// Compare if two objects of the same type are equal.
/// </summary>
/// <param name="left">The left.</param>
/// <param name="right">The right.</param>
/// <param name="targetType">Type of the target.</param>
/// <returns><c>true</c> if the objects are equal; otherwise, <c>false</c>.</returns>
/// <exception cref="ArgumentNullException">targetType.</exception>
public static bool AreObjectsEqual(object left, object right, Type targetType)
{
if (targetType == null)
throw new ArgumentNullException(nameof(targetType));
var properties = PropertyTypeCache.DefaultCache.Value.RetrieveAllProperties(targetType).ToArray();
foreach (var propertyTarget in properties)
{
var targetPropertyGetMethod = propertyTarget.GetCacheGetMethod();
if (propertyTarget.PropertyType.IsArray)
{
var leftObj = targetPropertyGetMethod(left) as IEnumerable;
var rightObj = targetPropertyGetMethod(right) as IEnumerable;
if (!AreEnumerationsEquals(leftObj, rightObj))
return false;
}
else
{
if (!Equals(targetPropertyGetMethod(left), targetPropertyGetMethod(right)))
return false;
}
}
return true;
}
/// <summary>
/// Compare if two structures of the same type are equal.
/// </summary>
/// <typeparam name="T">The type of structs to compare.</typeparam>
/// <param name="left">The left.</param>
/// <param name="right">The right.</param>
/// <returns><c>true</c> if the structs are equal; otherwise, <c>false</c>.</returns>
public static bool AreStructsEqual<T>(T left, T right)
where T : struct
{
return AreStructsEqual(left, right, typeof(T));
}
/// <summary>
/// Compare if two structures of the same type are equal.
/// </summary>
/// <param name="left">The left.</param>
/// <param name="right">The right.</param>
/// <param name="targetType">Type of the target.</param>
/// <returns>
/// <c>true</c> if the structs are equal; otherwise, <c>false</c>.
/// </returns>
/// <exception cref="ArgumentNullException">targetType.</exception>
public static bool AreStructsEqual(object left, object right, Type targetType)
{
if (targetType == null)
throw new ArgumentNullException(nameof(targetType));
var fields = new List<MemberInfo>(FieldTypeCache.DefaultCache.Value.RetrieveAllFields(targetType))
.Union(PropertyTypeCache.DefaultCache.Value.RetrieveAllProperties(targetType));
foreach (var targetMember in fields)
{
switch (targetMember)
{
case FieldInfo field:
if (Equals(field.GetValue(left), field.GetValue(right)) == false)
return false;
break;
case PropertyInfo property:
var targetPropertyGetMethod = property.GetCacheGetMethod();
if (targetPropertyGetMethod != null &&
!Equals(targetPropertyGetMethod(left), targetPropertyGetMethod(right)))
return false;
break;
}
}
return true;
}
/// <summary>
/// Compare if two enumerables are equal.
/// </summary>
/// <typeparam name="T">The type of enums to compare.</typeparam>
/// <param name="left">The left.</param>
/// <param name="right">The right.</param>
/// <returns>
/// <c>true</c> if two specified types are equal; otherwise, <c>false</c>.
/// </returns>
/// <exception cref="ArgumentNullException">
/// left
/// or
/// right.
/// </exception>
public static bool AreEnumerationsEquals<T>(T left, T right)
where T : IEnumerable
{
if (Equals(left, default(T)))
throw new ArgumentNullException(nameof(left));
if (Equals(right, default(T)))
throw new ArgumentNullException(nameof(right));
var leftEnumerable = left.Cast<object>().ToArray();
var rightEnumerable = right.Cast<object>().ToArray();
if (leftEnumerable.Length != rightEnumerable.Length)
return false;
for (var i = 0; i < leftEnumerable.Length; i++)
{
var leftEl = leftEnumerable[i];
var rightEl = rightEnumerable[i];
if (!AreEqual(leftEl, rightEl, leftEl.GetType()))
{
return false;
}
}
return true;
}
}
/// <typeparam name="T">The type of objects to compare.</typeparam>
/// <param name="left">The left.</param>
/// <param name="right">The right.</param>
/// <returns><c>true</c> if the variables are equal; otherwise, <c>false</c>.</returns>
public static Boolean AreEqual<T>(T left, T right) => AreEqual(left, right, typeof(T));
/// <summary>
/// Compare if two variables of the same type are equal.
/// </summary>
/// <param name="left">The left.</param>
/// <param name="right">The right.</param>
/// <param name="targetType">Type of the target.</param>
/// <returns>
/// <c>true</c> if the variables are equal; otherwise, <c>false</c>.
/// </returns>
/// <exception cref="ArgumentNullException">targetType.</exception>
public static Boolean AreEqual(Object left, Object right, Type targetType) {
if(targetType == null) {
throw new ArgumentNullException(nameof(targetType));
}
if(Definitions.BasicTypesInfo.Value.ContainsKey(targetType)) {
return Equals(left, right);
}
return targetType.IsValueType || targetType.IsArray ? AreStructsEqual(left, right, targetType) : AreObjectsEqual(left, right, targetType);
}
/// <summary>
/// Compare if two objects of the same type are equal.
/// </summary>
/// <typeparam name="T">The type of objects to compare.</typeparam>
/// <param name="left">The left.</param>
/// <param name="right">The right.</param>
/// <returns><c>true</c> if the objects are equal; otherwise, <c>false</c>.</returns>
public static Boolean AreObjectsEqual<T>(T left, T right) where T : class => AreObjectsEqual(left, right, typeof(T));
/// <summary>
/// Compare if two objects of the same type are equal.
/// </summary>
/// <param name="left">The left.</param>
/// <param name="right">The right.</param>
/// <param name="targetType">Type of the target.</param>
/// <returns><c>true</c> if the objects are equal; otherwise, <c>false</c>.</returns>
/// <exception cref="ArgumentNullException">targetType.</exception>
public static Boolean AreObjectsEqual(Object left, Object right, Type targetType) {
if(targetType == null) {
throw new ArgumentNullException(nameof(targetType));
}
PropertyInfo[] properties = PropertyTypeCache.DefaultCache.Value.RetrieveAllProperties(targetType).ToArray();
foreach(PropertyInfo propertyTarget in properties) {
Func<Object, Object> targetPropertyGetMethod = propertyTarget.GetCacheGetMethod();
if(propertyTarget.PropertyType.IsArray) {
IEnumerable leftObj = targetPropertyGetMethod(left) as IEnumerable;
IEnumerable rightObj = targetPropertyGetMethod(right) as IEnumerable;
if(!AreEnumerationsEquals(leftObj, rightObj)) {
return false;
}
} else {
if(!Equals(targetPropertyGetMethod(left), targetPropertyGetMethod(right))) {
return false;
}
}
}
return true;
}
/// <summary>
/// Compare if two structures of the same type are equal.
/// </summary>
/// <typeparam name="T">The type of structs to compare.</typeparam>
/// <param name="left">The left.</param>
/// <param name="right">The right.</param>
/// <returns><c>true</c> if the structs are equal; otherwise, <c>false</c>.</returns>
public static Boolean AreStructsEqual<T>(T left, T right) where T : struct => AreStructsEqual(left, right, typeof(T));
/// <summary>
/// Compare if two structures of the same type are equal.
/// </summary>
/// <param name="left">The left.</param>
/// <param name="right">The right.</param>
/// <param name="targetType">Type of the target.</param>
/// <returns>
/// <c>true</c> if the structs are equal; otherwise, <c>false</c>.
/// </returns>
/// <exception cref="ArgumentNullException">targetType.</exception>
public static Boolean AreStructsEqual(Object left, Object right, Type targetType) {
if(targetType == null) {
throw new ArgumentNullException(nameof(targetType));
}
IEnumerable<MemberInfo> fields = new List<MemberInfo>(FieldTypeCache.DefaultCache.Value.RetrieveAllFields(targetType)).Union(PropertyTypeCache.DefaultCache.Value.RetrieveAllProperties(targetType));
foreach(MemberInfo targetMember in fields) {
switch(targetMember) {
case FieldInfo field:
if(Equals(field.GetValue(left), field.GetValue(right)) == false) {
return false;
}
break;
case PropertyInfo property:
Func<Object, Object> targetPropertyGetMethod = property.GetCacheGetMethod();
if(targetPropertyGetMethod != null &&
!Equals(targetPropertyGetMethod(left), targetPropertyGetMethod(right))) {
return false;
}
break;
}
}
return true;
}
/// <summary>
/// Compare if two enumerables are equal.
/// </summary>
/// <typeparam name="T">The type of enums to compare.</typeparam>
/// <param name="left">The left.</param>
/// <param name="right">The right.</param>
/// <returns>
/// <c>true</c> if two specified types are equal; otherwise, <c>false</c>.
/// </returns>
/// <exception cref="ArgumentNullException">
/// left
/// or
/// right.
/// </exception>
public static Boolean AreEnumerationsEquals<T>(T left, T right) where T : IEnumerable {
if(Equals(left, default(T))) {
throw new ArgumentNullException(nameof(left));
}
if(Equals(right, default(T))) {
throw new ArgumentNullException(nameof(right));
}
Object[] leftEnumerable = left.Cast<Object>().ToArray();
Object[] rightEnumerable = right.Cast<Object>().ToArray();
if(leftEnumerable.Length != rightEnumerable.Length) {
return false;
}
for(Int32 i = 0; i < leftEnumerable.Length; i++) {
Object leftEl = leftEnumerable[i];
Object rightEl = rightEnumerable[i];
if(!AreEqual(leftEl, rightEl, leftEl.GetType())) {
return false;
}
}
return true;
}
}
}

View File

@ -1,99 +1,85 @@
using System;
namespace Swan
{
namespace Swan {
/// <summary>
/// A utility class to compute paging or batching offsets.
/// </summary>
public class Paginator {
/// <summary>
/// A utility class to compute paging or batching offsets.
/// Initializes a new instance of the <see cref="Paginator" /> class.
/// </summary>
public class Paginator
{
/// <summary>
/// Initializes a new instance of the <see cref="Paginator" /> class.
/// </summary>
/// <param name="totalCount">The total count of items to page over.</param>
/// <param name="pageSize">The desired size of individual pages.</param>
public Paginator(int totalCount, int pageSize)
{
TotalCount = totalCount;
PageSize = pageSize;
PageCount = ComputePageCount();
}
/// <summary>
/// Gets the desired number of items per page.
/// </summary>
public int PageSize { get; }
/// <summary>
/// Gets the total number of items to page over.
/// </summary>
public int TotalCount { get; }
/// <summary>
/// Gets the computed number of pages.
/// </summary>
public int PageCount { get; }
/// <summary>
/// Gets the start item index of the given page.
/// </summary>
/// <param name="pageIndex">Zero-based index of the page.</param>
/// <returns>The start item index.</returns>
public int GetFirstItemIndex(int pageIndex)
{
pageIndex = FixPageIndex(pageIndex);
return pageIndex * PageSize;
}
/// <summary>
/// Gets the end item index of the given page.
/// </summary>
/// <param name="pageIndex">Zero-based index of the page.</param>
/// <returns>The end item index.</returns>
public int GetLastItemIndex(int pageIndex)
{
var startIndex = GetFirstItemIndex(pageIndex);
return Math.Min(startIndex + PageSize - 1, TotalCount - 1);
}
/// <summary>
/// Gets the item count of the given page index.
/// </summary>
/// <param name="pageIndex">Zero-based index of the page.</param>
/// <returns>The number of items that the page contains.</returns>
public int GetItemCount(int pageIndex)
{
pageIndex = FixPageIndex(pageIndex);
return (pageIndex >= PageCount - 1)
? GetLastItemIndex(pageIndex) - GetFirstItemIndex(pageIndex) + 1
: PageSize;
}
/// <summary>
/// Fixes the index of the page by applying bound logic.
/// </summary>
/// <param name="pageIndex">Index of the page.</param>
/// <returns>A limit-bound index.</returns>
private int FixPageIndex(int pageIndex)
{
if (pageIndex < 0) return 0;
return pageIndex >= PageCount ? PageCount - 1 : pageIndex;
}
/// <summary>
/// Computes the number of pages for the paginator.
/// </summary>
/// <returns>The page count.</returns>
private int ComputePageCount()
{
// include this if when you always want at least 1 page
if (TotalCount == 0)
return 0;
return TotalCount % PageSize != 0
? (TotalCount / PageSize) + 1
: TotalCount / PageSize;
}
}
/// <param name="totalCount">The total count of items to page over.</param>
/// <param name="pageSize">The desired size of individual pages.</param>
public Paginator(Int32 totalCount, Int32 pageSize) {
this.TotalCount = totalCount;
this.PageSize = pageSize;
this.PageCount = this.ComputePageCount();
}
/// <summary>
/// Gets the desired number of items per page.
/// </summary>
public Int32 PageSize {
get;
}
/// <summary>
/// Gets the total number of items to page over.
/// </summary>
public Int32 TotalCount {
get;
}
/// <summary>
/// Gets the computed number of pages.
/// </summary>
public Int32 PageCount {
get;
}
/// <summary>
/// Gets the start item index of the given page.
/// </summary>
/// <param name="pageIndex">Zero-based index of the page.</param>
/// <returns>The start item index.</returns>
public Int32 GetFirstItemIndex(Int32 pageIndex) {
pageIndex = this.FixPageIndex(pageIndex);
return pageIndex * this.PageSize;
}
/// <summary>
/// Gets the end item index of the given page.
/// </summary>
/// <param name="pageIndex">Zero-based index of the page.</param>
/// <returns>The end item index.</returns>
public Int32 GetLastItemIndex(Int32 pageIndex) {
Int32 startIndex = this.GetFirstItemIndex(pageIndex);
return Math.Min(startIndex + this.PageSize - 1, this.TotalCount - 1);
}
/// <summary>
/// Gets the item count of the given page index.
/// </summary>
/// <param name="pageIndex">Zero-based index of the page.</param>
/// <returns>The number of items that the page contains.</returns>
public Int32 GetItemCount(Int32 pageIndex) {
pageIndex = this.FixPageIndex(pageIndex);
return (pageIndex >= this.PageCount - 1) ? this.GetLastItemIndex(pageIndex) - this.GetFirstItemIndex(pageIndex) + 1 : this.PageSize;
}
/// <summary>
/// Fixes the index of the page by applying bound logic.
/// </summary>
/// <param name="pageIndex">Index of the page.</param>
/// <returns>A limit-bound index.</returns>
private Int32 FixPageIndex(Int32 pageIndex) => pageIndex < 0 ? 0 : pageIndex >= this.PageCount ? this.PageCount - 1 : pageIndex;
/// <summary>
/// Computes the number of pages for the paginator.
/// </summary>
/// <returns>The page count.</returns>
private Int32 ComputePageCount() =>
// include this if when you always want at least 1 page
this.TotalCount == 0 ? 0 : this.TotalCount % this.PageSize != 0 ? (this.TotalCount / this.PageSize) + 1 : this.TotalCount / this.PageSize;
}
}

View File

@ -1,102 +1,105 @@
using System;
namespace Swan.Parsers
{
namespace Swan.Parsers {
/// <summary>
/// Models an option specification.
/// Based on CommandLine (Copyright 2005-2015 Giacomo Stelluti Scala and Contributors.).
/// </summary>
[AttributeUsage(AttributeTargets.Property)]
public sealed class ArgumentOptionAttribute
: Attribute {
/// <summary>
/// Models an option specification.
/// Based on CommandLine (Copyright 2005-2015 Giacomo Stelluti Scala and Contributors.).
/// Initializes a new instance of the <see cref="ArgumentOptionAttribute"/> class.
/// The default long name will be inferred from target property.
/// </summary>
[AttributeUsage(AttributeTargets.Property)]
public sealed class ArgumentOptionAttribute
: Attribute
{
/// <summary>
/// Initializes a new instance of the <see cref="ArgumentOptionAttribute"/> class.
/// The default long name will be inferred from target property.
/// </summary>
public ArgumentOptionAttribute()
: this(string.Empty, string.Empty)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="ArgumentOptionAttribute"/> class.
/// </summary>
/// <param name="longName">The long name of the option.</param>
public ArgumentOptionAttribute(string longName)
: this(string.Empty, longName)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="ArgumentOptionAttribute"/> class.
/// </summary>
/// <param name="shortName">The short name of the option.</param>
/// <param name="longName">The long name of the option or null if not used.</param>
public ArgumentOptionAttribute(char shortName, string longName)
: this(new string(shortName, 1), longName)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="ArgumentOptionAttribute"/> class.
/// </summary>
/// <param name="shortName">The short name of the option..</param>
public ArgumentOptionAttribute(char shortName)
: this(new string(shortName, 1), string.Empty)
{
}
private ArgumentOptionAttribute(string shortName, string longName)
{
ShortName = shortName ?? throw new ArgumentNullException(nameof(shortName));
LongName = longName ?? throw new ArgumentNullException(nameof(longName));
}
/// <summary>
/// Gets long name of this command line option. This name is usually a single English word.
/// </summary>
/// <value>
/// The long name.
/// </value>
public string LongName { get; }
/// <summary>
/// Gets a short name of this command line option, made of one character.
/// </summary>
/// <value>
/// The short name.
/// </value>
public string ShortName { get; }
/// <summary>
/// When applying attribute to <see cref="System.Collections.Generic.IEnumerable{T}"/> target properties,
/// it allows you to split an argument and consume its content as a sequence.
/// </summary>
public char Separator { get; set; } = '\0';
/// <summary>
/// Gets or sets mapped property default value.
/// </summary>
/// <value>
/// The default value.
/// </value>
public object DefaultValue { get; set; }
/// <summary>
/// Gets or sets a value indicating whether a command line option is required.
/// </summary>
/// <value>
/// <c>true</c> if required; otherwise, <c>false</c>.
/// </value>
public bool Required { get; set; }
/// <summary>
/// Gets or sets a short description of this command line option. Usually a sentence summary.
/// </summary>
/// <value>
/// The help text.
/// </value>
public string HelpText { get; set; }
}
public ArgumentOptionAttribute()
: this(String.Empty, String.Empty) {
}
/// <summary>
/// Initializes a new instance of the <see cref="ArgumentOptionAttribute"/> class.
/// </summary>
/// <param name="longName">The long name of the option.</param>
public ArgumentOptionAttribute(String longName)
: this(String.Empty, longName) {
}
/// <summary>
/// Initializes a new instance of the <see cref="ArgumentOptionAttribute"/> class.
/// </summary>
/// <param name="shortName">The short name of the option.</param>
/// <param name="longName">The long name of the option or null if not used.</param>
public ArgumentOptionAttribute(Char shortName, String longName)
: this(new String(shortName, 1), longName) {
}
/// <summary>
/// Initializes a new instance of the <see cref="ArgumentOptionAttribute"/> class.
/// </summary>
/// <param name="shortName">The short name of the option..</param>
public ArgumentOptionAttribute(Char shortName)
: this(new String(shortName, 1), String.Empty) {
}
private ArgumentOptionAttribute(String shortName, String longName) {
this.ShortName = shortName ?? throw new ArgumentNullException(nameof(shortName));
this.LongName = longName ?? throw new ArgumentNullException(nameof(longName));
}
/// <summary>
/// Gets long name of this command line option. This name is usually a single English word.
/// </summary>
/// <value>
/// The long name.
/// </value>
public String LongName {
get;
}
/// <summary>
/// Gets a short name of this command line option, made of one character.
/// </summary>
/// <value>
/// The short name.
/// </value>
public String ShortName {
get;
}
/// <summary>
/// When applying attribute to <see cref="System.Collections.Generic.IEnumerable{T}"/> target properties,
/// it allows you to split an argument and consume its content as a sequence.
/// </summary>
public Char Separator { get; set; } = '\0';
/// <summary>
/// Gets or sets mapped property default value.
/// </summary>
/// <value>
/// The default value.
/// </value>
public Object DefaultValue {
get; set;
}
/// <summary>
/// Gets or sets a value indicating whether a command line option is required.
/// </summary>
/// <value>
/// <c>true</c> if required; otherwise, <c>false</c>.
/// </value>
public Boolean Required {
get; set;
}
/// <summary>
/// Gets or sets a short description of this command line option. Usually a sentence summary.
/// </summary>
/// <value>
/// The help text.
/// </value>
public String HelpText {
get; set;
}
}
}

View File

@ -4,157 +4,126 @@ using System.Linq;
using System.Reflection;
using Swan.Reflection;
namespace Swan.Parsers
{
/// <summary>
/// Provides methods to parse command line arguments.
///
/// Based on CommandLine (Copyright 2005-2015 Giacomo Stelluti Scala and Contributors).
/// </summary>
public partial class ArgumentParser
{
private sealed class Validator
{
private readonly object _instance;
private readonly IEnumerable<string> _args;
private readonly List<PropertyInfo> _updatedList = new List<PropertyInfo>();
private readonly ArgumentParserSettings _settings;
private readonly PropertyInfo[] _properties;
public Validator(
PropertyInfo[] properties,
IEnumerable<string> args,
object instance,
ArgumentParserSettings settings)
{
_args = args;
_instance = instance;
_settings = settings;
_properties = properties;
PopulateInstance();
SetDefaultValues();
GetRequiredList();
}
public List<string> UnknownList { get; } = new List<string>();
public List<string> RequiredList { get; } = new List<string>();
public bool IsValid() => (_settings.IgnoreUnknownArguments || !UnknownList.Any()) && !RequiredList.Any();
public IEnumerable<ArgumentOptionAttribute> GetPropertiesOptions()
=> _properties.Select(p => AttributeCache.DefaultCache.Value.RetrieveOne<ArgumentOptionAttribute>(p))
.Where(x => x != null);
private void GetRequiredList()
{
foreach (var targetProperty in _properties)
{
var optionAttr = AttributeCache.DefaultCache.Value.RetrieveOne<ArgumentOptionAttribute>(targetProperty);
if (optionAttr == null || optionAttr.Required == false)
continue;
if (targetProperty.GetValue(_instance) == null)
{
RequiredList.Add(optionAttr.LongName ?? optionAttr.ShortName);
}
}
}
private void SetDefaultValues()
{
foreach (var targetProperty in _properties.Except(_updatedList))
{
var optionAttr = AttributeCache.DefaultCache.Value.RetrieveOne<ArgumentOptionAttribute>(targetProperty);
var defaultValue = optionAttr?.DefaultValue;
if (defaultValue == null)
continue;
if (SetPropertyValue(targetProperty, defaultValue.ToString(), _instance, optionAttr))
_updatedList.Add(targetProperty);
}
}
private void PopulateInstance()
{
const char dash = '-';
var propertyName = string.Empty;
foreach (var arg in _args)
{
var ignoreSetValue = string.IsNullOrWhiteSpace(propertyName);
if (ignoreSetValue)
{
if (string.IsNullOrWhiteSpace(arg) || arg[0] != dash) continue;
propertyName = arg.Substring(1);
if (!string.IsNullOrWhiteSpace(propertyName) && propertyName[0] == dash)
propertyName = propertyName.Substring(1);
}
var targetProperty = TryGetProperty(propertyName);
if (targetProperty == null)
{
// Skip if the property is not found
UnknownList.Add(propertyName);
continue;
}
if (!ignoreSetValue && SetPropertyValue(targetProperty, arg, _instance))
{
_updatedList.Add(targetProperty);
propertyName = string.Empty;
}
else if (targetProperty.PropertyType == typeof(bool))
{
// If the arg is a boolean property set it to true.
targetProperty.SetValue(_instance, true);
_updatedList.Add(targetProperty);
propertyName = string.Empty;
}
}
if (!string.IsNullOrEmpty(propertyName))
{
UnknownList.Add(propertyName);
}
}
private bool SetPropertyValue(
PropertyInfo targetProperty,
string propertyValueString,
object result,
ArgumentOptionAttribute optionAttr = null)
{
if (!targetProperty.PropertyType.IsEnum)
{
return targetProperty.PropertyType.IsArray
? targetProperty.TrySetArray(propertyValueString.Split(optionAttr?.Separator ?? ','), result)
: targetProperty.TrySetBasicType(propertyValueString, result);
}
var parsedValue = Enum.Parse(
targetProperty.PropertyType,
propertyValueString,
_settings.CaseInsensitiveEnumValues);
targetProperty.SetValue(result, Enum.ToObject(targetProperty.PropertyType, parsedValue));
return true;
}
private PropertyInfo TryGetProperty(string propertyName)
=> _properties.FirstOrDefault(p =>
string.Equals(AttributeCache.DefaultCache.Value.RetrieveOne<ArgumentOptionAttribute>(p)?.LongName, propertyName, _settings.NameComparer) ||
string.Equals(AttributeCache.DefaultCache.Value.RetrieveOne<ArgumentOptionAttribute>(p)?.ShortName, propertyName, _settings.NameComparer));
}
}
namespace Swan.Parsers {
/// <summary>
/// Provides methods to parse command line arguments.
///
/// Based on CommandLine (Copyright 2005-2015 Giacomo Stelluti Scala and Contributors).
/// </summary>
public partial class ArgumentParser {
private sealed class Validator {
private readonly Object _instance;
private readonly IEnumerable<String> _args;
private readonly List<PropertyInfo> _updatedList = new List<PropertyInfo>();
private readonly ArgumentParserSettings _settings;
private readonly PropertyInfo[] _properties;
public Validator(PropertyInfo[] properties, IEnumerable<String> args, Object instance, ArgumentParserSettings settings) {
this._args = args;
this._instance = instance;
this._settings = settings;
this._properties = properties;
this.PopulateInstance();
this.SetDefaultValues();
this.GetRequiredList();
}
public List<String> UnknownList { get; } = new List<String>();
public List<String> RequiredList { get; } = new List<String>();
public Boolean IsValid() => (this._settings.IgnoreUnknownArguments || !this.UnknownList.Any()) && !this.RequiredList.Any();
public IEnumerable<ArgumentOptionAttribute> GetPropertiesOptions() => this._properties.Select(p => AttributeCache.DefaultCache.Value.RetrieveOne<ArgumentOptionAttribute>(p)).Where(x => x != null);
private void GetRequiredList() {
foreach(PropertyInfo targetProperty in this._properties) {
ArgumentOptionAttribute optionAttr = AttributeCache.DefaultCache.Value.RetrieveOne<ArgumentOptionAttribute>(targetProperty);
if(optionAttr == null || optionAttr.Required == false) {
continue;
}
if(targetProperty.GetValue(this._instance) == null) {
this.RequiredList.Add(optionAttr.LongName ?? optionAttr.ShortName);
}
}
}
private void SetDefaultValues() {
foreach(PropertyInfo targetProperty in this._properties.Except(this._updatedList)) {
ArgumentOptionAttribute optionAttr = AttributeCache.DefaultCache.Value.RetrieveOne<ArgumentOptionAttribute>(targetProperty);
Object defaultValue = optionAttr?.DefaultValue;
if(defaultValue == null) {
continue;
}
if(this.SetPropertyValue(targetProperty, defaultValue.ToString(), this._instance, optionAttr)) {
this._updatedList.Add(targetProperty);
}
}
}
private void PopulateInstance() {
const Char dash = '-';
String propertyName = String.Empty;
foreach(String arg in this._args) {
Boolean ignoreSetValue = String.IsNullOrWhiteSpace(propertyName);
if(ignoreSetValue) {
if(String.IsNullOrWhiteSpace(arg) || arg[0] != dash) {
continue;
}
propertyName = arg.Substring(1);
if(!String.IsNullOrWhiteSpace(propertyName) && propertyName[0] == dash) {
propertyName = propertyName.Substring(1);
}
}
PropertyInfo targetProperty = this.TryGetProperty(propertyName);
if(targetProperty == null) {
// Skip if the property is not found
this.UnknownList.Add(propertyName);
continue;
}
if(!ignoreSetValue && this.SetPropertyValue(targetProperty, arg, this._instance)) {
this._updatedList.Add(targetProperty);
propertyName = String.Empty;
} else if(targetProperty.PropertyType == typeof(Boolean)) {
// If the arg is a boolean property set it to true.
targetProperty.SetValue(this._instance, true);
this._updatedList.Add(targetProperty);
propertyName = String.Empty;
}
}
if(!String.IsNullOrEmpty(propertyName)) {
this.UnknownList.Add(propertyName);
}
}
private Boolean SetPropertyValue(PropertyInfo targetProperty, String propertyValueString, Object result, ArgumentOptionAttribute optionAttr = null) {
if(!targetProperty.PropertyType.IsEnum) {
return targetProperty.PropertyType.IsArray ? targetProperty.TrySetArray(propertyValueString.Split(optionAttr?.Separator ?? ','), result) : targetProperty.TrySetBasicType(propertyValueString, result);
}
Object parsedValue = Enum.Parse(targetProperty.PropertyType, propertyValueString, this._settings.CaseInsensitiveEnumValues);
targetProperty.SetValue(result, Enum.ToObject(targetProperty.PropertyType, parsedValue));
return true;
}
private PropertyInfo TryGetProperty(String propertyName) => this._properties.FirstOrDefault(p => String.Equals(AttributeCache.DefaultCache.Value.RetrieveOne<ArgumentOptionAttribute>(p)?.LongName, propertyName, this._settings.NameComparer) || String.Equals(AttributeCache.DefaultCache.Value.RetrieveOne<ArgumentOptionAttribute>(p)?.ShortName, propertyName, this._settings.NameComparer));
}
}
}

View File

@ -1,57 +1,49 @@
using System;
#nullable enable
using System;
using System.Linq;
using System.Reflection;
using Swan.Reflection;
namespace Swan.Parsers
{
/// <summary>
/// Provides methods to parse command line arguments.
/// </summary>
public partial class ArgumentParser
{
private sealed class TypeResolver<T>
{
private readonly string _selectedVerb;
private PropertyInfo[]? _properties;
public TypeResolver(string selectedVerb)
{
_selectedVerb = selectedVerb;
}
public PropertyInfo[]? Properties => _properties?.Any() == true ? _properties : null;
public object? GetOptionsObject(T instance)
{
_properties = PropertyTypeCache.DefaultCache.Value.RetrieveAllProperties<T>(true).ToArray();
if (!_properties.Any(x => x.GetCustomAttributes(typeof(VerbOptionAttribute), false).Any()))
return instance;
var selectedVerb = string.IsNullOrWhiteSpace(_selectedVerb)
? null
: _properties.FirstOrDefault(x =>
AttributeCache.DefaultCache.Value.RetrieveOne<VerbOptionAttribute>(x).Name == _selectedVerb);
if (selectedVerb == null) return null;
var type = instance.GetType();
var verbProperty = type.GetProperty(selectedVerb.Name);
if (verbProperty?.GetValue(instance) == null)
{
var propertyInstance = Activator.CreateInstance(selectedVerb.PropertyType);
verbProperty?.SetValue(instance, propertyInstance);
}
_properties = PropertyTypeCache.DefaultCache.Value.RetrieveAllProperties(selectedVerb.PropertyType, true)
.ToArray();
return verbProperty?.GetValue(instance);
}
}
}
namespace Swan.Parsers {
/// <summary>
/// Provides methods to parse command line arguments.
/// </summary>
public partial class ArgumentParser {
private sealed class TypeResolver<T> {
private readonly String _selectedVerb;
private PropertyInfo[]? _properties;
public TypeResolver(String selectedVerb) => this._selectedVerb = selectedVerb;
public PropertyInfo[]? Properties => this._properties?.Any() == true ? this._properties : null;
public Object? GetOptionsObject(T instance) {
this._properties = PropertyTypeCache.DefaultCache.Value.RetrieveAllProperties<T>(true).ToArray();
if(!this._properties.Any(x => x.GetCustomAttributes(typeof(VerbOptionAttribute), false).Any())) {
return instance;
}
PropertyInfo? selectedVerb = String.IsNullOrWhiteSpace(this._selectedVerb) ? null : this._properties.FirstOrDefault(x => AttributeCache.DefaultCache.Value.RetrieveOne<VerbOptionAttribute>(x).Name == this._selectedVerb);
if(selectedVerb == null) {
return null;
}
Type? type = instance?.GetType();
PropertyInfo? verbProperty = type?.GetProperty(selectedVerb.Name);
if(verbProperty?.GetValue(instance) == null) {
Object? propertyInstance = Activator.CreateInstance(selectedVerb.PropertyType);
verbProperty?.SetValue(instance, propertyInstance);
}
this._properties = PropertyTypeCache.DefaultCache.Value.RetrieveAllProperties(selectedVerb.PropertyType, true).ToArray();
return verbProperty?.GetValue(instance);
}
}
}
}

View File

@ -3,251 +3,240 @@ using System;
using System.Collections.Generic;
using System.Linq;
namespace Swan.Parsers
{
namespace Swan.Parsers {
/// <summary>
/// Provides methods to parse command line arguments.
/// Based on CommandLine (Copyright 2005-2015 Giacomo Stelluti Scala and Contributors.).
/// </summary>
/// <example>
/// The following example shows how to parse CLI arguments into objects.
/// <code>
/// class Example
/// {
/// using System;
/// using Swan.Parsers;
///
/// static void Main(string[] args)
/// {
/// // parse the supplied command-line arguments into the options object
/// var res = Runtime.ArgumentParser.ParseArguments(args, out var options);
/// }
///
/// class Options
/// {
/// [ArgumentOption('v', "verbose", HelpText = "Set verbose mode.")]
/// public bool Verbose { get; set; }
///
/// [ArgumentOption('u', Required = true, HelpText = "Set user name.")]
/// public string Username { get; set; }
///
/// [ArgumentOption('n', "names", Separator = ',',
/// Required = true, HelpText = "A list of files separated by a comma")]
/// public string[] Files { get; set; }
///
/// [ArgumentOption('p', "port", DefaultValue = 22, HelpText = "Set port.")]
/// public int Port { get; set; }
///
/// [ArgumentOption("color", DefaultValue = ConsoleColor.Red,
/// HelpText = "Set a color.")]
/// public ConsoleColor Color { get; set; }
/// }
/// }
/// </code>
/// The following code describes how to parse CLI verbs.
/// <code>
/// class Example2
/// {
/// using Swan;
/// using Swan.Parsers;
///
/// static void Main(string[] args)
/// {
/// // create an instance of the VerbOptions class
/// var options = new VerbOptions();
///
/// // parse the supplied command-line arguments into the options object
/// var res = Runtime.ArgumentParser.ParseArguments(args, options);
///
/// // if there were no errors parsing
/// if (res)
/// {
/// if(options.Run != null)
/// {
/// // run verb was selected
/// }
///
/// if(options.Print != null)
/// {
/// // print verb was selected
/// }
/// }
///
/// // flush all error messages
/// Terminal.Flush();
/// }
///
/// class VerbOptions
/// {
/// [VerbOption("run", HelpText = "Run verb.")]
/// public RunVerbOption Run { get; set; }
///
/// [VerbOption("print", HelpText = "Print verb.")]
/// public PrintVerbOption Print { get; set; }
/// }
///
/// class RunVerbOption
/// {
/// [ArgumentOption('o', "outdir", HelpText = "Output directory",
/// DefaultValue = "", Required = false)]
/// public string OutDir { get; set; }
/// }
///
/// class PrintVerbOption
/// {
/// [ArgumentOption('t', "text", HelpText = "Text to print",
/// DefaultValue = "", Required = false)]
/// public string Text { get; set; }
/// }
/// }
/// </code>
/// </example>
public partial class ArgumentParser {
/// <summary>
/// Provides methods to parse command line arguments.
/// Based on CommandLine (Copyright 2005-2015 Giacomo Stelluti Scala and Contributors.).
/// Initializes a new instance of the <see cref="ArgumentParser"/> class.
/// </summary>
/// <example>
/// The following example shows how to parse CLI arguments into objects.
/// <code>
/// class Example
/// {
/// using System;
/// using Swan.Parsers;
///
/// static void Main(string[] args)
/// {
/// // parse the supplied command-line arguments into the options object
/// var res = Runtime.ArgumentParser.ParseArguments(args, out var options);
/// }
///
/// class Options
/// {
/// [ArgumentOption('v', "verbose", HelpText = "Set verbose mode.")]
/// public bool Verbose { get; set; }
///
/// [ArgumentOption('u', Required = true, HelpText = "Set user name.")]
/// public string Username { get; set; }
///
/// [ArgumentOption('n', "names", Separator = ',',
/// Required = true, HelpText = "A list of files separated by a comma")]
/// public string[] Files { get; set; }
///
/// [ArgumentOption('p', "port", DefaultValue = 22, HelpText = "Set port.")]
/// public int Port { get; set; }
///
/// [ArgumentOption("color", DefaultValue = ConsoleColor.Red,
/// HelpText = "Set a color.")]
/// public ConsoleColor Color { get; set; }
/// }
/// }
/// </code>
/// The following code describes how to parse CLI verbs.
/// <code>
/// class Example2
/// {
/// using Swan;
/// using Swan.Parsers;
///
/// static void Main(string[] args)
/// {
/// // create an instance of the VerbOptions class
/// var options = new VerbOptions();
///
/// // parse the supplied command-line arguments into the options object
/// var res = Runtime.ArgumentParser.ParseArguments(args, options);
///
/// // if there were no errors parsing
/// if (res)
/// {
/// if(options.Run != null)
/// {
/// // run verb was selected
/// }
///
/// if(options.Print != null)
/// {
/// // print verb was selected
/// }
/// }
///
/// // flush all error messages
/// Terminal.Flush();
/// }
///
/// class VerbOptions
/// {
/// [VerbOption("run", HelpText = "Run verb.")]
/// public RunVerbOption Run { get; set; }
///
/// [VerbOption("print", HelpText = "Print verb.")]
/// public PrintVerbOption Print { get; set; }
/// }
///
/// class RunVerbOption
/// {
/// [ArgumentOption('o', "outdir", HelpText = "Output directory",
/// DefaultValue = "", Required = false)]
/// public string OutDir { get; set; }
/// }
///
/// class PrintVerbOption
/// {
/// [ArgumentOption('t', "text", HelpText = "Text to print",
/// DefaultValue = "", Required = false)]
/// public string Text { get; set; }
/// }
/// }
/// </code>
/// </example>
public partial class ArgumentParser
{
/// <summary>
/// Initializes a new instance of the <see cref="ArgumentParser"/> class.
/// </summary>
public ArgumentParser()
: this(new ArgumentParserSettings())
{
}
/// <summary>
/// Initializes a new instance of the <see cref="ArgumentParser" /> class,
/// configurable with <see cref="ArgumentParserSettings" /> using a delegate.
/// </summary>
/// <param name="parseSettings">The parse settings.</param>
public ArgumentParser(ArgumentParserSettings parseSettings)
{
Settings = parseSettings ?? throw new ArgumentNullException(nameof(parseSettings));
}
/// <summary>
/// Gets the current.
/// </summary>
/// <value>
/// The current.
/// </value>
public static ArgumentParser Current { get; } = new ArgumentParser();
/// <summary>
/// Gets the instance that implements <see cref="ArgumentParserSettings" /> in use.
/// </summary>
/// <value>
/// The settings.
/// </value>
public ArgumentParserSettings Settings { get; }
/// <summary>
/// Parses a string array of command line arguments constructing values in an instance of type <typeparamref name="T" />.
/// </summary>
/// <typeparam name="T">The type of the options.</typeparam>
/// <param name="args">The arguments.</param>
/// <param name="instance">The instance.</param>
/// <returns>
/// <c>true</c> if was converted successfully; otherwise, <c>false</c>.
/// </returns>
/// <exception cref="ArgumentNullException">
/// The exception that is thrown when a null reference (Nothing in Visual Basic)
/// is passed to a method that does not accept it as a valid argument.
/// </exception>
/// <exception cref="InvalidOperationException">
/// The exception that is thrown when a method call is invalid for the object's current state.
/// </exception>
public bool ParseArguments<T>(IEnumerable<string> args, out T instance)
{
instance = Activator.CreateInstance<T>();
return ParseArguments(args, instance);
}
/// <summary>
/// Parses a string array of command line arguments constructing values in an instance of type <typeparamref name="T" />.
/// </summary>
/// <typeparam name="T">The type of the options.</typeparam>
/// <param name="args">The arguments.</param>
/// <param name="instance">The instance.</param>
/// <returns>
/// <c>true</c> if was converted successfully; otherwise, <c>false</c>.
/// </returns>
/// <exception cref="ArgumentNullException">
/// The exception that is thrown when a null reference (Nothing in Visual Basic)
/// is passed to a method that does not accept it as a valid argument.
/// </exception>
/// <exception cref="InvalidOperationException">
/// The exception that is thrown when a method call is invalid for the object's current state.
/// </exception>
public bool ParseArguments<T>(IEnumerable<string> args, T instance)
{
if (args == null)
throw new ArgumentNullException(nameof(args));
if (Equals(instance, default(T)))
throw new ArgumentNullException(nameof(instance));
var typeResolver = new TypeResolver<T>(args.FirstOrDefault());
var options = typeResolver.GetOptionsObject(instance);
if (options == null)
{
ReportUnknownVerb<T>();
return false;
}
if (typeResolver.Properties == null)
throw new InvalidOperationException($"Type {typeof(T).Name} is not valid");
var validator = new Validator(typeResolver.Properties, args, options, Settings);
if (validator.IsValid())
return true;
ReportIssues(validator);
return false;
}
private static void ReportUnknownVerb<T>()
{
Terminal.WriteLine("No verb was specified", ConsoleColor.Red);
Terminal.WriteLine("Valid verbs:", ConsoleColor.Cyan);
PropertyTypeCache.DefaultCache.Value
.RetrieveAllProperties<T>(true)
.Select(x => AttributeCache.DefaultCache.Value.RetrieveOne<VerbOptionAttribute>(x))
.Where(x => x != null)
.ToList()
.ForEach(x => Terminal.WriteLine(x.ToString(), ConsoleColor.Cyan));
}
private void ReportIssues(Validator validator)
{
if (Settings.WriteBanner)
Terminal.WriteWelcomeBanner();
var options = validator.GetPropertiesOptions();
foreach (var option in options)
{
Terminal.WriteLine(string.Empty);
// TODO: If Enum list values
var shortName = string.IsNullOrWhiteSpace(option.ShortName) ? string.Empty : $"-{option.ShortName}";
var longName = string.IsNullOrWhiteSpace(option.LongName) ? string.Empty : $"--{option.LongName}";
var comma = string.IsNullOrWhiteSpace(shortName) || string.IsNullOrWhiteSpace(longName)
? string.Empty
: ", ";
var defaultValue = option.DefaultValue == null ? string.Empty : $"(Default: {option.DefaultValue}) ";
Terminal.WriteLine($" {shortName}{comma}{longName}\t\t{defaultValue}{option.HelpText}", ConsoleColor.Cyan);
}
Terminal.WriteLine(string.Empty);
Terminal.WriteLine(" --help\t\tDisplay this help screen.", ConsoleColor.Cyan);
if (validator.UnknownList.Any())
Terminal.WriteLine($"Unknown arguments: {string.Join(", ", validator.UnknownList)}", ConsoleColor.Red);
if (validator.RequiredList.Any())
Terminal.WriteLine($"Required arguments: {string.Join(", ", validator.RequiredList)}", ConsoleColor.Red);
}
}
public ArgumentParser() : this(new ArgumentParserSettings()) {
}
/// <summary>
/// Initializes a new instance of the <see cref="ArgumentParser" /> class,
/// configurable with <see cref="ArgumentParserSettings" /> using a delegate.
/// </summary>
/// <param name="parseSettings">The parse settings.</param>
public ArgumentParser(ArgumentParserSettings parseSettings) => this.Settings = parseSettings ?? throw new ArgumentNullException(nameof(parseSettings));
/// <summary>
/// Gets the current.
/// </summary>
/// <value>
/// The current.
/// </value>
public static ArgumentParser Current { get; } = new ArgumentParser();
/// <summary>
/// Gets the instance that implements <see cref="ArgumentParserSettings" /> in use.
/// </summary>
/// <value>
/// The settings.
/// </value>
public ArgumentParserSettings Settings {
get;
}
/// <summary>
/// Parses a string array of command line arguments constructing values in an instance of type <typeparamref name="T" />.
/// </summary>
/// <typeparam name="T">The type of the options.</typeparam>
/// <param name="args">The arguments.</param>
/// <param name="instance">The instance.</param>
/// <returns>
/// <c>true</c> if was converted successfully; otherwise, <c>false</c>.
/// </returns>
/// <exception cref="ArgumentNullException">
/// The exception that is thrown when a null reference (Nothing in Visual Basic)
/// is passed to a method that does not accept it as a valid argument.
/// </exception>
/// <exception cref="InvalidOperationException">
/// The exception that is thrown when a method call is invalid for the object's current state.
/// </exception>
public Boolean ParseArguments<T>(IEnumerable<String> args, out T instance) {
instance = Activator.CreateInstance<T>();
return this.ParseArguments(args, instance);
}
/// <summary>
/// Parses a string array of command line arguments constructing values in an instance of type <typeparamref name="T" />.
/// </summary>
/// <typeparam name="T">The type of the options.</typeparam>
/// <param name="args">The arguments.</param>
/// <param name="instance">The instance.</param>
/// <returns>
/// <c>true</c> if was converted successfully; otherwise, <c>false</c>.
/// </returns>
/// <exception cref="ArgumentNullException">
/// The exception that is thrown when a null reference (Nothing in Visual Basic)
/// is passed to a method that does not accept it as a valid argument.
/// </exception>
/// <exception cref="InvalidOperationException">
/// The exception that is thrown when a method call is invalid for the object's current state.
/// </exception>
public Boolean ParseArguments<T>(IEnumerable<String> args, T instance) {
if(args == null) {
throw new ArgumentNullException(nameof(args));
}
if(Equals(instance, default(T))) {
throw new ArgumentNullException(nameof(instance));
}
TypeResolver<T> typeResolver = new TypeResolver<T>(args.FirstOrDefault());
Object options = typeResolver.GetOptionsObject(instance);
if(options == null) {
ReportUnknownVerb<T>();
return false;
}
if(typeResolver.Properties == null) {
throw new InvalidOperationException($"Type {typeof(T).Name} is not valid");
}
Validator validator = new Validator(typeResolver.Properties, args, options, this.Settings);
if(validator.IsValid()) {
return true;
}
this.ReportIssues(validator);
return false;
}
private static void ReportUnknownVerb<T>() {
Terminal.WriteLine("No verb was specified", ConsoleColor.Red);
Terminal.WriteLine("Valid verbs:", ConsoleColor.Cyan);
PropertyTypeCache.DefaultCache.Value.RetrieveAllProperties<T>(true).Select(x => AttributeCache.DefaultCache.Value.RetrieveOne<VerbOptionAttribute>(x)).Where(x => x != null).ToList().ForEach(x => Terminal.WriteLine(x.ToString(), ConsoleColor.Cyan));
}
private void ReportIssues(Validator validator) {
if(this.Settings.WriteBanner) {
Terminal.WriteWelcomeBanner();
}
IEnumerable<ArgumentOptionAttribute> options = validator.GetPropertiesOptions();
foreach(ArgumentOptionAttribute option in options) {
Terminal.WriteLine(String.Empty);
// TODO: If Enum list values
String shortName = String.IsNullOrWhiteSpace(option.ShortName) ? String.Empty : $"-{option.ShortName}";
String longName = String.IsNullOrWhiteSpace(option.LongName) ? String.Empty : $"--{option.LongName}";
String comma = String.IsNullOrWhiteSpace(shortName) || String.IsNullOrWhiteSpace(longName) ? String.Empty : ", ";
String defaultValue = option.DefaultValue == null ? String.Empty : $"(Default: {option.DefaultValue}) ";
Terminal.WriteLine($" {shortName}{comma}{longName}\t\t{defaultValue}{option.HelpText}", ConsoleColor.Cyan);
}
Terminal.WriteLine(String.Empty);
Terminal.WriteLine(" --help\t\tDisplay this help screen.", ConsoleColor.Cyan);
if(validator.UnknownList.Any()) {
Terminal.WriteLine($"Unknown arguments: {String.Join(", ", validator.UnknownList)}", ConsoleColor.Red);
}
if(validator.RequiredList.Any()) {
Terminal.WriteLine($"Required arguments: {String.Join(", ", validator.RequiredList)}", ConsoleColor.Red);
}
}
}
}

View File

@ -1,53 +1,51 @@
using System;
namespace Swan.Parsers
{
namespace Swan.Parsers {
/// <summary>
/// Provides settings for <see cref="ArgumentParser"/>.
/// Based on CommandLine (Copyright 2005-2015 Giacomo Stelluti Scala and Contributors.).
/// </summary>
public class ArgumentParserSettings {
/// <summary>
/// Provides settings for <see cref="ArgumentParser"/>.
/// Based on CommandLine (Copyright 2005-2015 Giacomo Stelluti Scala and Contributors.).
/// Gets or sets a value indicating whether [write banner].
/// </summary>
public class ArgumentParserSettings
{
/// <summary>
/// Gets or sets a value indicating whether [write banner].
/// </summary>
/// <value>
/// <c>true</c> if [write banner]; otherwise, <c>false</c>.
/// </value>
public bool WriteBanner { get; set; } = true;
/// <summary>
/// Gets or sets a value indicating whether perform case sensitive comparisons.
/// Note that case insensitivity only applies to <i>parameters</i>, not the values
/// assigned to them (for example, enum parsing).
/// </summary>
/// <value>
/// <c>true</c> if [case sensitive]; otherwise, <c>false</c>.
/// </value>
public bool CaseSensitive { get; set; } = false;
/// <summary>
/// Gets or sets a value indicating whether perform case sensitive comparisons of <i>values</i>.
/// Note that case insensitivity only applies to <i>values</i>, not the parameters.
/// </summary>
/// <value>
/// <c>true</c> if [case insensitive enum values]; otherwise, <c>false</c>.
/// </value>
public bool CaseInsensitiveEnumValues { get; set; } = true;
/// <summary>
/// Gets or sets a value indicating whether the parser shall move on to the next argument and ignore the given argument if it
/// encounter an unknown arguments.
/// </summary>
/// <value>
/// <c>true</c> to allow parsing the arguments with different class options that do not have all the arguments.
/// </value>
/// <remarks>
/// This allows fragmented version class parsing, useful for project with add-on where add-ons also requires command line arguments but
/// when these are unknown by the main program at build time.
/// </remarks>
public bool IgnoreUnknownArguments { get; set; } = true;
internal StringComparison NameComparer => CaseSensitive ? StringComparison.Ordinal : StringComparison.OrdinalIgnoreCase;
}
/// <value>
/// <c>true</c> if [write banner]; otherwise, <c>false</c>.
/// </value>
public Boolean WriteBanner { get; set; } = true;
/// <summary>
/// Gets or sets a value indicating whether perform case sensitive comparisons.
/// Note that case insensitivity only applies to <i>parameters</i>, not the values
/// assigned to them (for example, enum parsing).
/// </summary>
/// <value>
/// <c>true</c> if [case sensitive]; otherwise, <c>false</c>.
/// </value>
public Boolean CaseSensitive { get; set; } = false;
/// <summary>
/// Gets or sets a value indicating whether perform case sensitive comparisons of <i>values</i>.
/// Note that case insensitivity only applies to <i>values</i>, not the parameters.
/// </summary>
/// <value>
/// <c>true</c> if [case insensitive enum values]; otherwise, <c>false</c>.
/// </value>
public Boolean CaseInsensitiveEnumValues { get; set; } = true;
/// <summary>
/// Gets or sets a value indicating whether the parser shall move on to the next argument and ignore the given argument if it
/// encounter an unknown arguments.
/// </summary>
/// <value>
/// <c>true</c> to allow parsing the arguments with different class options that do not have all the arguments.
/// </value>
/// <remarks>
/// This allows fragmented version class parsing, useful for project with add-on where add-ons also requires command line arguments but
/// when these are unknown by the main program at build time.
/// </remarks>
public Boolean IgnoreUnknownArguments { get; set; } = true;
internal StringComparison NameComparer => this.CaseSensitive ? StringComparison.Ordinal : StringComparison.OrdinalIgnoreCase;
}
}

View File

@ -3,115 +3,107 @@ using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
namespace Swan.Parsers
{
namespace Swan.Parsers {
/// <summary>
/// Represents a generic expression parser.
/// </summary>
public abstract class ExpressionParser {
/// <summary>
/// Represents a generic expression parser.
/// Resolves the expression.
/// </summary>
public abstract class ExpressionParser
{
/// <summary>
/// Resolves the expression.
/// </summary>
/// <typeparam name="T">The type of expression result.</typeparam>
/// <param name="tokens">The tokens.</param>
/// <returns>The representation of the expression parsed.</returns>
public virtual T ResolveExpression<T>(IEnumerable<Token> tokens) =>
ResolveExpression<T>(tokens, System.Globalization.CultureInfo.InvariantCulture);
/// <summary>
/// Resolves the expression.
/// </summary>
/// <typeparam name="T">The type of expression result.</typeparam>
/// <param name="tokens">The tokens.</param>
/// <param name="formatProvider">The format provider.</param>
/// <returns>The representation of the expression parsed.</returns>
public virtual T ResolveExpression<T>(IEnumerable<Token> tokens, IFormatProvider formatProvider)
{
var conversion = Expression.Convert(Parse(tokens,formatProvider), typeof(T));
return Expression.Lambda<Func<T>>(conversion).Compile()();
}
/// <summary>
/// Parses the specified tokens.
/// </summary>
/// <param name="tokens">The tokens.</param>
/// <returns>
/// The final expression.
/// </returns>
public virtual Expression Parse(IEnumerable<Token> tokens) =>
Parse(tokens, System.Globalization.CultureInfo.InvariantCulture);
/// <summary>
/// Parses the specified tokens.
/// </summary>
/// <param name="tokens">The tokens.</param>
/// <param name="formatProvider">The format provider.</param>
/// <returns>
/// The final expression.
/// </returns>
public virtual Expression Parse(IEnumerable<Token> tokens, IFormatProvider formatProvider)
{
var expressionStack = new List<Stack<Expression>>();
foreach (var token in tokens)
{
if (expressionStack.Any() == false)
expressionStack.Add(new Stack<Expression>());
switch (token.Type)
{
case TokenType.Wall:
expressionStack.Add(new Stack<Expression>());
break;
case TokenType.Number:
expressionStack.Last().Push(Expression.Constant(Convert.ToDecimal(token.Value, formatProvider)));
break;
case TokenType.Variable:
ResolveVariable(token.Value, expressionStack.Last());
break;
case TokenType.String:
expressionStack.Last().Push(Expression.Constant(token.Value));
break;
case TokenType.Operator:
ResolveOperator(token.Value, expressionStack.Last());
break;
case TokenType.Function:
ResolveFunction(token.Value, expressionStack.Last());
if (expressionStack.Count > 1 && expressionStack.Last().Count == 1)
{
var lastValue = expressionStack.Last().Pop();
expressionStack.Remove(expressionStack.Last());
expressionStack.Last().Push(lastValue);
}
break;
}
}
return expressionStack.Last().Pop();
}
/// <summary>
/// Resolves the variable.
/// </summary>
/// <param name="value">The value.</param>
/// <param name="expressionStack">The expression stack.</param>
public abstract void ResolveVariable(string value, Stack<Expression> expressionStack);
/// <summary>
/// Resolves the operator.
/// </summary>
/// <param name="value">The value.</param>
/// <param name="expressionStack">The expression stack.</param>
public abstract void ResolveOperator(string value, Stack<Expression> expressionStack);
/// <summary>
/// Resolves the function.
/// </summary>
/// <param name="value">The value.</param>
/// <param name="expressionStack">The expression stack.</param>
public abstract void ResolveFunction(string value, Stack<Expression> expressionStack);
}
/// <typeparam name="T">The type of expression result.</typeparam>
/// <param name="tokens">The tokens.</param>
/// <returns>The representation of the expression parsed.</returns>
public virtual T ResolveExpression<T>(IEnumerable<Token> tokens) => this.ResolveExpression<T>(tokens, System.Globalization.CultureInfo.InvariantCulture);
/// <summary>
/// Resolves the expression.
/// </summary>
/// <typeparam name="T">The type of expression result.</typeparam>
/// <param name="tokens">The tokens.</param>
/// <param name="formatProvider">The format provider.</param>
/// <returns>The representation of the expression parsed.</returns>
public virtual T ResolveExpression<T>(IEnumerable<Token> tokens, IFormatProvider formatProvider) {
UnaryExpression conversion = Expression.Convert(this.Parse(tokens, formatProvider), typeof(T));
return Expression.Lambda<Func<T>>(conversion).Compile()();
}
/// <summary>
/// Parses the specified tokens.
/// </summary>
/// <param name="tokens">The tokens.</param>
/// <returns>
/// The final expression.
/// </returns>
public virtual Expression Parse(IEnumerable<Token> tokens) => this.Parse(tokens, System.Globalization.CultureInfo.InvariantCulture);
/// <summary>
/// Parses the specified tokens.
/// </summary>
/// <param name="tokens">The tokens.</param>
/// <param name="formatProvider">The format provider.</param>
/// <returns>
/// The final expression.
/// </returns>
public virtual Expression Parse(IEnumerable<Token> tokens, IFormatProvider formatProvider) {
List<Stack<Expression>> expressionStack = new List<Stack<Expression>>();
foreach(Token token in tokens) {
if(expressionStack.Any() == false) {
expressionStack.Add(new Stack<Expression>());
}
switch(token.Type) {
case TokenType.Wall:
expressionStack.Add(new Stack<Expression>());
break;
case TokenType.Number:
expressionStack.Last().Push(Expression.Constant(Convert.ToDecimal(token.Value, formatProvider)));
break;
case TokenType.Variable:
this.ResolveVariable(token.Value, expressionStack.Last());
break;
case TokenType.String:
expressionStack.Last().Push(Expression.Constant(token.Value));
break;
case TokenType.Operator:
this.ResolveOperator(token.Value, expressionStack.Last());
break;
case TokenType.Function:
this.ResolveFunction(token.Value, expressionStack.Last());
if(expressionStack.Count > 1 && expressionStack.Last().Count == 1) {
Expression lastValue = expressionStack.Last().Pop();
_ = expressionStack.Remove(expressionStack.Last());
expressionStack.Last().Push(lastValue);
}
break;
}
}
return expressionStack.Last().Pop();
}
/// <summary>
/// Resolves the variable.
/// </summary>
/// <param name="value">The value.</param>
/// <param name="expressionStack">The expression stack.</param>
public abstract void ResolveVariable(String value, Stack<Expression> expressionStack);
/// <summary>
/// Resolves the operator.
/// </summary>
/// <param name="value">The value.</param>
/// <param name="expressionStack">The expression stack.</param>
public abstract void ResolveOperator(String value, Stack<Expression> expressionStack);
/// <summary>
/// Resolves the function.
/// </summary>
/// <param name="value">The value.</param>
/// <param name="expressionStack">The expression stack.</param>
public abstract void ResolveFunction(String value, Stack<Expression> expressionStack);
}
}

View File

@ -1,32 +1,38 @@
namespace Swan.Parsers
{
using System;
namespace Swan.Parsers {
/// <summary>
/// Represents an operator with precedence.
/// </summary>
public class Operator {
/// <summary>
/// Represents an operator with precedence.
/// Gets or sets the name.
/// </summary>
public class Operator
{
/// <summary>
/// Gets or sets the name.
/// </summary>
/// <value>
/// The name.
/// </value>
public string Name { get; set; }
/// <summary>
/// Gets or sets the precedence.
/// </summary>
/// <value>
/// The precedence.
/// </value>
public int Precedence { get; set; }
/// <summary>
/// Gets or sets a value indicating whether [right associative].
/// </summary>
/// <value>
/// <c>true</c> if [right associative]; otherwise, <c>false</c>.
/// </value>
public bool RightAssociative { get; set; }
}
/// <value>
/// The name.
/// </value>
public String Name {
get; set;
}
/// <summary>
/// Gets or sets the precedence.
/// </summary>
/// <value>
/// The precedence.
/// </value>
public Int32 Precedence {
get; set;
}
/// <summary>
/// Gets or sets a value indicating whether [right associative].
/// </summary>
/// <value>
/// <c>true</c> if [right associative]; otherwise, <c>false</c>.
/// </value>
public Boolean RightAssociative {
get; set;
}
}
}

View File

@ -1,35 +1,38 @@
namespace Swan.Parsers
{
using System;
namespace Swan.Parsers {
/// <summary>
/// Represents a Token structure.
/// </summary>
public struct Token {
/// <summary>
/// Represents a Token structure.
/// Initializes a new instance of the <see cref="Token"/> struct.
/// </summary>
public struct Token
{
/// <summary>
/// Initializes a new instance of the <see cref="Token"/> struct.
/// </summary>
/// <param name="type">The type.</param>
/// <param name="value">The value.</param>
public Token(TokenType type, string value)
{
Type = type;
Value = type == TokenType.Function || type == TokenType.Operator ? value.ToLowerInvariant() : value;
}
/// <summary>
/// Gets or sets the type.
/// </summary>
/// <value>
/// The type.
/// </value>
public TokenType Type { get; set; }
/// <summary>
/// Gets the value.
/// </summary>
/// <value>
/// The value.
/// </value>
public string Value { get; }
}
/// <param name="type">The type.</param>
/// <param name="value">The value.</param>
public Token(TokenType type, String value) {
this.Type = type;
this.Value = type == TokenType.Function || type == TokenType.Operator ? value.ToLowerInvariant() : value;
}
/// <summary>
/// Gets or sets the type.
/// </summary>
/// <value>
/// The type.
/// </value>
public TokenType Type {
get; set;
}
/// <summary>
/// Gets the value.
/// </summary>
/// <value>
/// The value.
/// </value>
public String Value {
get;
}
}
}

View File

@ -1,48 +1,46 @@
namespace Swan.Parsers
{
namespace Swan.Parsers {
/// <summary>
/// Enums the token types.
/// </summary>
public enum TokenType {
/// <summary>
/// Enums the token types.
/// The number
/// </summary>
public enum TokenType
{
/// <summary>
/// The number
/// </summary>
Number,
/// <summary>
/// The string
/// </summary>
String,
/// <summary>
/// The variable
/// </summary>
Variable,
/// <summary>
/// The function
/// </summary>
Function,
/// <summary>
/// The parenthesis
/// </summary>
Parenthesis,
/// <summary>
/// The operator
/// </summary>
Operator,
/// <summary>
/// The comma
/// </summary>
Comma,
/// <summary>
/// The wall, used to specified the end of argument list of the following function
/// </summary>
Wall,
}
Number,
/// <summary>
/// The string
/// </summary>
String,
/// <summary>
/// The variable
/// </summary>
Variable,
/// <summary>
/// The function
/// </summary>
Function,
/// <summary>
/// The parenthesis
/// </summary>
Parenthesis,
/// <summary>
/// The operator
/// </summary>
Operator,
/// <summary>
/// The comma
/// </summary>
Comma,
/// <summary>
/// The wall, used to specified the end of argument list of the following function
/// </summary>
Wall,
}
}

View File

@ -2,142 +2,138 @@
using System.Collections.Generic;
using System.Linq;
namespace Swan.Parsers
{
namespace Swan.Parsers {
/// <summary>
/// Represents a generic tokenizer.
/// </summary>
public abstract class Tokenizer {
private const Char PeriodChar = '.';
private const Char CommaChar = ',';
private const Char StringQuotedChar = '"';
private const Char OpenFuncChar = '(';
private const Char CloseFuncChar = ')';
private const Char NegativeChar = '-';
private const String OpenFuncStr = "(";
private readonly List<Operator> _operators = new List<Operator>();
/// <summary>
/// Represents a generic tokenizer.
/// Initializes a new instance of the <see cref="Tokenizer"/> class.
/// This constructor will use the following default operators:
///
/// <list type="table">
/// <listheader>
/// <term>Operator</term>
/// <description>Precedence</description>
/// </listheader>
/// <item>
/// <term>=</term>
/// <description>1</description>
/// </item>
/// <item>
/// <term>!=</term>
/// <description>1</description>
/// </item>
/// <item>
/// <term>&gt;</term>
/// <description>2</description>
/// </item>
/// <item>
/// <term>&lt;</term>
/// <description>2</description>
/// </item>
/// <item>
/// <term>&gt;=</term>
/// <description>2</description>
/// </item>
/// <item>
/// <term>&lt;=</term>
/// <description>2</description>
/// </item>
/// <item>
/// <term>+</term>
/// <description>3</description>
/// </item>
/// <item>
/// <term>&amp;</term>
/// <description>3</description>
/// </item>
/// <item>
/// <term>-</term>
/// <description>3</description>
/// </item>
/// <item>
/// <term>*</term>
/// <description>4</description>
/// </item>
/// <item>
/// <term>(backslash)</term>
/// <description>4</description>
/// </item>
/// <item>
/// <term>/</term>
/// <description>4</description>
/// </item>
/// <item>
/// <term>^</term>
/// <description>4</description>
/// </item>
/// </list>
/// </summary>
public abstract class Tokenizer
/// <param name="input">The input.</param>
protected Tokenizer(String input) {
this._operators.AddRange(this.GetDefaultOperators());
this.Tokenize(input);
}
/// <summary>
/// Initializes a new instance of the <see cref="Tokenizer" /> class.
/// </summary>
/// <param name="input">The input.</param>
/// <param name="operators">The operators to use.</param>
protected Tokenizer(String input, IEnumerable<Operator> operators) {
this._operators.AddRange(operators);
this.Tokenize(input);
}
/// <summary>
/// Gets the tokens.
/// </summary>
/// <value>
/// The tokens.
/// </value>
public List<Token> Tokens { get; } = new List<Token>();
/// <summary>
/// Validates the input and return the start index for tokenizer.
/// </summary>
/// <param name="input">The input.</param>
/// <param name="startIndex">The start index.</param>
/// <returns><c>true</c> if the input is valid, otherwise <c>false</c>.</returns>
public abstract Boolean ValidateInput(String input, out Int32 startIndex);
/// <summary>
/// Resolves the type of the function or member.
/// </summary>
/// <param name="input">The input.</param>
/// <returns>The token type.</returns>
public abstract TokenType ResolveFunctionOrMemberType(String input);
/// <summary>
/// Evaluates the function or member.
/// </summary>
/// <param name="input">The input.</param>
/// <param name="position">The position.</param>
/// <returns><c>true</c> if the input is a valid function or variable, otherwise <c>false</c>.</returns>
public virtual Boolean EvaluateFunctionOrMember(String input, Int32 position) => false;
/// <summary>
/// Gets the default operators.
/// </summary>
/// <returns>An array with the operators to use for the tokenizer.</returns>
public virtual Operator[] GetDefaultOperators() => new[]
{
private const char PeriodChar = '.';
private const char CommaChar = ',';
private const char StringQuotedChar = '"';
private const char OpenFuncChar = '(';
private const char CloseFuncChar = ')';
private const char NegativeChar = '-';
private const string OpenFuncStr = "(";
private readonly List<Operator> _operators = new List<Operator>();
/// <summary>
/// Initializes a new instance of the <see cref="Tokenizer"/> class.
/// This constructor will use the following default operators:
///
/// <list type="table">
/// <listheader>
/// <term>Operator</term>
/// <description>Precedence</description>
/// </listheader>
/// <item>
/// <term>=</term>
/// <description>1</description>
/// </item>
/// <item>
/// <term>!=</term>
/// <description>1</description>
/// </item>
/// <item>
/// <term>&gt;</term>
/// <description>2</description>
/// </item>
/// <item>
/// <term>&lt;</term>
/// <description>2</description>
/// </item>
/// <item>
/// <term>&gt;=</term>
/// <description>2</description>
/// </item>
/// <item>
/// <term>&lt;=</term>
/// <description>2</description>
/// </item>
/// <item>
/// <term>+</term>
/// <description>3</description>
/// </item>
/// <item>
/// <term>&amp;</term>
/// <description>3</description>
/// </item>
/// <item>
/// <term>-</term>
/// <description>3</description>
/// </item>
/// <item>
/// <term>*</term>
/// <description>4</description>
/// </item>
/// <item>
/// <term>(backslash)</term>
/// <description>4</description>
/// </item>
/// <item>
/// <term>/</term>
/// <description>4</description>
/// </item>
/// <item>
/// <term>^</term>
/// <description>4</description>
/// </item>
/// </list>
/// </summary>
/// <param name="input">The input.</param>
protected Tokenizer(string input)
{
_operators.AddRange(GetDefaultOperators());
Tokenize(input);
}
/// <summary>
/// Initializes a new instance of the <see cref="Tokenizer" /> class.
/// </summary>
/// <param name="input">The input.</param>
/// <param name="operators">The operators to use.</param>
protected Tokenizer(string input, IEnumerable<Operator> operators)
{
_operators.AddRange(operators);
Tokenize(input);
}
/// <summary>
/// Gets the tokens.
/// </summary>
/// <value>
/// The tokens.
/// </value>
public List<Token> Tokens { get; } = new List<Token>();
/// <summary>
/// Validates the input and return the start index for tokenizer.
/// </summary>
/// <param name="input">The input.</param>
/// <param name="startIndex">The start index.</param>
/// <returns><c>true</c> if the input is valid, otherwise <c>false</c>.</returns>
public abstract bool ValidateInput(string input, out int startIndex);
/// <summary>
/// Resolves the type of the function or member.
/// </summary>
/// <param name="input">The input.</param>
/// <returns>The token type.</returns>
public abstract TokenType ResolveFunctionOrMemberType(string input);
/// <summary>
/// Evaluates the function or member.
/// </summary>
/// <param name="input">The input.</param>
/// <param name="position">The position.</param>
/// <returns><c>true</c> if the input is a valid function or variable, otherwise <c>false</c>.</returns>
public virtual bool EvaluateFunctionOrMember(string input, int position) => false;
/// <summary>
/// Gets the default operators.
/// </summary>
/// <returns>An array with the operators to use for the tokenizer.</returns>
public virtual Operator[] GetDefaultOperators() => new[]
{
new Operator {Name = "=", Precedence = 1},
new Operator {Name = "!=", Precedence = 1},
new Operator {Name = ">", Precedence = 2},
@ -151,211 +147,162 @@ namespace Swan.Parsers
new Operator {Name = "/", Precedence = 4},
new Operator {Name = "\\", Precedence = 4},
new Operator {Name = "^", Precedence = 4},
};
/// <summary>
/// Shunting the yard.
/// </summary>
/// <param name="includeFunctionStopper">if set to <c>true</c> [include function stopper] (Token type <c>Wall</c>).</param>
/// <returns>
/// Enumerable of the token in in.
/// </returns>
/// <exception cref="InvalidOperationException">
/// Wrong token
/// or
/// Mismatched parenthesis.
/// </exception>
public virtual IEnumerable<Token> ShuntingYard(bool includeFunctionStopper = true)
{
var stack = new Stack<Token>();
foreach (var tok in Tokens)
{
switch (tok.Type)
{
case TokenType.Number:
case TokenType.Variable:
case TokenType.String:
yield return tok;
break;
case TokenType.Function:
stack.Push(tok);
break;
case TokenType.Operator:
while (stack.Any() && stack.Peek().Type == TokenType.Operator &&
CompareOperators(tok.Value, stack.Peek().Value))
yield return stack.Pop();
stack.Push(tok);
break;
case TokenType.Comma:
while (stack.Any() && (stack.Peek().Type != TokenType.Comma &&
stack.Peek().Type != TokenType.Parenthesis))
yield return stack.Pop();
break;
case TokenType.Parenthesis:
if (tok.Value == OpenFuncStr)
{
if (stack.Any() && stack.Peek().Type == TokenType.Function)
{
if (includeFunctionStopper)
yield return new Token(TokenType.Wall, tok.Value);
}
stack.Push(tok);
}
else
{
while (stack.Peek().Value != OpenFuncStr)
yield return stack.Pop();
stack.Pop();
if (stack.Any() && stack.Peek().Type == TokenType.Function)
{
yield return stack.Pop();
}
}
break;
default:
throw new InvalidOperationException("Wrong token");
}
}
while (stack.Any())
{
var tok = stack.Pop();
if (tok.Type == TokenType.Parenthesis)
throw new InvalidOperationException("Mismatched parenthesis");
yield return tok;
}
}
private static bool CompareOperators(Operator op1, Operator op2) => op1.RightAssociative
? op1.Precedence < op2.Precedence
: op1.Precedence <= op2.Precedence;
private void Tokenize(string input)
{
if (!ValidateInput(input, out var startIndex))
{
return;
}
for (var i = startIndex; i < input.Length; i++)
{
if (char.IsWhiteSpace(input, i)) continue;
if (input[i] == CommaChar)
{
Tokens.Add(new Token(TokenType.Comma, new string(new[] { input[i] })));
continue;
}
if (input[i] == StringQuotedChar)
{
i = ExtractString(input, i);
continue;
}
if (char.IsLetter(input, i) || EvaluateFunctionOrMember(input, i))
{
i = ExtractFunctionOrMember(input, i);
continue;
}
if (char.IsNumber(input, i) || (
input[i] == NegativeChar &&
((Tokens.Any() && Tokens.Last().Type != TokenType.Number) || !Tokens.Any())))
{
i = ExtractNumber(input, i);
continue;
}
if (input[i] == OpenFuncChar ||
input[i] == CloseFuncChar)
{
Tokens.Add(new Token(TokenType.Parenthesis, new string(new[] { input[i] })));
continue;
}
i = ExtractOperator(input, i);
}
}
private int ExtractData(
string input,
int i,
Func<string, TokenType> tokenTypeEvaluation,
Func<char, bool> evaluation,
int right = 0,
int left = -1)
{
var charCount = 0;
for (var j = i + right; j < input.Length; j++)
{
if (evaluation(input[j]))
break;
charCount++;
}
// Extract and set the value
var value = input.SliceLength(i + right, charCount);
Tokens.Add(new Token(tokenTypeEvaluation(value), value));
i += charCount + left;
return i;
}
private int ExtractOperator(string input, int i) =>
ExtractData(
input,
i,
x => TokenType.Operator,
x => x == OpenFuncChar ||
x == CommaChar ||
x == PeriodChar ||
x == StringQuotedChar ||
char.IsWhiteSpace(x) ||
char.IsNumber(x));
private int ExtractFunctionOrMember(string input, int i) =>
ExtractData(
input,
i,
ResolveFunctionOrMemberType,
x => x == OpenFuncChar ||
x == CloseFuncChar ||
x == CommaChar ||
char.IsWhiteSpace(x));
private int ExtractNumber(string input, int i) =>
ExtractData(
input,
i,
x => TokenType.Number,
x => !char.IsNumber(x) && x != PeriodChar && x != NegativeChar);
private int ExtractString(string input, int i)
{
var length = ExtractData(input, i, x => TokenType.String, x => x == StringQuotedChar, 1, 1);
// open string, report issue
if (length == input.Length && input[length - 1] != StringQuotedChar)
throw new FormatException($"Parser error (Position {i}): Expected '\"' but got '{input[length - 1]}'.");
return length;
}
private bool CompareOperators(string op1, string op2)
=> CompareOperators(GetOperatorOrDefault(op1), GetOperatorOrDefault(op2));
private Operator GetOperatorOrDefault(string op)
=> _operators.FirstOrDefault(x => x.Name == op) ?? new Operator { Name = op, Precedence = 0 };
}
};
/// <summary>
/// Shunting the yard.
/// </summary>
/// <param name="includeFunctionStopper">if set to <c>true</c> [include function stopper] (Token type <c>Wall</c>).</param>
/// <returns>
/// Enumerable of the token in in.
/// </returns>
/// <exception cref="InvalidOperationException">
/// Wrong token
/// or
/// Mismatched parenthesis.
/// </exception>
public virtual IEnumerable<Token> ShuntingYard(Boolean includeFunctionStopper = true) {
Stack<Token> stack = new Stack<Token>();
foreach(Token tok in this.Tokens) {
switch(tok.Type) {
case TokenType.Number:
case TokenType.Variable:
case TokenType.String:
yield return tok;
break;
case TokenType.Function:
stack.Push(tok);
break;
case TokenType.Operator:
while(stack.Any() && stack.Peek().Type == TokenType.Operator && this.CompareOperators(tok.Value, stack.Peek().Value)) {
yield return stack.Pop();
}
stack.Push(tok);
break;
case TokenType.Comma:
while(stack.Any() && stack.Peek().Type != TokenType.Comma && stack.Peek().Type != TokenType.Parenthesis) {
yield return stack.Pop();
}
break;
case TokenType.Parenthesis:
if(tok.Value == OpenFuncStr) {
if(stack.Any() && stack.Peek().Type == TokenType.Function) {
if(includeFunctionStopper) {
yield return new Token(TokenType.Wall, tok.Value);
}
}
stack.Push(tok);
} else {
while(stack.Peek().Value != OpenFuncStr) {
yield return stack.Pop();
}
_ = stack.Pop();
if(stack.Any() && stack.Peek().Type == TokenType.Function) {
yield return stack.Pop();
}
}
break;
default:
throw new InvalidOperationException("Wrong token");
}
}
while(stack.Any()) {
Token tok = stack.Pop();
if(tok.Type == TokenType.Parenthesis) {
throw new InvalidOperationException("Mismatched parenthesis");
}
yield return tok;
}
}
private static Boolean CompareOperators(Operator op1, Operator op2) => op1.RightAssociative ? op1.Precedence < op2.Precedence : op1.Precedence <= op2.Precedence;
private void Tokenize(String input) {
if(!this.ValidateInput(input, out Int32 startIndex)) {
return;
}
for(Int32 i = startIndex; i < input.Length; i++) {
if(Char.IsWhiteSpace(input, i)) {
continue;
}
if(input[i] == CommaChar) {
this.Tokens.Add(new Token(TokenType.Comma, new String(new[] { input[i] })));
continue;
}
if(input[i] == StringQuotedChar) {
i = this.ExtractString(input, i);
continue;
}
if(Char.IsLetter(input, i) || this.EvaluateFunctionOrMember(input, i)) {
i = this.ExtractFunctionOrMember(input, i);
continue;
}
if(Char.IsNumber(input, i) || input[i] == NegativeChar && (this.Tokens.Any() && this.Tokens.Last().Type != TokenType.Number || !this.Tokens.Any())) {
i = this.ExtractNumber(input, i);
continue;
}
if(input[i] == OpenFuncChar || input[i] == CloseFuncChar) {
this.Tokens.Add(new Token(TokenType.Parenthesis, new String(new[] { input[i] })));
continue;
}
i = this.ExtractOperator(input, i);
}
}
private Int32 ExtractData(String input, Int32 i, Func<String, TokenType> tokenTypeEvaluation, Func<Char, Boolean> evaluation, Int32 right = 0, Int32 left = -1) {
Int32 charCount = 0;
for(Int32 j = i + right; j < input.Length; j++) {
if(evaluation(input[j])) {
break;
}
charCount++;
}
// Extract and set the value
String value = input.SliceLength(i + right, charCount);
this.Tokens.Add(new Token(tokenTypeEvaluation(value), value));
i += charCount + left;
return i;
}
private Int32 ExtractOperator(String input, Int32 i) => this.ExtractData(input, i, x => TokenType.Operator, x => x == OpenFuncChar || x == CommaChar || x == PeriodChar || x == StringQuotedChar || Char.IsWhiteSpace(x) || Char.IsNumber(x));
private Int32 ExtractFunctionOrMember(String input, Int32 i) => this.ExtractData(input, i, this.ResolveFunctionOrMemberType, x => x == OpenFuncChar || x == CloseFuncChar || x == CommaChar || Char.IsWhiteSpace(x));
private Int32 ExtractNumber(String input, Int32 i) => this.ExtractData(input, i, x => TokenType.Number, x => !Char.IsNumber(x) && x != PeriodChar && x != NegativeChar);
private Int32 ExtractString(String input, Int32 i) {
Int32 length = this.ExtractData(input, i, x => TokenType.String, x => x == StringQuotedChar, 1, 1);
// open string, report issue
if(length == input.Length && input[length - 1] != StringQuotedChar) {
throw new FormatException($"Parser error (Position {i}): Expected '\"' but got '{input[length - 1]}'.");
}
return length;
}
private Boolean CompareOperators(String op1, String op2) => CompareOperators(this.GetOperatorOrDefault(op1), this.GetOperatorOrDefault(op2));
private Operator GetOperatorOrDefault(String op) => this._operators.FirstOrDefault(x => x.Name == op) ?? new Operator { Name = op, Precedence = 0 };
}
}

View File

@ -1,40 +1,39 @@
using System;
namespace Swan.Parsers
{
namespace Swan.Parsers {
/// <summary>
/// Models a verb option.
/// </summary>
[AttributeUsage(AttributeTargets.Property)]
public sealed class VerbOptionAttribute : Attribute {
/// <summary>
/// Models a verb option.
/// Initializes a new instance of the <see cref="VerbOptionAttribute" /> class.
/// </summary>
[AttributeUsage(AttributeTargets.Property)]
public sealed class VerbOptionAttribute : Attribute
{
/// <summary>
/// Initializes a new instance of the <see cref="VerbOptionAttribute" /> class.
/// </summary>
/// <param name="name">The name.</param>
/// <exception cref="ArgumentNullException">name.</exception>
public VerbOptionAttribute(string name)
{
Name = name ?? throw new ArgumentNullException(nameof(name));
}
/// <summary>
/// Gets the name of the verb option.
/// </summary>
/// <value>
/// Name.
/// </value>
public string Name { get; }
/// <summary>
/// Gets or sets a short description of this command line verb. Usually a sentence summary.
/// </summary>
/// <value>
/// The help text.
/// </value>
public string HelpText { get; set; }
/// <inheritdoc />
public override string ToString() => $" {Name}\t\t{HelpText}";
}
/// <param name="name">The name.</param>
/// <exception cref="ArgumentNullException">name.</exception>
public VerbOptionAttribute(String name) => this.Name = name ?? throw new ArgumentNullException(nameof(name));
/// <summary>
/// Gets the name of the verb option.
/// </summary>
/// <value>
/// Name.
/// </value>
public String Name {
get;
}
/// <summary>
/// Gets or sets a short description of this command line verb. Usually a sentence summary.
/// </summary>
/// <value>
/// The help text.
/// </value>
public String HelpText {
get; set;
}
/// <inheritdoc />
public override String ToString() => $" {this.Name}\t\t{this.HelpText}";
}
}

View File

@ -1,188 +1,161 @@
using System;
#nullable enable
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
namespace Swan.Reflection
{
namespace Swan.Reflection {
/// <summary>
/// A thread-safe cache of attributes belonging to a given key (MemberInfo or Type).
///
/// The Retrieve method is the most useful one in this class as it
/// calls the retrieval process if the type is not contained
/// in the cache.
/// </summary>
public class AttributeCache {
private readonly Lazy<ConcurrentDictionary<Tuple<Object, Type>, IEnumerable<Object>>> _data = new Lazy<ConcurrentDictionary<Tuple<Object, Type>, IEnumerable<Object>>>(() => new ConcurrentDictionary<Tuple<Object, Type>, IEnumerable<Object>>(), true);
/// <summary>
/// A thread-safe cache of attributes belonging to a given key (MemberInfo or Type).
///
/// The Retrieve method is the most useful one in this class as it
/// calls the retrieval process if the type is not contained
/// in the cache.
/// Initializes a new instance of the <see cref="AttributeCache"/> class.
/// </summary>
public class AttributeCache
{
private readonly Lazy<ConcurrentDictionary<Tuple<object, Type>, IEnumerable<object>>> _data =
new Lazy<ConcurrentDictionary<Tuple<object, Type>, IEnumerable<object>>>(() =>
new ConcurrentDictionary<Tuple<object, Type>, IEnumerable<object>>(), true);
/// <summary>
/// Initializes a new instance of the <see cref="AttributeCache"/> class.
/// </summary>
/// <param name="propertyCache">The property cache object.</param>
public AttributeCache(PropertyTypeCache? propertyCache = null)
{
PropertyTypeCache = propertyCache ?? PropertyTypeCache.DefaultCache.Value;
}
/// <summary>
/// Gets the default cache.
/// </summary>
/// <value>
/// The default cache.
/// </value>
public static Lazy<AttributeCache> DefaultCache { get; } = new Lazy<AttributeCache>(() => new AttributeCache());
/// <summary>
/// A PropertyTypeCache object for caching properties and their attributes.
/// </summary>
public PropertyTypeCache PropertyTypeCache { get; }
/// <summary>
/// Determines whether [contains] [the specified member].
/// </summary>
/// <typeparam name="T">The type of the attribute to be retrieved.</typeparam>
/// <param name="member">The member.</param>
/// <returns>
/// <c>true</c> if [contains] [the specified member]; otherwise, <c>false</c>.
/// </returns>
public bool Contains<T>(MemberInfo member) => _data.Value.ContainsKey(new Tuple<object, Type>(member, typeof(T)));
/// <summary>
/// Gets specific attributes from a member constrained to an attribute.
/// </summary>
/// <typeparam name="T">The type of the attribute to be retrieved.</typeparam>
/// <param name="member">The member.</param>
/// <param name="inherit"><c>true</c> to inspect the ancestors of element; otherwise, <c>false</c>.</param>
/// <returns>An array of the attributes stored for the specified type.</returns>
public IEnumerable<object> Retrieve<T>(MemberInfo member, bool inherit = false)
where T : Attribute
{
if (member == null)
throw new ArgumentNullException(nameof(member));
return Retrieve(new Tuple<object, Type>(member, typeof(T)), t => member.GetCustomAttributes<T>(inherit));
}
/// <summary>
/// Gets all attributes of a specific type from a member.
/// </summary>
/// <param name="member">The member.</param>
/// <param name="type">The attribute type.</param>
/// <param name="inherit"><c>true</c> to inspect the ancestors of element; otherwise, <c>false</c>.</param>
/// <returns>An array of the attributes stored for the specified type.</returns>
public IEnumerable<object> Retrieve(MemberInfo member, Type type, bool inherit = false)
{
if (member == null)
throw new ArgumentNullException(nameof(member));
if (type == null)
throw new ArgumentNullException(nameof(type));
return Retrieve(
new Tuple<object, Type>(member, type),
t => member.GetCustomAttributes(type, inherit));
}
/// <summary>
/// Gets one attribute of a specific type from a member.
/// </summary>
/// <typeparam name="T">The attribute type.</typeparam>
/// <param name="member">The member.</param>
/// <param name="inherit"><c>true</c> to inspect the ancestors of element; otherwise, <c>false</c>.</param>
/// <returns>An attribute stored for the specified type.</returns>
public T RetrieveOne<T>(MemberInfo member, bool inherit = false)
where T : Attribute
{
if (member == null)
return default;
var attr = Retrieve(
new Tuple<object, Type>(member, typeof(T)),
t => member.GetCustomAttributes(typeof(T), inherit));
return ConvertToAttribute<T>(attr);
}
/// <summary>
/// Gets one attribute of a specific type from a generic type.
/// </summary>
/// <typeparam name="TAttribute">The type of the attribute.</typeparam>
/// <typeparam name="T">The type to retrieve the attribute.</typeparam>
/// <param name="inherit">if set to <c>true</c> [inherit].</param>
/// <returns>An attribute stored for the specified type.</returns>
public TAttribute RetrieveOne<TAttribute, T>(bool inherit = false)
where TAttribute : Attribute
{
var attr = Retrieve(
new Tuple<object, Type>(typeof(T), typeof(TAttribute)),
t => typeof(T).GetCustomAttributes(typeof(TAttribute), inherit));
return ConvertToAttribute<TAttribute>(attr);
}
/// <summary>
/// Gets all properties an their attributes of a given type constrained to only attributes.
/// </summary>
/// <typeparam name="T">The type of the attribute to retrieve.</typeparam>
/// <param name="type">The type of the object.</param>
/// <param name="inherit"><c>true</c> to inspect the ancestors of element; otherwise, <c>false</c>.</param>
/// <returns>A dictionary of the properties and their attributes stored for the specified type.</returns>
public Dictionary<PropertyInfo, IEnumerable<object>> Retrieve<T>(Type type, bool inherit = false)
where T : Attribute =>
PropertyTypeCache.RetrieveAllProperties(type, true)
.ToDictionary(x => x, x => Retrieve<T>(x, inherit));
/// <summary>
/// Gets all properties and their attributes of a given type.
/// </summary>
/// <typeparam name="T">The object type used to extract the properties from.</typeparam>
/// <typeparam name="TAttribute">The type of the attribute.</typeparam>
/// <param name="inherit"><c>true</c> to inspect the ancestors of element; otherwise, <c>false</c>.</param>
/// <returns>
/// A dictionary of the properties and their attributes stored for the specified type.
/// </returns>
public Dictionary<PropertyInfo, IEnumerable<object>> RetrieveFromType<T, TAttribute>(bool inherit = false)
=> RetrieveFromType<T>(typeof(TAttribute), inherit);
/// <summary>
/// Gets all properties and their attributes of a given type.
/// </summary>
/// <typeparam name="T">The object type used to extract the properties from.</typeparam>
/// <param name="attributeType">Type of the attribute.</param>
/// <param name="inherit"><c>true</c> to inspect the ancestors of element; otherwise, <c>false</c>.</param>
/// <returns>
/// A dictionary of the properties and their attributes stored for the specified type.
/// </returns>
public Dictionary<PropertyInfo, IEnumerable<object>> RetrieveFromType<T>(Type attributeType, bool inherit = false)
{
if (attributeType == null)
throw new ArgumentNullException(nameof(attributeType));
return PropertyTypeCache.RetrieveAllProperties<T>(true)
.ToDictionary(x => x, x => Retrieve(x, attributeType, inherit));
}
private static T ConvertToAttribute<T>(IEnumerable<object> attr)
where T : Attribute
{
if (attr?.Any() != true)
return default;
return attr.Count() == 1
? (T) Convert.ChangeType(attr.First(), typeof(T))
: throw new AmbiguousMatchException("Multiple custom attributes of the same type found.");
}
private IEnumerable<object> Retrieve(Tuple<object, Type> key, Func<Tuple<object, Type>, IEnumerable<object>> factory)
{
if (factory == null)
throw new ArgumentNullException(nameof(factory));
return _data.Value.GetOrAdd(key, k => factory.Invoke(k).Where(item => item != null));
}
}
/// <param name="propertyCache">The property cache object.</param>
public AttributeCache(PropertyTypeCache? propertyCache = null) => this.PropertyTypeCache = propertyCache ?? PropertyTypeCache.DefaultCache.Value;
/// <summary>
/// Gets the default cache.
/// </summary>
/// <value>
/// The default cache.
/// </value>
public static Lazy<AttributeCache> DefaultCache { get; } = new Lazy<AttributeCache>(() => new AttributeCache());
/// <summary>
/// A PropertyTypeCache object for caching properties and their attributes.
/// </summary>
public PropertyTypeCache PropertyTypeCache {
get;
}
/// <summary>
/// Determines whether [contains] [the specified member].
/// </summary>
/// <typeparam name="T">The type of the attribute to be retrieved.</typeparam>
/// <param name="member">The member.</param>
/// <returns>
/// <c>true</c> if [contains] [the specified member]; otherwise, <c>false</c>.
/// </returns>
public Boolean Contains<T>(MemberInfo member) => this._data.Value.ContainsKey(new Tuple<Object, Type>(member, typeof(T)));
/// <summary>
/// Gets specific attributes from a member constrained to an attribute.
/// </summary>
/// <typeparam name="T">The type of the attribute to be retrieved.</typeparam>
/// <param name="member">The member.</param>
/// <param name="inherit"><c>true</c> to inspect the ancestors of element; otherwise, <c>false</c>.</param>
/// <returns>An array of the attributes stored for the specified type.</returns>
public IEnumerable<Object> Retrieve<T>(MemberInfo member, Boolean inherit = false) where T : Attribute {
if(member == null) {
throw new ArgumentNullException(nameof(member));
}
return this.Retrieve(new Tuple<Object, Type>(member, typeof(T)), t => member.GetCustomAttributes<T>(inherit));
}
/// <summary>
/// Gets all attributes of a specific type from a member.
/// </summary>
/// <param name="member">The member.</param>
/// <param name="type">The attribute type.</param>
/// <param name="inherit"><c>true</c> to inspect the ancestors of element; otherwise, <c>false</c>.</param>
/// <returns>An array of the attributes stored for the specified type.</returns>
public IEnumerable<Object> Retrieve(MemberInfo member, Type type, Boolean inherit = false) {
if(member == null) {
throw new ArgumentNullException(nameof(member));
}
if(type == null) {
throw new ArgumentNullException(nameof(type));
}
return this.Retrieve(new Tuple<Object, Type>(member, type), t => member.GetCustomAttributes(type, inherit));
}
/// <summary>
/// Gets one attribute of a specific type from a member.
/// </summary>
/// <typeparam name="T">The attribute type.</typeparam>
/// <param name="member">The member.</param>
/// <param name="inherit"><c>true</c> to inspect the ancestors of element; otherwise, <c>false</c>.</param>
/// <returns>An attribute stored for the specified type.</returns>
public T RetrieveOne<T>(MemberInfo member, Boolean inherit = false) where T : Attribute {
if(member == null) {
return default!;
}
IEnumerable<Object> attr = this.Retrieve(new Tuple<Object, Type>(member, typeof(T)), t => member.GetCustomAttributes(typeof(T), inherit));
return ConvertToAttribute<T>(attr);
}
/// <summary>
/// Gets one attribute of a specific type from a generic type.
/// </summary>
/// <typeparam name="TAttribute">The type of the attribute.</typeparam>
/// <typeparam name="T">The type to retrieve the attribute.</typeparam>
/// <param name="inherit">if set to <c>true</c> [inherit].</param>
/// <returns>An attribute stored for the specified type.</returns>
public TAttribute RetrieveOne<TAttribute, T>(Boolean inherit = false) where TAttribute : Attribute {
IEnumerable<Object> attr = this.Retrieve(new Tuple<Object, Type>(typeof(T), typeof(TAttribute)), t => typeof(T).GetCustomAttributes(typeof(TAttribute), inherit));
return ConvertToAttribute<TAttribute>(attr);
}
/// <summary>
/// Gets all properties an their attributes of a given type constrained to only attributes.
/// </summary>
/// <typeparam name="T">The type of the attribute to retrieve.</typeparam>
/// <param name="type">The type of the object.</param>
/// <param name="inherit"><c>true</c> to inspect the ancestors of element; otherwise, <c>false</c>.</param>
/// <returns>A dictionary of the properties and their attributes stored for the specified type.</returns>
public Dictionary<PropertyInfo, IEnumerable<Object>> Retrieve<T>(Type type, Boolean inherit = false) where T : Attribute => this.PropertyTypeCache.RetrieveAllProperties(type, true).ToDictionary(x => x, x => this.Retrieve<T>(x, inherit));
/// <summary>
/// Gets all properties and their attributes of a given type.
/// </summary>
/// <typeparam name="T">The object type used to extract the properties from.</typeparam>
/// <typeparam name="TAttribute">The type of the attribute.</typeparam>
/// <param name="inherit"><c>true</c> to inspect the ancestors of element; otherwise, <c>false</c>.</param>
/// <returns>
/// A dictionary of the properties and their attributes stored for the specified type.
/// </returns>
public Dictionary<PropertyInfo, IEnumerable<Object>> RetrieveFromType<T, TAttribute>(Boolean inherit = false) => this.RetrieveFromType<T>(typeof(TAttribute), inherit);
/// <summary>
/// Gets all properties and their attributes of a given type.
/// </summary>
/// <typeparam name="T">The object type used to extract the properties from.</typeparam>
/// <param name="attributeType">Type of the attribute.</param>
/// <param name="inherit"><c>true</c> to inspect the ancestors of element; otherwise, <c>false</c>.</param>
/// <returns>
/// A dictionary of the properties and their attributes stored for the specified type.
/// </returns>
public Dictionary<PropertyInfo, IEnumerable<Object>> RetrieveFromType<T>(Type attributeType, Boolean inherit = false) {
if(attributeType == null) {
throw new ArgumentNullException(nameof(attributeType));
}
return this.PropertyTypeCache.RetrieveAllProperties<T>(true).ToDictionary(x => x, x => this.Retrieve(x, attributeType, inherit));
}
private static T ConvertToAttribute<T>(IEnumerable<Object> attr) where T : Attribute => attr?.Any() != true ? (default!) : attr.Count() == 1 ? (T)Convert.ChangeType(attr.First(), typeof(T)) : throw new AmbiguousMatchException("Multiple custom attributes of the same type found.");
private IEnumerable<Object> Retrieve(Tuple<Object, Type> key, Func<Tuple<Object, Type>, IEnumerable<Object>> factory) {
if(factory == null) {
throw new ArgumentNullException(nameof(factory));
}
return this._data.Value.GetOrAdd(key, k => factory.Invoke(k).Where(item => item != null));
}
}
}

View File

@ -4,48 +4,39 @@ using System.Collections.Generic;
using System.Linq;
using System.Reflection;
namespace Swan.Lite.Reflection
{
namespace Swan.Lite.Reflection {
/// <summary>
/// A thread-safe cache of constructors belonging to a given type.
/// </summary>
public class ConstructorTypeCache : TypeCache<Tuple<ConstructorInfo, ParameterInfo[]>> {
/// <summary>
/// A thread-safe cache of constructors belonging to a given type.
/// Gets the default cache.
/// </summary>
public class ConstructorTypeCache : TypeCache<Tuple<ConstructorInfo, ParameterInfo[]>>
{
/// <summary>
/// Gets the default cache.
/// </summary>
/// <value>
/// The default cache.
/// </value>
public static Lazy<ConstructorTypeCache> DefaultCache { get; } =
new Lazy<ConstructorTypeCache>(() => new ConstructorTypeCache());
/// <summary>
/// Retrieves all constructors order by the number of parameters ascending.
/// </summary>
/// <typeparam name="T">The type to inspect.</typeparam>
/// <param name="includeNonPublic">if set to <c>true</c> [include non public].</param>
/// <returns>
/// A collection with all the constructors in the given type.
/// </returns>
public IEnumerable<Tuple<ConstructorInfo, ParameterInfo[]>> RetrieveAllConstructors<T>(bool includeNonPublic = false)
=> Retrieve<T>(GetConstructors(includeNonPublic));
/// <summary>
/// Retrieves all constructors order by the number of parameters ascending.
/// </summary>
/// <param name="type">The type.</param>
/// <param name="includeNonPublic">if set to <c>true</c> [include non public].</param>
/// <returns>
/// A collection with all the constructors in the given type.
/// </returns>
public IEnumerable<Tuple<ConstructorInfo, ParameterInfo[]>> RetrieveAllConstructors(Type type, bool includeNonPublic = false)
=> Retrieve(type, GetConstructors(includeNonPublic));
private static Func<Type, IEnumerable<Tuple<ConstructorInfo, ParameterInfo[]>>> GetConstructors(bool includeNonPublic)
=> t => t.GetConstructors(includeNonPublic ? BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance : BindingFlags.Public | BindingFlags.Instance)
.Select(x => Tuple.Create(x, x.GetParameters()))
.OrderBy(x => x.Item2.Length)
.ToList();
}
/// <value>
/// The default cache.
/// </value>
public static Lazy<ConstructorTypeCache> DefaultCache { get; } = new Lazy<ConstructorTypeCache>(() => new ConstructorTypeCache());
/// <summary>
/// Retrieves all constructors order by the number of parameters ascending.
/// </summary>
/// <typeparam name="T">The type to inspect.</typeparam>
/// <param name="includeNonPublic">if set to <c>true</c> [include non public].</param>
/// <returns>
/// A collection with all the constructors in the given type.
/// </returns>
public IEnumerable<Tuple<ConstructorInfo, ParameterInfo[]>> RetrieveAllConstructors<T>(Boolean includeNonPublic = false) => this.Retrieve<T>(GetConstructors(includeNonPublic));
/// <summary>
/// Retrieves all constructors order by the number of parameters ascending.
/// </summary>
/// <param name="type">The type.</param>
/// <param name="includeNonPublic">if set to <c>true</c> [include non public].</param>
/// <returns>
/// A collection with all the constructors in the given type.
/// </returns>
public IEnumerable<Tuple<ConstructorInfo, ParameterInfo[]>> RetrieveAllConstructors(Type type, Boolean includeNonPublic = false) => this.Retrieve(type, GetConstructors(includeNonPublic));
private static Func<Type, IEnumerable<Tuple<ConstructorInfo, ParameterInfo[]>>> GetConstructors(Boolean includeNonPublic) => t => t.GetConstructors(includeNonPublic ? BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance : BindingFlags.Public | BindingFlags.Instance).Select(x => Tuple.Create(x, x.GetParameters())).OrderBy(x => x.Item2.Length).ToList();
}
}

View File

@ -2,106 +2,112 @@
using System.Reflection;
using Swan.Configuration;
namespace Swan.Reflection
{
namespace Swan.Reflection {
/// <summary>
/// Represents a Property object from a Object Reflection Property with extended values.
/// </summary>
public class ExtendedPropertyInfo {
/// <summary>
/// Represents a Property object from a Object Reflection Property with extended values.
/// Initializes a new instance of the <see cref="ExtendedPropertyInfo"/> class.
/// </summary>
public class ExtendedPropertyInfo
{
/// <summary>
/// Initializes a new instance of the <see cref="ExtendedPropertyInfo"/> class.
/// </summary>
/// <param name="propertyInfo">The property information.</param>
public ExtendedPropertyInfo(PropertyInfo propertyInfo)
{
if (propertyInfo == null)
{
throw new ArgumentNullException(nameof(propertyInfo));
}
Property = propertyInfo.Name;
DataType = propertyInfo.PropertyType.Name;
foreach (PropertyDisplayAttribute display in AttributeCache.DefaultCache.Value.Retrieve<PropertyDisplayAttribute>(propertyInfo, true))
{
Name = display.Name;
Description = display.Description;
GroupName = display.GroupName;
DefaultValue = display.DefaultValue;
}
}
/// <summary>
/// Gets or sets the property.
/// </summary>
/// <value>
/// The property.
/// </value>
public string Property { get; }
/// <summary>
/// Gets or sets the type of the data.
/// </summary>
/// <value>
/// The type of the data.
/// </value>
public string DataType { get; }
/// <summary>
/// Gets or sets the value.
/// </summary>
/// <value>
/// The value.
/// </value>
public object Value { get; set; }
/// <summary>
/// Gets or sets the default value.
/// </summary>
/// <value>
/// The default value.
/// </value>
public object DefaultValue { get; }
/// <summary>
/// Gets or sets the name.
/// </summary>
/// <value>
/// The name.
/// </value>
public string Name { get; }
/// <summary>
/// Gets or sets the description.
/// </summary>
/// <value>
/// The description.
/// </value>
public string Description { get; }
/// <summary>
/// Gets or sets the name of the group.
/// </summary>
/// <value>
/// The name of the group.
/// </value>
public string GroupName { get; }
}
/// <param name="propertyInfo">The property information.</param>
public ExtendedPropertyInfo(PropertyInfo propertyInfo) {
if(propertyInfo == null) {
throw new ArgumentNullException(nameof(propertyInfo));
}
this.Property = propertyInfo.Name;
this.DataType = propertyInfo.PropertyType.Name;
foreach(PropertyDisplayAttribute display in AttributeCache.DefaultCache.Value.Retrieve<PropertyDisplayAttribute>(propertyInfo, true)) {
this.Name = display.Name;
this.Description = display.Description;
this.GroupName = display.GroupName;
this.DefaultValue = display.DefaultValue;
}
}
/// <summary>
/// Represents a Property object from a Object Reflection Property with extended values.
/// Gets or sets the property.
/// </summary>
/// <typeparam name="T">The type of the object.</typeparam>
public class ExtendedPropertyInfo<T> : ExtendedPropertyInfo
{
/// <summary>
/// Initializes a new instance of the <see cref="ExtendedPropertyInfo{T}"/> class.
/// </summary>
/// <param name="property">The property.</param>
public ExtendedPropertyInfo(string property)
: base(typeof(T).GetProperty(property))
{
}
}
/// <value>
/// The property.
/// </value>
public String Property {
get;
}
/// <summary>
/// Gets or sets the type of the data.
/// </summary>
/// <value>
/// The type of the data.
/// </value>
public String DataType {
get;
}
/// <summary>
/// Gets or sets the value.
/// </summary>
/// <value>
/// The value.
/// </value>
public Object Value {
get; set;
}
/// <summary>
/// Gets or sets the default value.
/// </summary>
/// <value>
/// The default value.
/// </value>
public Object DefaultValue {
get;
}
/// <summary>
/// Gets or sets the name.
/// </summary>
/// <value>
/// The name.
/// </value>
public String Name {
get;
}
/// <summary>
/// Gets or sets the description.
/// </summary>
/// <value>
/// The description.
/// </value>
public String Description {
get;
}
/// <summary>
/// Gets or sets the name of the group.
/// </summary>
/// <value>
/// The name of the group.
/// </value>
public String GroupName {
get;
}
}
/// <summary>
/// Represents a Property object from a Object Reflection Property with extended values.
/// </summary>
/// <typeparam name="T">The type of the object.</typeparam>
public class ExtendedPropertyInfo<T> : ExtendedPropertyInfo {
/// <summary>
/// Initializes a new instance of the <see cref="ExtendedPropertyInfo{T}"/> class.
/// </summary>
/// <param name="property">The property.</param>
public ExtendedPropertyInfo(String property) : base(typeof(T).GetProperty(property)) {
}
}
}

View File

@ -1,267 +1,247 @@
using System;
#nullable enable
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Globalization;
using System.Linq;
using System.Reflection;
namespace Swan.Reflection
{
/// <summary>
/// Provides extended information about a type.
///
/// This class is mainly used to define sets of types within the Definition class
/// and it is not meant for other than querying the BasicTypesInfo dictionary.
/// </summary>
public class ExtendedTypeInfo
namespace Swan.Reflection {
/// <summary>
/// Provides extended information about a type.
///
/// This class is mainly used to define sets of types within the Definition class
/// and it is not meant for other than querying the BasicTypesInfo dictionary.
/// </summary>
public class ExtendedTypeInfo {
private const String TryParseMethodName = nameof(Byte.TryParse);
private const String ToStringMethodName = nameof(ToString);
private static readonly Type[] NumericTypes =
{
private const string TryParseMethodName = nameof(byte.TryParse);
private const string ToStringMethodName = nameof(ToString);
private static readonly Type[] NumericTypes =
{
typeof(byte),
typeof(sbyte),
typeof(decimal),
typeof(double),
typeof(float),
typeof(int),
typeof(uint),
typeof(long),
typeof(ulong),
typeof(short),
typeof(ushort),
};
private readonly ParameterInfo[]? _tryParseParameters;
private readonly int _toStringArgumentLength;
#region Constructors
/// <summary>
/// Initializes a new instance of the <see cref="ExtendedTypeInfo"/> class.
/// </summary>
/// <param name="t">The t.</param>
public ExtendedTypeInfo(Type t)
{
Type = t ?? throw new ArgumentNullException(nameof(t));
IsNullableValueType = Type.IsGenericType
&& Type.GetGenericTypeDefinition() == typeof(Nullable<>);
IsValueType = t.IsValueType;
UnderlyingType = IsNullableValueType ?
new NullableConverter(Type).UnderlyingType :
Type;
IsNumeric = NumericTypes.Contains(UnderlyingType);
// Extract the TryParse method info
try
{
TryParseMethodInfo = UnderlyingType.GetMethod(TryParseMethodName,
new[] { typeof(string), typeof(NumberStyles), typeof(IFormatProvider), UnderlyingType.MakeByRefType() }) ??
UnderlyingType.GetMethod(TryParseMethodName,
new[] { typeof(string), UnderlyingType.MakeByRefType() });
_tryParseParameters = TryParseMethodInfo?.GetParameters();
}
catch
{
// ignored
}
// Extract the ToString method Info
try
{
ToStringMethodInfo = UnderlyingType.GetMethod(ToStringMethodName,
new[] { typeof(IFormatProvider) }) ??
UnderlyingType.GetMethod(ToStringMethodName,
Array.Empty<Type>());
_toStringArgumentLength = ToStringMethodInfo?.GetParameters().Length ?? 0;
}
catch
{
// ignored
}
}
#endregion
#region Properties
/// <summary>
/// Gets the type this extended info class provides for.
/// </summary>
/// <value>
/// The type.
/// </value>
public Type Type { get; }
/// <summary>
/// Gets a value indicating whether the type is a nullable value type.
/// </summary>
/// <value>
/// <c>true</c> if this instance is nullable value type; otherwise, <c>false</c>.
/// </value>
public bool IsNullableValueType { get; }
/// <summary>
/// Gets a value indicating whether the type or underlying type is numeric.
/// </summary>
/// <value>
/// <c>true</c> if this instance is numeric; otherwise, <c>false</c>.
/// </value>
public bool IsNumeric { get; }
/// <summary>
/// Gets a value indicating whether the type is value type.
/// Nullable value types have this property set to False.
/// </summary>
public bool IsValueType { get; }
/// <summary>
/// When dealing with nullable value types, this property will
/// return the underlying value type of the nullable,
/// Otherwise it will return the same type as the Type property.
/// </summary>
/// <value>
/// The type of the underlying.
/// </value>
public Type UnderlyingType { get; }
/// <summary>
/// Gets the try parse method information. If the type does not contain
/// a suitable TryParse static method, it will return null.
/// </summary>
/// <value>
/// The try parse method information.
/// </value>
public MethodInfo TryParseMethodInfo { get; }
/// <summary>
/// Gets the ToString method info
/// It will prefer the overload containing the IFormatProvider argument.
/// </summary>
/// <value>
/// To string method information.
/// </value>
public MethodInfo ToStringMethodInfo { get; }
/// <summary>
/// Gets a value indicating whether the type contains a suitable TryParse method.
/// </summary>
/// <value>
/// <c>true</c> if this instance can parse natively; otherwise, <c>false</c>.
/// </value>
public bool CanParseNatively => TryParseMethodInfo != null;
#endregion
#region Methods
/// <summary>
/// Tries to parse the string into an object of the type this instance represents.
/// Returns false when no suitable TryParse methods exists for the type or when parsing fails
/// for any reason. When possible, this method uses CultureInfo.InvariantCulture and NumberStyles.Any.
/// </summary>
/// <param name="s">The s.</param>
/// <param name="result">The result.</param>
/// <returns><c>true</c> if parse was converted successfully; otherwise, <c>false</c>.</returns>
public bool TryParse(string s, out object? result)
{
result = Type.GetDefault();
try
{
if (Type == typeof(string))
{
result = Convert.ChangeType(s, Type, CultureInfo.InvariantCulture);
return true;
}
if ((IsNullableValueType && string.IsNullOrEmpty(s)) || !CanParseNatively)
{
return true;
}
// Build the arguments of the TryParse method
var dynamicArguments = new List<object?> { s };
for (var pi = 1; pi < _tryParseParameters.Length - 1; pi++)
{
var argInfo = _tryParseParameters[pi];
if (argInfo.ParameterType == typeof(IFormatProvider))
dynamicArguments.Add(CultureInfo.InvariantCulture);
else if (argInfo.ParameterType == typeof(NumberStyles))
dynamicArguments.Add(NumberStyles.Any);
else
dynamicArguments.Add(null);
}
dynamicArguments.Add(null);
var parseArguments = dynamicArguments.ToArray();
if ((bool) TryParseMethodInfo.Invoke(null, parseArguments))
{
result = parseArguments[parseArguments.Length - 1];
return true;
}
}
catch
{
// Ignore
}
return false;
}
/// <summary>
/// Converts this instance to its string representation,
/// trying to use the CultureInfo.InvariantCulture
/// IFormat provider if the overload is available.
/// </summary>
/// <param name="instance">The instance.</param>
/// <returns>A <see cref="System.String" /> that represents the current object.</returns>
public string ToStringInvariant(object instance)
{
if (instance == null)
return string.Empty;
return _toStringArgumentLength != 1
? instance.ToString()
: ToStringMethodInfo.Invoke(instance, new object[] {CultureInfo.InvariantCulture}) as string ?? string.Empty;
}
#endregion
}
typeof(Byte),
typeof(SByte),
typeof(Decimal),
typeof(Double),
typeof(Single),
typeof(Int32),
typeof(UInt32),
typeof(Int64),
typeof(UInt64),
typeof(Int16),
typeof(UInt16),
};
private readonly ParameterInfo[]? _tryParseParameters;
private readonly Int32 _toStringArgumentLength;
#region Constructors
/// <summary>
/// Provides extended information about a type.
///
/// This class is mainly used to define sets of types within the Constants class
/// and it is not meant for other than querying the BasicTypesInfo dictionary.
/// Initializes a new instance of the <see cref="ExtendedTypeInfo"/> class.
/// </summary>
/// <typeparam name="T">The type of extended type information.</typeparam>
public class ExtendedTypeInfo<T> : ExtendedTypeInfo
{
/// <summary>
/// Initializes a new instance of the <see cref="ExtendedTypeInfo{T}"/> class.
/// </summary>
public ExtendedTypeInfo()
: base(typeof(T))
{
// placeholder
}
/// <summary>
/// Converts this instance to its string representation,
/// trying to use the CultureInfo.InvariantCulture
/// IFormat provider if the overload is available.
/// </summary>
/// <param name="instance">The instance.</param>
/// <returns>A <see cref="System.String" /> that represents the current object.</returns>
public string ToStringInvariant(T instance) => base.ToStringInvariant(instance);
}
/// <param name="t">The t.</param>
public ExtendedTypeInfo(Type t) {
this.Type = t ?? throw new ArgumentNullException(nameof(t));
this.IsNullableValueType = this.Type.IsGenericType && this.Type.GetGenericTypeDefinition() == typeof(Nullable<>);
this.IsValueType = t.IsValueType;
this.UnderlyingType = this.IsNullableValueType ? new NullableConverter(this.Type).UnderlyingType : this.Type;
this.IsNumeric = NumericTypes.Contains(this.UnderlyingType);
// Extract the TryParse method info
try {
this.TryParseMethodInfo = this.UnderlyingType.GetMethod(TryParseMethodName, new[] { typeof(String), typeof(NumberStyles), typeof(IFormatProvider), this.UnderlyingType.MakeByRefType() }) ?? this.UnderlyingType.GetMethod(TryParseMethodName, new[] { typeof(String), this.UnderlyingType.MakeByRefType() });
this._tryParseParameters = this.TryParseMethodInfo?.GetParameters();
} catch {
// ignored
}
// Extract the ToString method Info
try {
this.ToStringMethodInfo = this.UnderlyingType.GetMethod(ToStringMethodName, new[] { typeof(IFormatProvider) }) ?? this.UnderlyingType.GetMethod(ToStringMethodName, Array.Empty<Type>());
this._toStringArgumentLength = this.ToStringMethodInfo?.GetParameters().Length ?? 0;
} catch {
// ignored
}
}
#endregion
#region Properties
/// <summary>
/// Gets the type this extended info class provides for.
/// </summary>
/// <value>
/// The type.
/// </value>
public Type Type {
get;
}
/// <summary>
/// Gets a value indicating whether the type is a nullable value type.
/// </summary>
/// <value>
/// <c>true</c> if this instance is nullable value type; otherwise, <c>false</c>.
/// </value>
public Boolean IsNullableValueType {
get;
}
/// <summary>
/// Gets a value indicating whether the type or underlying type is numeric.
/// </summary>
/// <value>
/// <c>true</c> if this instance is numeric; otherwise, <c>false</c>.
/// </value>
public Boolean IsNumeric {
get;
}
/// <summary>
/// Gets a value indicating whether the type is value type.
/// Nullable value types have this property set to False.
/// </summary>
public Boolean IsValueType {
get;
}
/// <summary>
/// When dealing with nullable value types, this property will
/// return the underlying value type of the nullable,
/// Otherwise it will return the same type as the Type property.
/// </summary>
/// <value>
/// The type of the underlying.
/// </value>
public Type UnderlyingType {
get;
}
/// <summary>
/// Gets the try parse method information. If the type does not contain
/// a suitable TryParse static method, it will return null.
/// </summary>
/// <value>
/// The try parse method information.
/// </value>
public MethodInfo? TryParseMethodInfo {
get;
}
/// <summary>
/// Gets the ToString method info
/// It will prefer the overload containing the IFormatProvider argument.
/// </summary>
/// <value>
/// To string method information.
/// </value>
public MethodInfo? ToStringMethodInfo {
get;
}
/// <summary>
/// Gets a value indicating whether the type contains a suitable TryParse method.
/// </summary>
/// <value>
/// <c>true</c> if this instance can parse natively; otherwise, <c>false</c>.
/// </value>
public Boolean CanParseNatively => this.TryParseMethodInfo != null;
#endregion
#region Methods
/// <summary>
/// Tries to parse the string into an object of the type this instance represents.
/// Returns false when no suitable TryParse methods exists for the type or when parsing fails
/// for any reason. When possible, this method uses CultureInfo.InvariantCulture and NumberStyles.Any.
/// </summary>
/// <param name="s">The s.</param>
/// <param name="result">The result.</param>
/// <returns><c>true</c> if parse was converted successfully; otherwise, <c>false</c>.</returns>
public Boolean TryParse(String s, out Object? result) {
result = this.Type.GetDefault();
try {
if(this.Type == typeof(String)) {
result = Convert.ChangeType(s, this.Type, CultureInfo.InvariantCulture);
return true;
}
if(this.IsNullableValueType && String.IsNullOrEmpty(s) || !this.CanParseNatively) {
return true;
}
// Build the arguments of the TryParse method
List<Object?> dynamicArguments = new List<Object?> { s };
if(this._tryParseParameters != null) {
for(Int32 pi = 1; pi < this._tryParseParameters.Length - 1; pi++) {
ParameterInfo argInfo = this._tryParseParameters[pi];
if(argInfo.ParameterType == typeof(IFormatProvider)) {
dynamicArguments.Add(CultureInfo.InvariantCulture);
} else if(argInfo.ParameterType == typeof(NumberStyles)) {
dynamicArguments.Add(NumberStyles.Any);
} else {
dynamicArguments.Add(null);
}
}
}
dynamicArguments.Add(null);
Object?[] parseArguments = dynamicArguments.ToArray();
if((Boolean)this.TryParseMethodInfo?.Invoke(null, parseArguments)!) {
result = parseArguments[^1];
return true;
}
} catch {
// Ignore
}
return false;
}
/// <summary>
/// Converts this instance to its string representation,
/// trying to use the CultureInfo.InvariantCulture
/// IFormat provider if the overload is available.
/// </summary>
/// <param name="instance">The instance.</param>
/// <returns>A <see cref="System.String" /> that represents the current object.</returns>
public String ToStringInvariant(Object? instance) => instance == null ? String.Empty : this._toStringArgumentLength != 1 ? instance.ToString()! : this.ToStringMethodInfo?.Invoke(instance, new Object[] { CultureInfo.InvariantCulture }) as String ?? String.Empty;
#endregion
}
/// <summary>
/// Provides extended information about a type.
///
/// This class is mainly used to define sets of types within the Constants class
/// and it is not meant for other than querying the BasicTypesInfo dictionary.
/// </summary>
/// <typeparam name="T">The type of extended type information.</typeparam>
public class ExtendedTypeInfo<T> : ExtendedTypeInfo {
/// <summary>
/// Initializes a new instance of the <see cref="ExtendedTypeInfo{T}"/> class.
/// </summary>
public ExtendedTypeInfo() : base(typeof(T)) {
// placeholder
}
/// <summary>
/// Converts this instance to its string representation,
/// trying to use the CultureInfo.InvariantCulture
/// IFormat provider if the overload is available.
/// </summary>
/// <param name="instance">The instance.</param>
/// <returns>A <see cref="System.String" /> that represents the current object.</returns>
public String ToStringInvariant(T instance) => base.ToStringInvariant(instance);
}
}

View File

@ -1,22 +1,22 @@
namespace Swan.Reflection
{
using System;
namespace Swan.Reflection {
/// <summary>
/// Represents a generic interface to store getters and setters.
/// </summary>
public interface IPropertyProxy {
/// <summary>
/// Represents a generic interface to store getters and setters.
/// Gets the property value via a stored delegate.
/// </summary>
public interface IPropertyProxy
{
/// <summary>
/// Gets the property value via a stored delegate.
/// </summary>
/// <param name="instance">The instance.</param>
/// <returns>The property value.</returns>
object GetValue(object instance);
/// <summary>
/// Sets the property value via a stored delegate.
/// </summary>
/// <param name="instance">The instance.</param>
/// <param name="value">The value.</param>
void SetValue(object instance, object value);
}
/// <param name="instance">The instance.</param>
/// <returns>The property value.</returns>
Object GetValue(Object instance);
/// <summary>
/// Sets the property value via a stored delegate.
/// </summary>
/// <param name="instance">The instance.</param>
/// <param name="value">The value.</param>
void SetValue(Object instance, Object value);
}
}

View File

@ -2,116 +2,109 @@
using System.Collections.Concurrent;
using System.Reflection;
namespace Swan.Reflection
{
namespace Swan.Reflection {
/// <summary>
/// Represents a Method Info Cache.
/// </summary>
public class MethodInfoCache : ConcurrentDictionary<String, MethodInfo> {
/// <summary>
/// Represents a Method Info Cache.
/// Retrieves the properties stored for the specified type.
/// If the properties are not available, it calls the factory method to retrieve them
/// and returns them as an array of PropertyInfo.
/// </summary>
public class MethodInfoCache : ConcurrentDictionary<string, MethodInfo>
{
/// <summary>
/// Retrieves the properties stored for the specified type.
/// If the properties are not available, it calls the factory method to retrieve them
/// and returns them as an array of PropertyInfo.
/// </summary>
/// <typeparam name="T">The type of type.</typeparam>
/// <param name="name">The name.</param>
/// <param name="alias">The alias.</param>
/// <param name="types">The types.</param>
/// <returns>
/// The cached MethodInfo.
/// </returns>
/// <exception cref="ArgumentNullException">name
/// or
/// factory.</exception>
public MethodInfo Retrieve<T>(string name, string alias, params Type[] types)
=> Retrieve(typeof(T), name, alias, types);
/// <summary>
/// Retrieves the specified name.
/// </summary>
/// <typeparam name="T">The type of type.</typeparam>
/// <param name="name">The name.</param>
/// <param name="types">The types.</param>
/// <returns>
/// The cached MethodInfo.
/// </returns>
public MethodInfo Retrieve<T>(string name, params Type[] types)
=> Retrieve(typeof(T), name, name, types);
/// <summary>
/// Retrieves the specified type.
/// </summary>
/// <param name="type">The type.</param>
/// <param name="name">The name.</param>
/// <param name="types">The types.</param>
/// <returns>
/// An array of the properties stored for the specified type.
/// </returns>
public MethodInfo Retrieve(Type type, string name, params Type[] types)
=> Retrieve(type, name, name, types);
/// <summary>
/// Retrieves the specified type.
/// </summary>
/// <param name="type">The type.</param>
/// <param name="name">The name.</param>
/// <param name="alias">The alias.</param>
/// <param name="types">The types.</param>
/// <returns>
/// The cached MethodInfo.
/// </returns>
public MethodInfo Retrieve(Type type, string name, string alias, params Type[] types)
{
if (type == null)
throw new ArgumentNullException(nameof(type));
if (alias == null)
throw new ArgumentNullException(nameof(alias));
if (name == null)
throw new ArgumentNullException(nameof(name));
return GetOrAdd(
alias,
x => type.GetMethod(name, types ?? Array.Empty<Type>()));
}
/// <summary>
/// Retrieves the specified name.
/// </summary>
/// <typeparam name="T">The type of type.</typeparam>
/// <param name="name">The name.</param>
/// <returns>
/// The cached MethodInfo.
/// </returns>
public MethodInfo Retrieve<T>(string name)
=> Retrieve(typeof(T), name);
/// <summary>
/// Retrieves the specified type.
/// </summary>
/// <param name="type">The type.</param>
/// <param name="name">The name.</param>
/// <returns>
/// The cached MethodInfo.
/// </returns>
/// <exception cref="ArgumentNullException">
/// type
/// or
/// name.
/// </exception>
public MethodInfo Retrieve(Type type, string name)
{
if (type == null)
throw new ArgumentNullException(nameof(type));
if (name == null)
throw new ArgumentNullException(nameof(name));
return GetOrAdd(
name,
type.GetMethod);
}
}
/// <typeparam name="T">The type of type.</typeparam>
/// <param name="name">The name.</param>
/// <param name="alias">The alias.</param>
/// <param name="types">The types.</param>
/// <returns>
/// The cached MethodInfo.
/// </returns>
/// <exception cref="ArgumentNullException">name
/// or
/// factory.</exception>
public MethodInfo Retrieve<T>(String name, String alias, params Type[] types) => this.Retrieve(typeof(T), name, alias, types);
/// <summary>
/// Retrieves the specified name.
/// </summary>
/// <typeparam name="T">The type of type.</typeparam>
/// <param name="name">The name.</param>
/// <param name="types">The types.</param>
/// <returns>
/// The cached MethodInfo.
/// </returns>
public MethodInfo Retrieve<T>(String name, params Type[] types) => this.Retrieve(typeof(T), name, name, types);
/// <summary>
/// Retrieves the specified type.
/// </summary>
/// <param name="type">The type.</param>
/// <param name="name">The name.</param>
/// <param name="types">The types.</param>
/// <returns>
/// An array of the properties stored for the specified type.
/// </returns>
public MethodInfo Retrieve(Type type, String name, params Type[] types) => this.Retrieve(type, name, name, types);
/// <summary>
/// Retrieves the specified type.
/// </summary>
/// <param name="type">The type.</param>
/// <param name="name">The name.</param>
/// <param name="alias">The alias.</param>
/// <param name="types">The types.</param>
/// <returns>
/// The cached MethodInfo.
/// </returns>
public MethodInfo Retrieve(Type type, String name, String alias, params Type[] types) {
if(type == null) {
throw new ArgumentNullException(nameof(type));
}
if(alias == null) {
throw new ArgumentNullException(nameof(alias));
}
if(name == null) {
throw new ArgumentNullException(nameof(name));
}
return this.GetOrAdd(alias, x => type.GetMethod(name, types ?? Array.Empty<Type>()));
}
/// <summary>
/// Retrieves the specified name.
/// </summary>
/// <typeparam name="T">The type of type.</typeparam>
/// <param name="name">The name.</param>
/// <returns>
/// The cached MethodInfo.
/// </returns>
public MethodInfo Retrieve<T>(String name) => this.Retrieve(typeof(T), name);
/// <summary>
/// Retrieves the specified type.
/// </summary>
/// <param name="type">The type.</param>
/// <param name="name">The name.</param>
/// <returns>
/// The cached MethodInfo.
/// </returns>
/// <exception cref="ArgumentNullException">
/// type
/// or
/// name.
/// </exception>
public MethodInfo Retrieve(Type type, String name) {
if(type == null) {
throw new ArgumentNullException(nameof(type));
}
if(name == null) {
throw new ArgumentNullException(nameof(name));
}
return this.GetOrAdd(name, type.GetMethod);
}
}
}

View File

@ -2,46 +2,43 @@
using System.Reflection;
using System.Runtime.CompilerServices;
namespace Swan.Reflection
{
namespace Swan.Reflection {
/// <summary>
/// Represents a generic class to store getters and setters.
/// </summary>
/// <typeparam name="TClass">The type of the class.</typeparam>
/// <typeparam name="TProperty">The type of the property.</typeparam>
/// <seealso cref="IPropertyProxy" />
public sealed class PropertyProxy<TClass, TProperty> : IPropertyProxy where TClass : class {
private readonly Func<TClass, TProperty> _getter;
private readonly Action<TClass, TProperty> _setter;
/// <summary>
/// Represents a generic class to store getters and setters.
/// Initializes a new instance of the <see cref="PropertyProxy{TClass, TProperty}"/> class.
/// </summary>
/// <typeparam name="TClass">The type of the class.</typeparam>
/// <typeparam name="TProperty">The type of the property.</typeparam>
/// <seealso cref="IPropertyProxy" />
public sealed class PropertyProxy<TClass, TProperty> : IPropertyProxy
where TClass : class
{
private readonly Func<TClass, TProperty> _getter;
private readonly Action<TClass, TProperty> _setter;
/// <summary>
/// Initializes a new instance of the <see cref="PropertyProxy{TClass, TProperty}"/> class.
/// </summary>
/// <param name="property">The property.</param>
public PropertyProxy(PropertyInfo property)
{
if (property == null)
throw new ArgumentNullException(nameof(property));
var getterInfo = property.GetGetMethod(false);
if (getterInfo != null)
_getter = (Func<TClass, TProperty>)Delegate.CreateDelegate(typeof(Func<TClass, TProperty>), getterInfo);
var setterInfo = property.GetSetMethod(false);
if (setterInfo != null)
_setter = (Action<TClass, TProperty>)Delegate.CreateDelegate(typeof(Action<TClass, TProperty>), setterInfo);
}
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
object IPropertyProxy.GetValue(object instance) =>
_getter(instance as TClass);
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
void IPropertyProxy.SetValue(object instance, object value) =>
_setter(instance as TClass, (TProperty)value);
}
/// <param name="property">The property.</param>
public PropertyProxy(PropertyInfo property) {
if(property == null) {
throw new ArgumentNullException(nameof(property));
}
MethodInfo getterInfo = property.GetGetMethod(false);
if(getterInfo != null) {
this._getter = (Func<TClass, TProperty>)Delegate.CreateDelegate(typeof(Func<TClass, TProperty>), getterInfo);
}
MethodInfo setterInfo = property.GetSetMethod(false);
if(setterInfo != null) {
this._setter = (Action<TClass, TProperty>)Delegate.CreateDelegate(typeof(Action<TClass, TProperty>), setterInfo);
}
}
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
Object IPropertyProxy.GetValue(Object instance) => this._getter(instance as TClass);
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
void IPropertyProxy.SetValue(Object instance, Object value) => this._setter(instance as TClass, (TProperty)value);
}
}

View File

@ -3,72 +3,54 @@ using System.Collections.Generic;
using System.Linq;
using System.Reflection;
namespace Swan.Reflection
{
namespace Swan.Reflection {
/// <summary>
/// A thread-safe cache of properties belonging to a given type.
/// </summary>
public class PropertyTypeCache : TypeCache<PropertyInfo> {
/// <summary>
/// A thread-safe cache of properties belonging to a given type.
/// Gets the default cache.
/// </summary>
public class PropertyTypeCache : TypeCache<PropertyInfo>
{
/// <summary>
/// Gets the default cache.
/// </summary>
/// <value>
/// The default cache.
/// </value>
public static Lazy<PropertyTypeCache> DefaultCache { get; } = new Lazy<PropertyTypeCache>(() => new PropertyTypeCache());
/// <summary>
/// Retrieves all properties.
/// </summary>
/// <typeparam name="T">The type to inspect.</typeparam>
/// <param name="onlyPublic">if set to <c>true</c> [only public].</param>
/// <returns>
/// A collection with all the properties in the given type.
/// </returns>
public IEnumerable<PropertyInfo> RetrieveAllProperties<T>(bool onlyPublic = false)
=> Retrieve<T>(onlyPublic ? GetAllPublicPropertiesFunc() : GetAllPropertiesFunc());
/// <summary>
/// Retrieves all properties.
/// </summary>
/// <param name="type">The type.</param>
/// <param name="onlyPublic">if set to <c>true</c> [only public].</param>
/// <returns>
/// A collection with all the properties in the given type.
/// </returns>
public IEnumerable<PropertyInfo> RetrieveAllProperties(Type type, bool onlyPublic = false)
=> Retrieve(type, onlyPublic ? GetAllPublicPropertiesFunc() : GetAllPropertiesFunc());
/// <summary>
/// Retrieves the filtered properties.
/// </summary>
/// <param name="type">The type.</param>
/// <param name="onlyPublic">if set to <c>true</c> [only public].</param>
/// <param name="filter">The filter.</param>
/// <returns>
/// A collection with all the properties in the given type.
/// </returns>
public IEnumerable<PropertyInfo> RetrieveFilteredProperties(
Type type,
bool onlyPublic,
Func<PropertyInfo, bool> filter)
=> Retrieve(type,
onlyPublic ? GetAllPublicPropertiesFunc(filter) : GetAllPropertiesFunc(filter));
private static Func<Type, IEnumerable<PropertyInfo>> GetAllPropertiesFunc(
Func<PropertyInfo, bool> filter = null)
=> GetPropertiesFunc(
BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance,
filter);
private static Func<Type, IEnumerable<PropertyInfo>> GetAllPublicPropertiesFunc(
Func<PropertyInfo, bool> filter = null)
=> GetPropertiesFunc(BindingFlags.Public | BindingFlags.Instance, filter);
private static Func<Type, IEnumerable<PropertyInfo>> GetPropertiesFunc(BindingFlags flags,
Func<PropertyInfo, bool> filter = null)
=> t => t.GetProperties(flags)
.Where(filter ?? (p => p.CanRead || p.CanWrite));
}
/// <value>
/// The default cache.
/// </value>
public static Lazy<PropertyTypeCache> DefaultCache { get; } = new Lazy<PropertyTypeCache>(() => new PropertyTypeCache());
/// <summary>
/// Retrieves all properties.
/// </summary>
/// <typeparam name="T">The type to inspect.</typeparam>
/// <param name="onlyPublic">if set to <c>true</c> [only public].</param>
/// <returns>
/// A collection with all the properties in the given type.
/// </returns>
public IEnumerable<PropertyInfo> RetrieveAllProperties<T>(Boolean onlyPublic = false) => this.Retrieve<T>(onlyPublic ? GetAllPublicPropertiesFunc() : GetAllPropertiesFunc());
/// <summary>
/// Retrieves all properties.
/// </summary>
/// <param name="type">The type.</param>
/// <param name="onlyPublic">if set to <c>true</c> [only public].</param>
/// <returns>
/// A collection with all the properties in the given type.
/// </returns>
public IEnumerable<PropertyInfo> RetrieveAllProperties(Type type, Boolean onlyPublic = false) => this.Retrieve(type, onlyPublic ? GetAllPublicPropertiesFunc() : GetAllPropertiesFunc());
/// <summary>
/// Retrieves the filtered properties.
/// </summary>
/// <param name="type">The type.</param>
/// <param name="onlyPublic">if set to <c>true</c> [only public].</param>
/// <param name="filter">The filter.</param>
/// <returns>
/// A collection with all the properties in the given type.
/// </returns>
public IEnumerable<PropertyInfo> RetrieveFilteredProperties(Type type, Boolean onlyPublic, Func<PropertyInfo, Boolean> filter) => this.Retrieve(type, onlyPublic ? GetAllPublicPropertiesFunc(filter) : GetAllPropertiesFunc(filter));
private static Func<Type, IEnumerable<PropertyInfo>> GetAllPropertiesFunc(Func<PropertyInfo, Boolean> filter = null) => GetPropertiesFunc(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance, filter);
private static Func<Type, IEnumerable<PropertyInfo>> GetAllPublicPropertiesFunc(Func<PropertyInfo, Boolean> filter = null) => GetPropertiesFunc(BindingFlags.Public | BindingFlags.Instance, filter);
private static Func<Type, IEnumerable<PropertyInfo>> GetPropertiesFunc(BindingFlags flags, Func<PropertyInfo, Boolean> filter = null) => t => t.GetProperties(flags).Where(filter ?? (p => p.CanRead || p.CanWrite));
}
}

View File

@ -3,76 +3,69 @@ using System.Collections.Generic;
using System.Reflection;
using Swan.Collections;
namespace Swan.Reflection
{
namespace Swan.Reflection {
/// <summary>
/// A thread-safe cache of members belonging to a given type.
///
/// The Retrieve method is the most useful one in this class as it
/// calls the retrieval process if the type is not contained
/// in the cache.
/// </summary>
/// <typeparam name="T">The type of Member to be cached.</typeparam>
public abstract class TypeCache<T> : CollectionCacheRepository<T> {
/// <summary>
/// A thread-safe cache of members belonging to a given type.
///
/// The Retrieve method is the most useful one in this class as it
/// calls the retrieval process if the type is not contained
/// in the cache.
/// Determines whether the cache contains the specified type.
/// </summary>
/// <typeparam name="T">The type of Member to be cached.</typeparam>
public abstract class TypeCache<T> : CollectionCacheRepository<T>
{
/// <summary>
/// Determines whether the cache contains the specified type.
/// </summary>
/// <typeparam name="TOut">The type of the out.</typeparam>
/// <returns>
/// <c>true</c> if [contains]; otherwise, <c>false</c>.
/// </returns>
public bool Contains<TOut>() => ContainsKey(typeof(TOut));
/// <summary>
/// Retrieves the properties stored for the specified type.
/// If the properties are not available, it calls the factory method to retrieve them
/// and returns them as an array of PropertyInfo.
/// </summary>
/// <typeparam name="TOut">The type of the out.</typeparam>
/// <param name="factory">The factory.</param>
/// <returns>An array of the properties stored for the specified type.</returns>
public IEnumerable<T> Retrieve<TOut>(Func<Type, IEnumerable<T>> factory)
=> Retrieve(typeof(TOut), factory);
}
/// <typeparam name="TOut">The type of the out.</typeparam>
/// <returns>
/// <c>true</c> if [contains]; otherwise, <c>false</c>.
/// </returns>
public Boolean Contains<TOut>() => this.ContainsKey(typeof(TOut));
/// <summary>
/// A thread-safe cache of fields belonging to a given type
/// The Retrieve method is the most useful one in this class as it
/// calls the retrieval process if the type is not contained
/// in the cache.
/// Retrieves the properties stored for the specified type.
/// If the properties are not available, it calls the factory method to retrieve them
/// and returns them as an array of PropertyInfo.
/// </summary>
public class FieldTypeCache : TypeCache<FieldInfo>
{
/// <summary>
/// Gets the default cache.
/// </summary>
/// <value>
/// The default cache.
/// </value>
public static Lazy<FieldTypeCache> DefaultCache { get; } = new Lazy<FieldTypeCache>(() => new FieldTypeCache());
/// <summary>
/// Retrieves all fields.
/// </summary>
/// <typeparam name="T">The type to inspect.</typeparam>
/// <returns>
/// A collection with all the fields in the given type.
/// </returns>
public IEnumerable<FieldInfo> RetrieveAllFields<T>()
=> Retrieve<T>(GetAllFieldsFunc());
/// <summary>
/// Retrieves all fields.
/// </summary>
/// <param name="type">The type.</param>
/// <returns>
/// A collection with all the fields in the given type.
/// </returns>
public IEnumerable<FieldInfo> RetrieveAllFields(Type type)
=> Retrieve(type, GetAllFieldsFunc());
private static Func<Type, IEnumerable<FieldInfo>> GetAllFieldsFunc()
=> t => t.GetFields(BindingFlags.Public | BindingFlags.Instance);
}
/// <typeparam name="TOut">The type of the out.</typeparam>
/// <param name="factory">The factory.</param>
/// <returns>An array of the properties stored for the specified type.</returns>
public IEnumerable<T> Retrieve<TOut>(Func<Type, IEnumerable<T>> factory) => this.Retrieve(typeof(TOut), factory);
}
/// <summary>
/// A thread-safe cache of fields belonging to a given type
/// The Retrieve method is the most useful one in this class as it
/// calls the retrieval process if the type is not contained
/// in the cache.
/// </summary>
public class FieldTypeCache : TypeCache<FieldInfo> {
/// <summary>
/// Gets the default cache.
/// </summary>
/// <value>
/// The default cache.
/// </value>
public static Lazy<FieldTypeCache> DefaultCache { get; } = new Lazy<FieldTypeCache>(() => new FieldTypeCache());
/// <summary>
/// Retrieves all fields.
/// </summary>
/// <typeparam name="T">The type to inspect.</typeparam>
/// <returns>
/// A collection with all the fields in the given type.
/// </returns>
public IEnumerable<FieldInfo> RetrieveAllFields<T>() => this.Retrieve<T>(GetAllFieldsFunc());
/// <summary>
/// Retrieves all fields.
/// </summary>
/// <param name="type">The type.</param>
/// <returns>
/// A collection with all the fields in the given type.
/// </returns>
public IEnumerable<FieldInfo> RetrieveAllFields(Type type) => this.Retrieve(type, GetAllFieldsFunc());
private static Func<Type, IEnumerable<FieldInfo>> GetAllFieldsFunc() => t => t.GetFields(BindingFlags.Public | BindingFlags.Instance);
}
}

View File

@ -1,59 +1,54 @@
using System;
namespace Swan
{
namespace Swan {
/// <summary>
/// Represents a singleton pattern abstract class.
/// </summary>
/// <typeparam name="T">The type of class.</typeparam>
public abstract class SingletonBase<T> : IDisposable where T : class {
/// <summary>
/// Represents a singleton pattern abstract class.
/// The static, singleton instance reference.
/// </summary>
/// <typeparam name="T">The type of class.</typeparam>
public abstract class SingletonBase<T> : IDisposable
where T : class
{
/// <summary>
/// The static, singleton instance reference.
/// </summary>
protected static readonly Lazy<T> LazyInstance = new Lazy<T>(
valueFactory: () => Activator.CreateInstance(typeof(T), true) as T,
isThreadSafe: true);
private bool _isDisposing; // To detect redundant calls
/// <summary>
/// Gets the instance that this singleton represents.
/// If the instance is null, it is constructed and assigned when this member is accessed.
/// </summary>
/// <value>
/// The instance.
/// </value>
public static T Instance => LazyInstance.Value;
/// <inheritdoc />
public void Dispose() => Dispose(true);
/// <summary>
/// Releases unmanaged and - optionally - managed resources.
/// Call the GC.SuppressFinalize if you override this method and use
/// a non-default class finalizer (destructor).
/// </summary>
/// <param name="disposeManaged"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.</param>
protected virtual void Dispose(bool disposeManaged)
{
if (_isDisposing) return;
_isDisposing = true;
// free managed resources
if (LazyInstance == null) return;
try
{
var disposableInstance = LazyInstance.Value as IDisposable;
disposableInstance?.Dispose();
}
catch
{
// swallow
}
}
}
protected static readonly Lazy<T> LazyInstance = new Lazy<T>(valueFactory: () => Activator.CreateInstance(typeof(T), true) as T, isThreadSafe: true);
private Boolean _isDisposing; // To detect redundant calls
/// <summary>
/// Gets the instance that this singleton represents.
/// If the instance is null, it is constructed and assigned when this member is accessed.
/// </summary>
/// <value>
/// The instance.
/// </value>
public static T Instance => LazyInstance.Value;
/// <inheritdoc />
public void Dispose() => this.Dispose(true);
/// <summary>
/// Releases unmanaged and - optionally - managed resources.
/// Call the GC.SuppressFinalize if you override this method and use
/// a non-default class finalizer (destructor).
/// </summary>
/// <param name="disposeManaged"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.</param>
protected virtual void Dispose(Boolean disposeManaged) {
if(this._isDisposing) {
return;
}
this._isDisposing = true;
// free managed resources
if(LazyInstance == null) {
return;
}
try {
IDisposable disposableInstance = LazyInstance.Value as IDisposable;
disposableInstance?.Dispose();
} catch {
// swallow
}
}
}
}

View File

@ -1,76 +1,62 @@
using System;
using System.Runtime.Serialization;
namespace Swan
{
namespace Swan {
/// <summary>
/// The exception that is thrown when a conversion from a string to a
/// specified type fails.
/// </summary>
/// <seealso cref="FromString" />
[Serializable]
public class StringConversionException : Exception {
/// <summary>
/// The exception that is thrown when a conversion from a string to a
/// specified type fails.
/// Initializes a new instance of the <see cref="StringConversionException"/> class.
/// </summary>
/// <seealso cref="FromString" />
[Serializable]
public class StringConversionException : Exception
{
/// <summary>
/// Initializes a new instance of the <see cref="StringConversionException"/> class.
/// </summary>
public StringConversionException()
{
}
/// <summary>
/// Initializes a new instance of the <see cref="StringConversionException"/> class.
/// </summary>
/// <param name="message">The error message that explains the reason for the exception.</param>
public StringConversionException(string message)
: base(message)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="StringConversionException"/> class.
/// </summary>
/// <param name="message">The error message that explains the reason for the exception.</param>
/// <param name="innerException">The exception that is the cause of the current exception,
/// or <see langword="null" /> if no inner exception is specified.</param>
public StringConversionException(string message, Exception innerException)
: base(message, innerException)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="StringConversionException"/> class.
/// </summary>
/// <param name="type">The desired resulting type of the attempted conversion.</param>
public StringConversionException(Type type)
: base(BuildStandardMessageForType(type))
{
}
/// <summary>
/// Initializes a new instance of the <see cref="StringConversionException"/> class.
/// </summary>
/// <param name="type">The desired resulting type of the attempted conversion.</param>
/// <param name="innerException">The exception that is the cause of the current exception,
/// or <see langword="null" /> if no inner exception is specified.</param>
public StringConversionException(Type type, Exception innerException)
: base(BuildStandardMessageForType(type), innerException)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="StringConversionException"/> class.
/// </summary>
/// <param name="info">The <see cref="SerializationInfo" /> that holds the serialized object data
/// about the exception being thrown.</param>
/// <param name="context">The <see cref="StreamingContext" /> that contains contextual information
/// about the source or destination.</param>
protected StringConversionException(SerializationInfo info, StreamingContext context)
: base(info, context)
{
}
private static string BuildStandardMessageForType(Type type)
=> $"Cannot convert a string to an instance of {type.FullName}";
}
public StringConversionException() {
}
/// <summary>
/// Initializes a new instance of the <see cref="StringConversionException"/> class.
/// </summary>
/// <param name="message">The error message that explains the reason for the exception.</param>
public StringConversionException(String message) : base(message) {
}
/// <summary>
/// Initializes a new instance of the <see cref="StringConversionException"/> class.
/// </summary>
/// <param name="message">The error message that explains the reason for the exception.</param>
/// <param name="innerException">The exception that is the cause of the current exception,
/// or <see langword="null" /> if no inner exception is specified.</param>
public StringConversionException(String message, Exception innerException) : base(message, innerException) {
}
/// <summary>
/// Initializes a new instance of the <see cref="StringConversionException"/> class.
/// </summary>
/// <param name="type">The desired resulting type of the attempted conversion.</param>
public StringConversionException(Type type) : base(BuildStandardMessageForType(type)) {
}
/// <summary>
/// Initializes a new instance of the <see cref="StringConversionException"/> class.
/// </summary>
/// <param name="type">The desired resulting type of the attempted conversion.</param>
/// <param name="innerException">The exception that is the cause of the current exception,
/// or <see langword="null" /> if no inner exception is specified.</param>
public StringConversionException(Type type, Exception innerException) : base(BuildStandardMessageForType(type), innerException) {
}
/// <summary>
/// Initializes a new instance of the <see cref="StringConversionException"/> class.
/// </summary>
/// <param name="info">The <see cref="SerializationInfo" /> that holds the serialized object data
/// about the exception being thrown.</param>
/// <param name="context">The <see cref="StreamingContext" /> that contains contextual information
/// about the source or destination.</param>
protected StringConversionException(SerializationInfo info, StreamingContext context) : base(info, context) {
}
private static String BuildStandardMessageForType(Type type) => $"Cannot convert a string to an instance of {type.FullName}";
}
}

View File

@ -1,30 +1,27 @@
using System;
namespace Swan
{
namespace Swan {
/// <summary>
/// An attribute used to help conversion structs back and forth into arrays of bytes via
/// extension methods included in this library ToStruct and ToBytes.
/// </summary>
/// <seealso cref="Attribute" />
[AttributeUsage(AttributeTargets.Field | AttributeTargets.Struct)]
public class StructEndiannessAttribute : Attribute {
/// <summary>
/// An attribute used to help conversion structs back and forth into arrays of bytes via
/// extension methods included in this library ToStruct and ToBytes.
/// Initializes a new instance of the <see cref="StructEndiannessAttribute"/> class.
/// </summary>
/// <seealso cref="Attribute" />
[AttributeUsage(AttributeTargets.Field | AttributeTargets.Struct)]
public class StructEndiannessAttribute : Attribute
{
/// <summary>
/// Initializes a new instance of the <see cref="StructEndiannessAttribute"/> class.
/// </summary>
/// <param name="endianness">The endianness.</param>
public StructEndiannessAttribute(Endianness endianness)
{
Endianness = endianness;
}
/// <summary>
/// Gets the endianness.
/// </summary>
/// <value>
/// The endianness.
/// </value>
public Endianness Endianness { get; }
}
/// <param name="endianness">The endianness.</param>
public StructEndiannessAttribute(Endianness endianness) => this.Endianness = endianness;
/// <summary>
/// Gets the endianness.
/// </summary>
/// <value>
/// The endianness.
/// </value>
public Endianness Endianness {
get;
}
}
}

View File

@ -13,6 +13,5 @@
<PackageLicenseUrl>https://raw.githubusercontent.com/unosquare/swan/master/LICENSE</PackageLicenseUrl>
<PackageTags>best-practices netcore network objectmapper json-serialization</PackageTags>
<LangVersion>8.0</LangVersion>
<Nullable>enable</Nullable>
</PropertyGroup>
</Project>

View File

@ -4,230 +4,197 @@ using System.IO;
using System.Reflection;
using System.Threading;
namespace Swan
{
namespace Swan {
/// <summary>
/// Provides utility methods to retrieve information about the current application.
/// </summary>
public static class SwanRuntime {
private static readonly Lazy<Assembly> EntryAssemblyLazy = new Lazy<Assembly>(Assembly.GetEntryAssembly);
private static readonly Lazy<String> CompanyNameLazy = new Lazy<String>(() => {
AssemblyCompanyAttribute attribute = EntryAssembly.GetCustomAttribute(typeof(AssemblyCompanyAttribute)) as AssemblyCompanyAttribute;
return attribute?.Company ?? String.Empty;
});
private static readonly Lazy<String> ProductNameLazy = new Lazy<String>(() => {
AssemblyProductAttribute attribute = EntryAssembly.GetCustomAttribute(typeof(AssemblyProductAttribute)) as AssemblyProductAttribute;
return attribute?.Product ?? String.Empty;
});
private static readonly Lazy<String> ProductTrademarkLazy = new Lazy<String>(() => {
AssemblyTrademarkAttribute attribute = EntryAssembly.GetCustomAttribute(typeof(AssemblyTrademarkAttribute)) as AssemblyTrademarkAttribute;
return attribute?.Trademark ?? String.Empty;
});
private static readonly String ApplicationMutexName = "Global\\{{" + EntryAssembly.FullName + "}}";
private static readonly Object SyncLock = new Object();
private static OperatingSystem? _oS;
#region Properties
/// <summary>
/// Provides utility methods to retrieve information about the current application.
/// Gets the current Operating System.
/// </summary>
public static class SwanRuntime
{
private static readonly Lazy<Assembly> EntryAssemblyLazy = new Lazy<Assembly>(Assembly.GetEntryAssembly);
private static readonly Lazy<string> CompanyNameLazy = new Lazy<string>(() =>
{
var attribute =
EntryAssembly.GetCustomAttribute(typeof(AssemblyCompanyAttribute)) as AssemblyCompanyAttribute;
return attribute?.Company ?? string.Empty;
});
private static readonly Lazy<string> ProductNameLazy = new Lazy<string>(() =>
{
var attribute =
EntryAssembly.GetCustomAttribute(typeof(AssemblyProductAttribute)) as AssemblyProductAttribute;
return attribute?.Product ?? string.Empty;
});
private static readonly Lazy<string> ProductTrademarkLazy = new Lazy<string>(() =>
{
var attribute =
EntryAssembly.GetCustomAttribute(typeof(AssemblyTrademarkAttribute)) as AssemblyTrademarkAttribute;
return attribute?.Trademark ?? string.Empty;
});
private static readonly string ApplicationMutexName = "Global\\{{" + EntryAssembly.FullName + "}}";
private static readonly object SyncLock = new object();
private static OperatingSystem? _oS;
#region Properties
/// <summary>
/// Gets the current Operating System.
/// </summary>
/// <value>
/// The os.
/// </value>
public static OperatingSystem OS
{
get
{
if (_oS.HasValue == false)
{
var windowsDirectory = Environment.GetEnvironmentVariable("windir");
if (string.IsNullOrEmpty(windowsDirectory) == false
&& windowsDirectory.Contains(@"\")
&& Directory.Exists(windowsDirectory))
{
_oS = OperatingSystem.Windows;
}
else
{
_oS = File.Exists(@"/proc/sys/kernel/ostype") ? OperatingSystem.Unix : OperatingSystem.Osx;
}
}
return _oS ?? OperatingSystem.Unknown;
}
}
/// <summary>
/// Checks if this application (including version number) is the only instance currently running.
/// </summary>
/// <value>
/// <c>true</c> if this instance is the only instance; otherwise, <c>false</c>.
/// </value>
public static bool IsTheOnlyInstance
{
get
{
lock (SyncLock)
{
try
{
// Try to open existing mutex.
Mutex.OpenExisting(ApplicationMutexName);
}
catch
{
try
{
// If exception occurred, there is no such mutex.
var appMutex = new Mutex(true, ApplicationMutexName);
$"Application Mutex created {appMutex} named '{ApplicationMutexName}'".Debug(
typeof(SwanRuntime));
// Only one instance.
return true;
}
catch
{
// Sometimes the user can't create the Global Mutex
}
}
// More than one instance.
return false;
}
}
}
/// <summary>
/// Gets a value indicating whether this application instance is using the MONO runtime.
/// </summary>
/// <value>
/// <c>true</c> if this instance is using MONO runtime; otherwise, <c>false</c>.
/// </value>
public static bool IsUsingMonoRuntime => Type.GetType("Mono.Runtime") != null;
/// <summary>
/// Gets the assembly that started the application.
/// </summary>
/// <value>
/// The entry assembly.
/// </value>
public static Assembly EntryAssembly => EntryAssemblyLazy.Value;
/// <summary>
/// Gets the name of the entry assembly.
/// </summary>
/// <value>
/// The name of the entry assembly.
/// </value>
public static AssemblyName EntryAssemblyName => EntryAssemblyLazy.Value.GetName();
/// <summary>
/// Gets the entry assembly version.
/// </summary>
public static Version EntryAssemblyVersion => EntryAssemblyName.Version;
/// <summary>
/// Gets the full path to the folder containing the assembly that started the application.
/// </summary>
/// <value>
/// The entry assembly directory.
/// </value>
public static string EntryAssemblyDirectory
{
get
{
var uri = new UriBuilder(EntryAssembly.CodeBase);
var path = Uri.UnescapeDataString(uri.Path);
return Path.GetDirectoryName(path);
}
}
/// <summary>
/// Gets the name of the company.
/// </summary>
/// <value>
/// The name of the company.
/// </value>
public static string CompanyName => CompanyNameLazy.Value;
/// <summary>
/// Gets the name of the product.
/// </summary>
/// <value>
/// The name of the product.
/// </value>
public static string ProductName => ProductNameLazy.Value;
/// <summary>
/// Gets the trademark.
/// </summary>
/// <value>
/// The product trademark.
/// </value>
public static string ProductTrademark => ProductTrademarkLazy.Value;
/// <summary>
/// Gets a local storage path with a version.
/// </summary>
/// <value>
/// The local storage path.
/// </value>
public static string LocalStoragePath
{
get
{
var localAppDataPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData),
EntryAssemblyName.Name);
var returnPath = Path.Combine(localAppDataPath, EntryAssemblyVersion.ToString());
if (!Directory.Exists(returnPath))
{
Directory.CreateDirectory(returnPath);
}
return returnPath;
}
}
#endregion
#region Methods
/// <summary>
/// Build a full path pointing to the current user's desktop with the given filename.
/// </summary>
/// <param name="filename">The filename.</param>
/// <returns>
/// The fully qualified location of path, such as "C:\MyFile.txt".
/// </returns>
/// <exception cref="ArgumentNullException">filename.</exception>
public static string GetDesktopFilePath(string filename)
{
if (string.IsNullOrWhiteSpace(filename))
throw new ArgumentNullException(nameof(filename));
var pathWithFilename = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory),
filename);
return Path.GetFullPath(pathWithFilename);
}
#endregion
}
/// <value>
/// The os.
/// </value>
public static OperatingSystem OS {
get {
if(_oS.HasValue == false) {
String windowsDirectory = Environment.GetEnvironmentVariable("windir");
_oS = String.IsNullOrEmpty(windowsDirectory) == false && windowsDirectory.Contains(@"\") && Directory.Exists(windowsDirectory)
? (OperatingSystem?)OperatingSystem.Windows
: (OperatingSystem?)(File.Exists(@"/proc/sys/kernel/ostype") ? OperatingSystem.Unix : OperatingSystem.Osx);
}
return _oS ?? OperatingSystem.Unknown;
}
}
/// <summary>
/// Checks if this application (including version number) is the only instance currently running.
/// </summary>
/// <value>
/// <c>true</c> if this instance is the only instance; otherwise, <c>false</c>.
/// </value>
[System.Diagnostics.CodeAnalysis.SuppressMessage("Codequalität", "IDE0067:Objekte verwerfen, bevor Bereich verloren geht", Justification = "<Ausstehend>")]
public static Boolean IsTheOnlyInstance {
get {
lock(SyncLock) {
try {
// Try to open existing mutex.
_ = Mutex.OpenExisting(ApplicationMutexName);
} catch {
try {
// If exception occurred, there is no such mutex.
Mutex appMutex = new Mutex(true, ApplicationMutexName);
$"Application Mutex created {appMutex} named '{ApplicationMutexName}'".Debug(typeof(SwanRuntime));
// Only one instance.
return true;
} catch {
// Sometimes the user can't create the Global Mutex
}
}
// More than one instance.
return false;
}
}
}
/// <summary>
/// Gets a value indicating whether this application instance is using the MONO runtime.
/// </summary>
/// <value>
/// <c>true</c> if this instance is using MONO runtime; otherwise, <c>false</c>.
/// </value>
public static Boolean IsUsingMonoRuntime => Type.GetType("Mono.Runtime") != null;
/// <summary>
/// Gets the assembly that started the application.
/// </summary>
/// <value>
/// The entry assembly.
/// </value>
public static Assembly EntryAssembly => EntryAssemblyLazy.Value;
/// <summary>
/// Gets the name of the entry assembly.
/// </summary>
/// <value>
/// The name of the entry assembly.
/// </value>
public static AssemblyName EntryAssemblyName => EntryAssemblyLazy.Value.GetName();
/// <summary>
/// Gets the entry assembly version.
/// </summary>
public static Version EntryAssemblyVersion => EntryAssemblyName.Version;
/// <summary>
/// Gets the full path to the folder containing the assembly that started the application.
/// </summary>
/// <value>
/// The entry assembly directory.
/// </value>
public static String EntryAssemblyDirectory {
get {
UriBuilder uri = new UriBuilder(EntryAssembly.CodeBase);
String path = Uri.UnescapeDataString(uri.Path);
return Path.GetDirectoryName(path);
}
}
/// <summary>
/// Gets the name of the company.
/// </summary>
/// <value>
/// The name of the company.
/// </value>
public static String CompanyName => CompanyNameLazy.Value;
/// <summary>
/// Gets the name of the product.
/// </summary>
/// <value>
/// The name of the product.
/// </value>
public static String ProductName => ProductNameLazy.Value;
/// <summary>
/// Gets the trademark.
/// </summary>
/// <value>
/// The product trademark.
/// </value>
public static String ProductTrademark => ProductTrademarkLazy.Value;
/// <summary>
/// Gets a local storage path with a version.
/// </summary>
/// <value>
/// The local storage path.
/// </value>
public static String LocalStoragePath {
get {
String localAppDataPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), EntryAssemblyName.Name);
String returnPath = Path.Combine(localAppDataPath, EntryAssemblyVersion.ToString());
if(!Directory.Exists(returnPath)) {
_ = Directory.CreateDirectory(returnPath);
}
return returnPath;
}
}
#endregion
#region Methods
/// <summary>
/// Build a full path pointing to the current user's desktop with the given filename.
/// </summary>
/// <param name="filename">The filename.</param>
/// <returns>
/// The fully qualified location of path, such as "C:\MyFile.txt".
/// </returns>
/// <exception cref="ArgumentNullException">filename.</exception>
public static String GetDesktopFilePath(String filename) {
if(String.IsNullOrWhiteSpace(filename)) {
throw new ArgumentNullException(nameof(filename));
}
String pathWithFilename = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory), filename);
return Path.GetFullPath(pathWithFilename);
}
#endregion
}
}

View File

@ -1,37 +1,36 @@
namespace Swan
{
using System;
namespace Swan {
/// <summary>
/// A console terminal helper to create nicer output and receive input from the user
/// This class is thread-safe :).
/// </summary>
public static partial class Terminal {
/// <summary>
/// A console terminal helper to create nicer output and receive input from the user
/// This class is thread-safe :).
/// Represents a Table to print in console.
/// </summary>
public static partial class Terminal
{
/// <summary>
/// Represents a Table to print in console.
/// </summary>
private static class Table
{
public static void Vertical() => Write('\u2502', Settings.BorderColor);
public static void RightTee() => Write('\u2524', Settings.BorderColor);
public static void TopRight() => Write('\u2510', Settings.BorderColor);
public static void BottomLeft() => Write('\u2514', Settings.BorderColor);
public static void BottomTee() => Write('\u2534', Settings.BorderColor);
public static void TopTee() => Write('\u252c', Settings.BorderColor);
public static void LeftTee() => Write('\u251c', Settings.BorderColor);
public static void Horizontal(int length) => Write(new string('\u2500', length), Settings.BorderColor);
public static void Tee() => Write('\u253c', Settings.BorderColor);
public static void BottomRight() => Write('\u2518', Settings.BorderColor);
public static void TopLeft() => Write('\u250C', Settings.BorderColor);
}
}
private static class Table {
public static void Vertical() => Write('\u2502', Settings.BorderColor);
public static void RightTee() => Write('\u2524', Settings.BorderColor);
public static void TopRight() => Write('\u2510', Settings.BorderColor);
public static void BottomLeft() => Write('\u2514', Settings.BorderColor);
public static void BottomTee() => Write('\u2534', Settings.BorderColor);
public static void TopTee() => Write('\u252c', Settings.BorderColor);
public static void LeftTee() => Write('\u251c', Settings.BorderColor);
public static void Horizontal(Int32 length) => Write(new String('\u2500', length), Settings.BorderColor);
public static void Tee() => Write('\u253c', Settings.BorderColor);
public static void BottomRight() => Write('\u2518', Settings.BorderColor);
public static void TopLeft() => Write('\u250C', Settings.BorderColor);
}
}
}

View File

@ -1,261 +1,238 @@
using System;
#nullable enable
using System;
using System.Collections.Generic;
using Swan.Lite.Logging;
using System.Globalization;
using Swan.Logging;
using Swan.Lite.Logging;
namespace Swan
{
namespace Swan {
/// <summary>
/// A console terminal helper to create nicer output and receive input from the user
/// This class is thread-safe :).
/// </summary>
public static partial class Terminal {
#region ReadKey
/// <summary>
/// A console terminal helper to create nicer output and receive input from the user
/// This class is thread-safe :).
/// Reads a key from the Terminal. This is the closest equivalent to Console.ReadKey.
/// </summary>
public static partial class Terminal
{
#region ReadKey
/// <summary>
/// Reads a key from the Terminal. This is the closest equivalent to Console.ReadKey.
/// </summary>
/// <param name="intercept">if set to <c>true</c> the pressed key will not be rendered to the output.</param>
/// <param name="disableLocking">if set to <c>true</c> the output will continue to be shown.
/// This is useful for services and daemons that are running as console applications and wait for a key to exit the program.</param>
/// <returns>The console key information.</returns>
public static ConsoleKeyInfo ReadKey(bool intercept, bool disableLocking = false)
{
if (!IsConsolePresent) return default;
if (disableLocking) return Console.ReadKey(intercept);
lock (SyncLock)
{
Flush();
InputDone.Reset();
try
{
Console.CursorVisible = true;
return Console.ReadKey(intercept);
}
finally
{
Console.CursorVisible = false;
InputDone.Set();
}
}
}
/// <summary>
/// Reads a key from the Terminal.
/// </summary>
/// <param name="prompt">The prompt.</param>
/// <param name="preventEcho">if set to <c>true</c> [prevent echo].</param>
/// <returns>The console key information.</returns>
public static ConsoleKeyInfo ReadKey(string prompt, bool preventEcho = true)
{
if (!IsConsolePresent) return default;
lock (SyncLock)
{
if (prompt != null)
{
Write($"{GetNowFormatted()}{Settings.UserInputPrefix} << {prompt} ", ConsoleColor.White);
}
var input = ReadKey(true);
var echo = preventEcho ? string.Empty : input.Key.ToString();
WriteLine(echo);
return input;
}
}
#endregion
#region Other Terminal Read Methods
/// <summary>
/// Clears the screen.
/// </summary>
public static void Clear()
{
Flush();
Console.Clear();
}
/// <summary>
/// Reads a line of text from the console.
/// </summary>
/// <returns>The read line.</returns>
public static string? ReadLine()
{
if (IsConsolePresent == false) return default;
lock (SyncLock)
{
Flush();
InputDone.Reset();
try
{
Console.CursorVisible = true;
return Console.ReadLine();
}
finally
{
Console.CursorVisible = false;
InputDone.Set();
}
}
}
/// <summary>
/// Reads a line from the input.
/// </summary>
/// <param name="prompt">The prompt.</param>
/// <returns>The read line.</returns>
public static string? ReadLine(string prompt)
{
if (!IsConsolePresent) return null;
lock (SyncLock)
{
Write($"{GetNowFormatted()}{Settings.UserInputPrefix} << {prompt}: ", ConsoleColor.White);
return ReadLine();
}
}
/// <summary>
/// Reads a number from the input. If unable to parse, it returns the default number.
/// </summary>
/// <param name="prompt">The prompt.</param>
/// <param name="defaultNumber">The default number.</param>
/// <returns>
/// Conversions of string representation of a number to its 32-bit signed integer equivalent.
/// </returns>
public static int ReadNumber(string prompt, int defaultNumber)
{
if (!IsConsolePresent) return defaultNumber;
lock (SyncLock)
{
Write($"{GetNowFormatted()}{Settings.UserInputPrefix} << {prompt} (default is {defaultNumber}): ",
ConsoleColor.White);
var input = ReadLine();
return int.TryParse(input, out var parsedInt) ? parsedInt : defaultNumber;
}
}
/// <summary>
/// Creates a table prompt where the user can enter an option based on the options dictionary provided.
/// </summary>
/// <param name="title">The title.</param>
/// <param name="options">The options.</param>
/// <param name="anyKeyOption">Any key option.</param>
/// <returns>
/// A value that identifies the console key that was pressed.
/// </returns>
/// <exception cref="ArgumentNullException">options.</exception>
public static ConsoleKeyInfo ReadPrompt(
string title,
IDictionary<ConsoleKey, string> options,
string anyKeyOption)
{
if (!IsConsolePresent) return default;
if (options == null)
throw new ArgumentNullException(nameof(options));
const ConsoleColor textColor = ConsoleColor.White;
var lineLength = Console.WindowWidth;
var lineAlign = -(lineLength - 2);
var textFormat = "{0," + lineAlign + "}";
// lock the output as an atomic operation
lock (SyncLock)
{
{
// Top border
Table.TopLeft();
Table.Horizontal(-lineAlign);
Table.TopRight();
}
{
// Title
Table.Vertical();
var titleText = string.Format(CultureInfo.CurrentCulture,
textFormat,
string.IsNullOrWhiteSpace(title) ? " Select an option from the list below." : $" {title}");
Write(titleText, textColor);
Table.Vertical();
}
{
// Title Bottom
Table.LeftTee();
Table.Horizontal(lineLength - 2);
Table.RightTee();
}
// Options
foreach (var kvp in options)
{
Table.Vertical();
Write(string.Format(
CultureInfo.CurrentCulture,
textFormat,
$" {"[ " + kvp.Key + " ]",-10} {kvp.Value}"),
textColor);
Table.Vertical();
}
// Any Key Options
if (string.IsNullOrWhiteSpace(anyKeyOption) == false)
{
Table.Vertical();
Write(string.Format(CultureInfo.CurrentCulture, textFormat, " "), ConsoleColor.Gray);
Table.Vertical();
Table.Vertical();
Write(string.Format(
CultureInfo.CurrentCulture,
textFormat,
$" {" ",-10} {anyKeyOption}"),
ConsoleColor.Gray);
Table.Vertical();
}
{
// Input section
Table.LeftTee();
Table.Horizontal(lineLength - 2);
Table.RightTee();
Table.Vertical();
Write(string.Format(CultureInfo.CurrentCulture, textFormat, Settings.UserOptionText),
ConsoleColor.Green);
Table.Vertical();
Table.BottomLeft();
Table.Horizontal(lineLength - 2);
Table.BottomRight();
}
}
var inputLeft = Settings.UserOptionText.Length + 3;
SetCursorPosition(inputLeft, CursorTop - 1);
var userInput = ReadKey(true);
Write(userInput.Key.ToString(), ConsoleColor.Gray);
SetCursorPosition(0, CursorTop + 2);
return userInput;
}
#endregion
private static string GetNowFormatted() =>
$" {(string.IsNullOrWhiteSpace(TextLogger.LoggingTimeFormat) ? string.Empty : DateTime.Now.ToString(TextLogger.LoggingTimeFormat) + " ")}";
}
/// <param name="intercept">if set to <c>true</c> the pressed key will not be rendered to the output.</param>
/// <param name="disableLocking">if set to <c>true</c> the output will continue to be shown.
/// This is useful for services and daemons that are running as console applications and wait for a key to exit the program.</param>
/// <returns>The console key information.</returns>
public static ConsoleKeyInfo ReadKey(Boolean intercept, Boolean disableLocking = false) {
if(!IsConsolePresent) {
return default;
}
if(disableLocking) {
return Console.ReadKey(intercept);
}
lock(SyncLock) {
Flush();
InputDone.Reset();
try {
Console.CursorVisible = true;
return Console.ReadKey(intercept);
} finally {
Console.CursorVisible = false;
InputDone.Set();
}
}
}
/// <summary>
/// Reads a key from the Terminal.
/// </summary>
/// <param name="prompt">The prompt.</param>
/// <param name="preventEcho">if set to <c>true</c> [prevent echo].</param>
/// <returns>The console key information.</returns>
public static ConsoleKeyInfo ReadKey(String prompt, Boolean preventEcho = true) {
if(!IsConsolePresent) {
return default;
}
lock(SyncLock) {
if(prompt != null) {
Write($"{GetNowFormatted()}{Settings.UserInputPrefix} << {prompt} ", ConsoleColor.White);
}
ConsoleKeyInfo input = ReadKey(true);
String echo = preventEcho ? String.Empty : input.Key.ToString();
WriteLine(echo);
return input;
}
}
#endregion
#region Other Terminal Read Methods
/// <summary>
/// Clears the screen.
/// </summary>
public static void Clear() {
Flush();
Console.Clear();
}
/// <summary>
/// Reads a line of text from the console.
/// </summary>
/// <returns>The read line.</returns>
public static String? ReadLine() {
if(IsConsolePresent == false) {
return default;
}
lock(SyncLock) {
Flush();
InputDone.Reset();
try {
Console.CursorVisible = true;
return Console.ReadLine();
} finally {
Console.CursorVisible = false;
InputDone.Set();
}
}
}
/// <summary>
/// Reads a line from the input.
/// </summary>
/// <param name="prompt">The prompt.</param>
/// <returns>The read line.</returns>
public static String? ReadLine(String prompt) {
if(!IsConsolePresent) {
return null;
}
lock(SyncLock) {
Write($"{GetNowFormatted()}{Settings.UserInputPrefix} << {prompt}: ", ConsoleColor.White);
return ReadLine();
}
}
/// <summary>
/// Reads a number from the input. If unable to parse, it returns the default number.
/// </summary>
/// <param name="prompt">The prompt.</param>
/// <param name="defaultNumber">The default number.</param>
/// <returns>
/// Conversions of string representation of a number to its 32-bit signed integer equivalent.
/// </returns>
public static Int32 ReadNumber(String prompt, Int32 defaultNumber) {
if(!IsConsolePresent) {
return defaultNumber;
}
lock(SyncLock) {
Write($"{GetNowFormatted()}{Settings.UserInputPrefix} << {prompt} (default is {defaultNumber}): ", ConsoleColor.White);
String? input = ReadLine();
return Int32.TryParse(input, out Int32 parsedInt) ? parsedInt : defaultNumber;
}
}
/// <summary>
/// Creates a table prompt where the user can enter an option based on the options dictionary provided.
/// </summary>
/// <param name="title">The title.</param>
/// <param name="options">The options.</param>
/// <param name="anyKeyOption">Any key option.</param>
/// <returns>
/// A value that identifies the console key that was pressed.
/// </returns>
/// <exception cref="ArgumentNullException">options.</exception>
public static ConsoleKeyInfo ReadPrompt(String title, IDictionary<ConsoleKey, String> options, String anyKeyOption) {
if(!IsConsolePresent) {
return default;
}
if(options == null) {
throw new ArgumentNullException(nameof(options));
}
const ConsoleColor textColor = ConsoleColor.White;
Int32 lineLength = Console.WindowWidth;
Int32 lineAlign = -(lineLength - 2);
String textFormat = "{0," + lineAlign + "}";
// lock the output as an atomic operation
lock(SyncLock) {
{
// Top border
Table.TopLeft();
Table.Horizontal(-lineAlign);
Table.TopRight();
}
{
// Title
Table.Vertical();
String titleText = String.Format(CultureInfo.CurrentCulture, textFormat, String.IsNullOrWhiteSpace(title) ? " Select an option from the list below." : $" {title}");
Write(titleText, textColor);
Table.Vertical();
}
{
// Title Bottom
Table.LeftTee();
Table.Horizontal(lineLength - 2);
Table.RightTee();
}
// Options
foreach(KeyValuePair<ConsoleKey, String> kvp in options) {
Table.Vertical();
Write(String.Format(CultureInfo.CurrentCulture, textFormat, $" {"[ " + kvp.Key + " ]",-10} {kvp.Value}"), textColor);
Table.Vertical();
}
// Any Key Options
if(String.IsNullOrWhiteSpace(anyKeyOption) == false) {
Table.Vertical();
Write(String.Format(CultureInfo.CurrentCulture, textFormat, " "), ConsoleColor.Gray);
Table.Vertical();
Table.Vertical();
Write(String.Format(CultureInfo.CurrentCulture, textFormat, $" {" ",-10} {anyKeyOption}"), ConsoleColor.Gray);
Table.Vertical();
}
{
// Input section
Table.LeftTee();
Table.Horizontal(lineLength - 2);
Table.RightTee();
Table.Vertical();
Write(String.Format(CultureInfo.CurrentCulture, textFormat, Settings.UserOptionText), ConsoleColor.Green);
Table.Vertical();
Table.BottomLeft();
Table.Horizontal(lineLength - 2);
Table.BottomRight();
}
}
Int32 inputLeft = Settings.UserOptionText.Length + 3;
SetCursorPosition(inputLeft, CursorTop - 1);
ConsoleKeyInfo userInput = ReadKey(true);
Write(userInput.Key.ToString(), ConsoleColor.Gray);
SetCursorPosition(0, CursorTop + 2);
return userInput;
}
#endregion
private static String GetNowFormatted() => $" {(String.IsNullOrWhiteSpace(TextLogger.LoggingTimeFormat) ? String.Empty : DateTime.Now.ToString(TextLogger.LoggingTimeFormat) + " ")}";
}
}

View File

@ -1,97 +1,88 @@
using System;
#nullable enable
using System;
namespace Swan
{
namespace Swan {
/// <summary>
/// A console terminal helper to create nicer output and receive input from the user
/// This class is thread-safe :).
/// </summary>
public static partial class Terminal {
/// <summary>
/// A console terminal helper to create nicer output and receive input from the user
/// This class is thread-safe :).
/// Writes a character a number of times, optionally adding a new line at the end.
/// </summary>
public static partial class Terminal
{
/// <summary>
/// Writes a character a number of times, optionally adding a new line at the end.
/// </summary>
/// <param name="charCode">The character code.</param>
/// <param name="color">The color.</param>
/// <param name="count">The count.</param>
/// <param name="newLine">if set to <c>true</c> [new line].</param>
/// <param name="writerFlags">The writer flags.</param>
public static void Write(char charCode, ConsoleColor? color = null, int count = 1, bool newLine = false, TerminalWriters writerFlags = TerminalWriters.StandardOutput)
{
lock (SyncLock)
{
var text = new string(charCode, count);
if (newLine)
{
text += Environment.NewLine;
}
var buffer = OutputEncoding.GetBytes(text);
var context = new OutputContext
{
OutputColor = color ?? Settings.DefaultColor,
OutputText = OutputEncoding.GetChars(buffer),
OutputWriters = writerFlags,
};
EnqueueOutput(context);
}
}
/// <summary>
/// Writes the specified text in the given color.
/// </summary>
/// <param name="text">The text.</param>
/// <param name="color">The color.</param>
/// <param name="writerFlags">The writer flags.</param>
public static void Write(string? text, ConsoleColor? color = null, TerminalWriters writerFlags = TerminalWriters.StandardOutput)
{
if (text == null) return;
lock (SyncLock)
{
var buffer = OutputEncoding.GetBytes(text);
var context = new OutputContext
{
OutputColor = color ?? Settings.DefaultColor,
OutputText = OutputEncoding.GetChars(buffer),
OutputWriters = writerFlags,
};
EnqueueOutput(context);
}
}
/// <summary>
/// Writes a New Line Sequence to the standard output.
/// </summary>
/// <param name="writerFlags">The writer flags.</param>
public static void WriteLine(TerminalWriters writerFlags = TerminalWriters.StandardOutput)
=> Write(Environment.NewLine, Settings.DefaultColor, writerFlags);
/// <summary>
/// Writes a line of text in the current console foreground color
/// to the standard output.
/// </summary>
/// <param name="text">The text.</param>
/// <param name="color">The color.</param>
/// <param name="writerFlags">The writer flags.</param>
public static void WriteLine(string text, ConsoleColor? color = null, TerminalWriters writerFlags = TerminalWriters.StandardOutput)
=> Write($"{text ?? string.Empty}{Environment.NewLine}", color, writerFlags);
/// <summary>
/// As opposed to WriteLine methods, it prepends a Carriage Return character to the text
/// so that the console moves the cursor one position up after the text has been written out.
/// </summary>
/// <param name="text">The text.</param>
/// <param name="color">The color.</param>
/// <param name="writerFlags">The writer flags.</param>
public static void OverwriteLine(string text, ConsoleColor? color = null, TerminalWriters writerFlags = TerminalWriters.StandardOutput)
{
Write($"\r{text ?? string.Empty}", color, writerFlags);
Flush();
CursorLeft = 0;
}
}
/// <param name="charCode">The character code.</param>
/// <param name="color">The color.</param>
/// <param name="count">The count.</param>
/// <param name="newLine">if set to <c>true</c> [new line].</param>
/// <param name="writerFlags">The writer flags.</param>
public static void Write(Char charCode, ConsoleColor? color = null, Int32 count = 1, Boolean newLine = false, TerminalWriters writerFlags = TerminalWriters.StandardOutput) {
lock(SyncLock) {
String text = new String(charCode, count);
if(newLine) {
text += Environment.NewLine;
}
Byte[] buffer = OutputEncoding.GetBytes(text);
OutputContext context = new OutputContext {
OutputColor = color ?? Settings.DefaultColor,
OutputText = OutputEncoding.GetChars(buffer),
OutputWriters = writerFlags,
};
EnqueueOutput(context);
}
}
/// <summary>
/// Writes the specified text in the given color.
/// </summary>
/// <param name="text">The text.</param>
/// <param name="color">The color.</param>
/// <param name="writerFlags">The writer flags.</param>
public static void Write(String? text, ConsoleColor? color = null, TerminalWriters writerFlags = TerminalWriters.StandardOutput) {
if(text == null) {
return;
}
lock(SyncLock) {
Byte[] buffer = OutputEncoding.GetBytes(text);
OutputContext context = new OutputContext {
OutputColor = color ?? Settings.DefaultColor,
OutputText = OutputEncoding.GetChars(buffer),
OutputWriters = writerFlags,
};
EnqueueOutput(context);
}
}
/// <summary>
/// Writes a New Line Sequence to the standard output.
/// </summary>
/// <param name="writerFlags">The writer flags.</param>
public static void WriteLine(TerminalWriters writerFlags = TerminalWriters.StandardOutput) => Write(Environment.NewLine, Settings.DefaultColor, writerFlags);
/// <summary>
/// Writes a line of text in the current console foreground color
/// to the standard output.
/// </summary>
/// <param name="text">The text.</param>
/// <param name="color">The color.</param>
/// <param name="writerFlags">The writer flags.</param>
public static void WriteLine(String text, ConsoleColor? color = null, TerminalWriters writerFlags = TerminalWriters.StandardOutput) => Write($"{text ?? String.Empty}{Environment.NewLine}", color, writerFlags);
/// <summary>
/// As opposed to WriteLine methods, it prepends a Carriage Return character to the text
/// so that the console moves the cursor one position up after the text has been written out.
/// </summary>
/// <param name="text">The text.</param>
/// <param name="color">The color.</param>
/// <param name="writerFlags">The writer flags.</param>
public static void OverwriteLine(String text, ConsoleColor? color = null, TerminalWriters writerFlags = TerminalWriters.StandardOutput) {
Write($"\r{text ?? String.Empty}", color, writerFlags);
Flush();
CursorLeft = 0;
}
}
}

View File

@ -1,49 +1,46 @@
using System;
namespace Swan
{
namespace Swan {
/// <summary>
/// A console terminal helper to create nicer output and receive input from the user
/// This class is thread-safe :).
/// </summary>
public static partial class Terminal {
/// <summary>
/// A console terminal helper to create nicer output and receive input from the user
/// This class is thread-safe :).
/// Terminal global settings.
/// </summary>
public static partial class Terminal
{
/// <summary>
/// Terminal global settings.
/// </summary>
public static class Settings
{
/// <summary>
/// Gets or sets the default output color.
/// </summary>
/// <value>
/// The default color.
/// </value>
public static ConsoleColor DefaultColor { get; set; } = Console.ForegroundColor;
/// <summary>
/// Gets the color of the border.
/// </summary>
/// <value>
/// The color of the border.
/// </value>
public static ConsoleColor BorderColor { get; } = ConsoleColor.DarkGreen;
/// <summary>
/// Gets or sets the user input prefix.
/// </summary>
/// <value>
/// The user input prefix.
/// </value>
public static string UserInputPrefix { get; set; } = "USR";
/// <summary>
/// Gets or sets the user option text.
/// </summary>
/// <value>
/// The user option text.
/// </value>
public static string UserOptionText { get; set; } = " Option: ";
}
}
public static class Settings {
/// <summary>
/// Gets or sets the default output color.
/// </summary>
/// <value>
/// The default color.
/// </value>
public static ConsoleColor DefaultColor { get; set; } = Console.ForegroundColor;
/// <summary>
/// Gets the color of the border.
/// </summary>
/// <value>
/// The color of the border.
/// </value>
public static ConsoleColor BorderColor { get; } = ConsoleColor.DarkGreen;
/// <summary>
/// Gets or sets the user input prefix.
/// </summary>
/// <value>
/// The user input prefix.
/// </value>
public static String UserInputPrefix { get; set; } = "USR";
/// <summary>
/// Gets or sets the user option text.
/// </summary>
/// <value>
/// The user option text.
/// </value>
public static String UserOptionText { get; set; } = " Option: ";
}
}
}

View File

@ -2,338 +2,329 @@
using System.Collections.Concurrent;
using System.Text;
using System.Threading;
using Swan.Threading;
namespace Swan
{
namespace Swan {
/// <summary>
/// A console terminal helper to create nicer output and receive input from the user.
/// This class is thread-safe :).
/// </summary>
public static partial class Terminal {
#region Private Declarations
private const Int32 OutputFlushInterval = 15;
private static readonly ExclusiveTimer DequeueOutputTimer;
private static readonly Object SyncLock = new Object();
private static readonly ConcurrentQueue<OutputContext> OutputQueue = new ConcurrentQueue<OutputContext>();
private static readonly ManualResetEventSlim OutputDone = new ManualResetEventSlim(false);
private static readonly ManualResetEventSlim InputDone = new ManualResetEventSlim(true);
private static Boolean? _isConsolePresent;
#endregion
#region Constructors
/// <summary>
/// A console terminal helper to create nicer output and receive input from the user.
/// This class is thread-safe :).
/// Initializes static members of the <see cref="Terminal"/> class.
/// </summary>
public static partial class Terminal
{
#region Private Declarations
private const int OutputFlushInterval = 15;
private static readonly ExclusiveTimer DequeueOutputTimer;
private static readonly object SyncLock = new object();
private static readonly ConcurrentQueue<OutputContext> OutputQueue = new ConcurrentQueue<OutputContext>();
private static readonly ManualResetEventSlim OutputDone = new ManualResetEventSlim(false);
private static readonly ManualResetEventSlim InputDone = new ManualResetEventSlim(true);
private static bool? _isConsolePresent;
#endregion
#region Constructors
/// <summary>
/// Initializes static members of the <see cref="Terminal"/> class.
/// </summary>
static Terminal()
{
lock (SyncLock)
{
if (DequeueOutputTimer != null) return;
if (IsConsolePresent)
{
Console.CursorVisible = false;
}
// Here we start the output task, fire-and-forget
DequeueOutputTimer = new ExclusiveTimer(DequeueOutputCycle);
DequeueOutputTimer.Resume(OutputFlushInterval);
}
}
#endregion
#region Synchronized Cursor Movement
/// <summary>
/// Gets or sets the cursor left position.
/// </summary>
/// <value>
/// The cursor left.
/// </value>
public static int CursorLeft
{
get
{
if (IsConsolePresent == false) return -1;
lock (SyncLock)
{
Flush();
return Console.CursorLeft;
}
}
set
{
if (IsConsolePresent == false) return;
lock (SyncLock)
{
Flush();
Console.CursorLeft = value;
}
}
}
/// <summary>
/// Gets or sets the cursor top position.
/// </summary>
/// <value>
/// The cursor top.
/// </value>
public static int CursorTop
{
get
{
if (IsConsolePresent == false) return -1;
lock (SyncLock)
{
Flush();
return Console.CursorTop;
}
}
set
{
if (IsConsolePresent == false) return;
lock (SyncLock)
{
Flush();
Console.CursorTop = value;
}
}
}
#endregion
#region Properties
/// <summary>
/// Gets a value indicating whether the Console is present.
/// </summary>
/// <value>
/// <c>true</c> if this instance is console present; otherwise, <c>false</c>.
/// </value>
public static bool IsConsolePresent
{
get
{
if (_isConsolePresent == null)
{
_isConsolePresent = true;
try
{
var windowHeight = Console.WindowHeight;
_isConsolePresent = windowHeight >= 0;
}
catch
{
_isConsolePresent = false;
}
}
return _isConsolePresent.Value;
}
}
/// <summary>
/// Gets the available output writers in a bitwise mask.
/// </summary>
/// <value>
/// The available writers.
/// </value>
public static TerminalWriters AvailableWriters =>
IsConsolePresent
? TerminalWriters.StandardError | TerminalWriters.StandardOutput
: TerminalWriters.None;
/// <summary>
/// Gets or sets the output encoding for the current console.
/// </summary>
/// <value>
/// The output encoding.
/// </value>
public static Encoding OutputEncoding
{
get => Console.OutputEncoding;
set => Console.OutputEncoding = value;
}
#endregion
#region Methods
/// <summary>
/// Waits for all of the queued output messages to be written out to the console.
/// Call this method if it is important to display console text before
/// quitting the application such as showing usage or help.
/// Set the timeout to null or TimeSpan.Zero to wait indefinitely.
/// </summary>
/// <param name="timeout">The timeout. Set the amount of time to black before this method exits.</param>
public static void Flush(TimeSpan? timeout = null)
{
if (timeout == null) timeout = TimeSpan.Zero;
var startTime = DateTime.UtcNow;
while (OutputQueue.Count > 0)
{
// Manually trigger a timer cycle to run immediately
DequeueOutputTimer.Change(0, OutputFlushInterval);
// Wait for the output to finish
if (OutputDone.Wait(OutputFlushInterval))
break;
// infinite timeout
if (timeout.Value == TimeSpan.Zero)
continue;
// break if we have reached a timeout condition
if (DateTime.UtcNow.Subtract(startTime) >= timeout.Value)
break;
}
}
/// <summary>
/// Sets the cursor position.
/// </summary>
/// <param name="left">The left.</param>
/// <param name="top">The top.</param>
public static void SetCursorPosition(int left, int top)
{
if (!IsConsolePresent) return;
lock (SyncLock)
{
Flush();
Console.SetCursorPosition(left.Clamp(0, left), top.Clamp(0, top));
}
}
/// <summary>
/// Moves the output cursor one line up starting at left position 0
/// Please note that backlining the cursor does not clear the contents of the
/// previous line so you might need to clear it by writing an empty string the
/// length of the console width.
/// </summary>
public static void BacklineCursor() => SetCursorPosition(0, CursorTop - 1);
/// <summary>
/// Writes a standard banner to the standard output
/// containing the company name, product name, assembly version and trademark.
/// </summary>
/// <param name="color">The color.</param>
public static void WriteWelcomeBanner(ConsoleColor color = ConsoleColor.Gray)
{
WriteLine($"{SwanRuntime.CompanyName} {SwanRuntime.ProductName} [Version {SwanRuntime.EntryAssemblyVersion}]", color);
WriteLine($"{SwanRuntime.ProductTrademark}", color);
}
/// <summary>
/// Enqueues the output to be written to the console
/// This is the only method that should enqueue to the output
/// Please note that if AvailableWriters is None, then no output will be enqueued.
/// </summary>
/// <param name="context">The context.</param>
private static void EnqueueOutput(OutputContext context)
{
lock (SyncLock)
{
var availableWriters = AvailableWriters;
if (availableWriters == TerminalWriters.None || context.OutputWriters == TerminalWriters.None)
{
OutputDone.Set();
return;
}
if ((context.OutputWriters & availableWriters) == TerminalWriters.None)
return;
OutputDone.Reset();
OutputQueue.Enqueue(context);
}
}
/// <summary>
/// Runs a Terminal I/O cycle in the <see cref="ThreadPool"/> thread.
/// </summary>
private static void DequeueOutputCycle()
{
if (AvailableWriters == TerminalWriters.None)
{
OutputDone.Set();
return;
}
InputDone.Wait();
if (OutputQueue.Count <= 0)
{
OutputDone.Set();
return;
}
OutputDone.Reset();
while (OutputQueue.Count > 0)
{
if (!OutputQueue.TryDequeue(out var context)) continue;
// Process Console output and Skip over stuff we can't display so we don't stress the output too much.
if (!IsConsolePresent) continue;
Console.ForegroundColor = context.OutputColor;
// Output to the standard output
if (context.OutputWriters.HasFlag(TerminalWriters.StandardOutput))
{
Console.Out.Write(context.OutputText);
}
// output to the standard error
if (context.OutputWriters.HasFlag(TerminalWriters.StandardError))
{
Console.Error.Write(context.OutputText);
}
Console.ResetColor();
Console.ForegroundColor = context.OriginalColor;
}
}
#endregion
#region Output Context
/// <summary>
/// Represents an asynchronous output context.
/// </summary>
private sealed class OutputContext
{
/// <summary>
/// Initializes a new instance of the <see cref="OutputContext"/> class.
/// </summary>
public OutputContext()
{
OriginalColor = Settings.DefaultColor;
OutputWriters = IsConsolePresent
? TerminalWriters.StandardOutput
: TerminalWriters.None;
}
public ConsoleColor OriginalColor { get; }
public ConsoleColor OutputColor { get; set; }
public char[] OutputText { get; set; }
public TerminalWriters OutputWriters { get; set; }
}
#endregion
}
static Terminal() {
lock(SyncLock) {
if(DequeueOutputTimer != null) {
return;
}
if(IsConsolePresent) {
Console.CursorVisible = false;
}
// Here we start the output task, fire-and-forget
DequeueOutputTimer = new ExclusiveTimer(DequeueOutputCycle);
DequeueOutputTimer.Resume(OutputFlushInterval);
}
}
#endregion
#region Synchronized Cursor Movement
/// <summary>
/// Gets or sets the cursor left position.
/// </summary>
/// <value>
/// The cursor left.
/// </value>
public static Int32 CursorLeft {
get {
if(IsConsolePresent == false) {
return -1;
}
lock(SyncLock) {
Flush();
return Console.CursorLeft;
}
}
set {
if(IsConsolePresent == false) {
return;
}
lock(SyncLock) {
Flush();
Console.CursorLeft = value;
}
}
}
/// <summary>
/// Gets or sets the cursor top position.
/// </summary>
/// <value>
/// The cursor top.
/// </value>
public static Int32 CursorTop {
get {
if(IsConsolePresent == false) {
return -1;
}
lock(SyncLock) {
Flush();
return Console.CursorTop;
}
}
set {
if(IsConsolePresent == false) {
return;
}
lock(SyncLock) {
Flush();
Console.CursorTop = value;
}
}
}
#endregion
#region Properties
/// <summary>
/// Gets a value indicating whether the Console is present.
/// </summary>
/// <value>
/// <c>true</c> if this instance is console present; otherwise, <c>false</c>.
/// </value>
public static Boolean IsConsolePresent {
get {
if(_isConsolePresent == null) {
_isConsolePresent = true;
try {
Int32 windowHeight = Console.WindowHeight;
_isConsolePresent = windowHeight >= 0;
} catch {
_isConsolePresent = false;
}
}
return _isConsolePresent.Value;
}
}
/// <summary>
/// Gets the available output writers in a bitwise mask.
/// </summary>
/// <value>
/// The available writers.
/// </value>
public static TerminalWriters AvailableWriters => IsConsolePresent ? TerminalWriters.StandardError | TerminalWriters.StandardOutput : TerminalWriters.None;
/// <summary>
/// Gets or sets the output encoding for the current console.
/// </summary>
/// <value>
/// The output encoding.
/// </value>
public static Encoding OutputEncoding {
get => Console.OutputEncoding;
set => Console.OutputEncoding = value;
}
#endregion
#region Methods
/// <summary>
/// Waits for all of the queued output messages to be written out to the console.
/// Call this method if it is important to display console text before
/// quitting the application such as showing usage or help.
/// Set the timeout to null or TimeSpan.Zero to wait indefinitely.
/// </summary>
/// <param name="timeout">The timeout. Set the amount of time to black before this method exits.</param>
public static void Flush(TimeSpan? timeout = null) {
if(timeout == null) {
timeout = TimeSpan.Zero;
}
DateTime startTime = DateTime.UtcNow;
while(OutputQueue.Count > 0) {
// Manually trigger a timer cycle to run immediately
DequeueOutputTimer.Change(0, OutputFlushInterval);
// Wait for the output to finish
if(OutputDone.Wait(OutputFlushInterval)) {
break;
}
// infinite timeout
if(timeout.Value == TimeSpan.Zero) {
continue;
}
// break if we have reached a timeout condition
if(DateTime.UtcNow.Subtract(startTime) >= timeout.Value) {
break;
}
}
}
/// <summary>
/// Sets the cursor position.
/// </summary>
/// <param name="left">The left.</param>
/// <param name="top">The top.</param>
public static void SetCursorPosition(Int32 left, Int32 top) {
if(!IsConsolePresent) {
return;
}
lock(SyncLock) {
Flush();
Console.SetCursorPosition(left.Clamp(0, left), top.Clamp(0, top));
}
}
/// <summary>
/// Moves the output cursor one line up starting at left position 0
/// Please note that backlining the cursor does not clear the contents of the
/// previous line so you might need to clear it by writing an empty string the
/// length of the console width.
/// </summary>
public static void BacklineCursor() => SetCursorPosition(0, CursorTop - 1);
/// <summary>
/// Writes a standard banner to the standard output
/// containing the company name, product name, assembly version and trademark.
/// </summary>
/// <param name="color">The color.</param>
public static void WriteWelcomeBanner(ConsoleColor color = ConsoleColor.Gray) {
WriteLine($"{SwanRuntime.CompanyName} {SwanRuntime.ProductName} [Version {SwanRuntime.EntryAssemblyVersion}]", color);
WriteLine($"{SwanRuntime.ProductTrademark}", color);
}
/// <summary>
/// Enqueues the output to be written to the console
/// This is the only method that should enqueue to the output
/// Please note that if AvailableWriters is None, then no output will be enqueued.
/// </summary>
/// <param name="context">The context.</param>
private static void EnqueueOutput(OutputContext context) {
lock(SyncLock) {
TerminalWriters availableWriters = AvailableWriters;
if(availableWriters == TerminalWriters.None || context.OutputWriters == TerminalWriters.None) {
OutputDone.Set();
return;
}
if((context.OutputWriters & availableWriters) == TerminalWriters.None) {
return;
}
OutputDone.Reset();
OutputQueue.Enqueue(context);
}
}
/// <summary>
/// Runs a Terminal I/O cycle in the <see cref="ThreadPool"/> thread.
/// </summary>
private static void DequeueOutputCycle() {
if(AvailableWriters == TerminalWriters.None) {
OutputDone.Set();
return;
}
InputDone.Wait();
if(OutputQueue.Count <= 0) {
OutputDone.Set();
return;
}
OutputDone.Reset();
while(OutputQueue.Count > 0) {
if(!OutputQueue.TryDequeue(out OutputContext context)) {
continue;
}
// Process Console output and Skip over stuff we can't display so we don't stress the output too much.
if(!IsConsolePresent) {
continue;
}
Console.ForegroundColor = context.OutputColor;
// Output to the standard output
if(context.OutputWriters.HasFlag(TerminalWriters.StandardOutput)) {
Console.Out.Write(context.OutputText);
}
// output to the standard error
if(context.OutputWriters.HasFlag(TerminalWriters.StandardError)) {
Console.Error.Write(context.OutputText);
}
Console.ResetColor();
Console.ForegroundColor = context.OriginalColor;
}
}
#endregion
#region Output Context
/// <summary>
/// Represents an asynchronous output context.
/// </summary>
private sealed class OutputContext {
/// <summary>
/// Initializes a new instance of the <see cref="OutputContext"/> class.
/// </summary>
public OutputContext() {
this.OriginalColor = Settings.DefaultColor;
this.OutputWriters = IsConsolePresent ? TerminalWriters.StandardOutput : TerminalWriters.None;
}
public ConsoleColor OriginalColor {
get;
}
public ConsoleColor OutputColor {
get; set;
}
public Char[] OutputText {
get; set;
}
public TerminalWriters OutputWriters {
get; set;
}
}
#endregion
}
}

View File

@ -1,31 +1,29 @@
using System;
namespace Swan
{
namespace Swan {
/// <summary>
/// Defines a set of bitwise standard terminal writers.
/// </summary>
[Flags]
public enum TerminalWriters {
/// <summary>
/// Defines a set of bitwise standard terminal writers.
/// Prevents output
/// </summary>
[Flags]
public enum TerminalWriters
{
/// <summary>
/// Prevents output
/// </summary>
None = 0,
/// <summary>
/// Writes to the Console.Out
/// </summary>
StandardOutput = 1,
/// <summary>
/// Writes to the Console.Error
/// </summary>
StandardError = 2,
/// <summary>
/// Writes to all possible terminal writers
/// </summary>
All = StandardOutput | StandardError,
}
None = 0,
/// <summary>
/// Writes to the Console.Out
/// </summary>
StandardOutput = 1,
/// <summary>
/// Writes to the Console.Error
/// </summary>
StandardError = 2,
/// <summary>
/// Writes to all possible terminal writers
/// </summary>
All = StandardOutput | StandardError,
}
}

View File

@ -1,24 +1,22 @@
namespace Swan.Threading
{
using System;
namespace Swan.Threading {
/// <summary>
/// Fast, atomic boolean combining interlocked to write value and volatile to read values.
/// </summary>
public sealed class AtomicBoolean : AtomicTypeBase<Boolean> {
/// <summary>
/// Fast, atomic boolean combining interlocked to write value and volatile to read values.
/// Initializes a new instance of the <see cref="AtomicBoolean"/> class.
/// </summary>
public sealed class AtomicBoolean : AtomicTypeBase<bool>
{
/// <summary>
/// Initializes a new instance of the <see cref="AtomicBoolean"/> class.
/// </summary>
/// <param name="initialValue">if set to <c>true</c> [initial value].</param>
public AtomicBoolean(bool initialValue = default)
: base(initialValue ? 1 : 0)
{
// placeholder
}
/// <inheritdoc/>
protected override bool FromLong(long backingValue) => backingValue != 0;
/// <inheritdoc/>
protected override long ToLong(bool value) => value ? 1 : 0;
}
/// <param name="initialValue">if set to <c>true</c> [initial value].</param>
public AtomicBoolean(Boolean initialValue = default) : base(initialValue ? 1 : 0) {
// placeholder
}
/// <inheritdoc/>
protected override Boolean FromLong(Int64 backingValue) => backingValue != 0;
/// <inheritdoc/>
protected override Int64 ToLong(Boolean value) => value ? 1 : 0;
}
}

View File

@ -1,26 +1,22 @@
using System;
namespace Swan.Threading
{
namespace Swan.Threading {
/// <summary>
/// Defines an atomic DateTime.
/// </summary>
public sealed class AtomicDateTime : AtomicTypeBase<DateTime> {
/// <summary>
/// Defines an atomic DateTime.
/// Initializes a new instance of the <see cref="AtomicDateTime"/> class.
/// </summary>
public sealed class AtomicDateTime : AtomicTypeBase<DateTime>
{
/// <summary>
/// Initializes a new instance of the <see cref="AtomicDateTime"/> class.
/// </summary>
/// <param name="initialValue">The initial value.</param>
public AtomicDateTime(DateTime initialValue)
: base(initialValue.Ticks)
{
// placeholder
}
/// <inheritdoc />
protected override DateTime FromLong(long backingValue) => new DateTime(backingValue);
/// <inheritdoc />
protected override long ToLong(DateTime value) => value.Ticks;
}
/// <param name="initialValue">The initial value.</param>
public AtomicDateTime(DateTime initialValue) : base(initialValue.Ticks) {
// placeholder
}
/// <inheritdoc />
protected override DateTime FromLong(Int64 backingValue) => new DateTime(backingValue);
/// <inheritdoc />
protected override Int64 ToLong(DateTime value) => value.Ticks;
}
}

View File

@ -1,28 +1,22 @@
using System;
namespace Swan.Threading
{
namespace Swan.Threading {
/// <summary>
/// Fast, atomic double combining interlocked to write value and volatile to read values.
/// </summary>
public sealed class AtomicDouble : AtomicTypeBase<Double> {
/// <summary>
/// Fast, atomic double combining interlocked to write value and volatile to read values.
/// Initializes a new instance of the <see cref="AtomicDouble"/> class.
/// </summary>
public sealed class AtomicDouble : AtomicTypeBase<double>
{
/// <summary>
/// Initializes a new instance of the <see cref="AtomicDouble"/> class.
/// </summary>
/// <param name="initialValue">if set to <c>true</c> [initial value].</param>
public AtomicDouble(double initialValue = default)
: base(BitConverter.DoubleToInt64Bits(initialValue))
{
// placeholder
}
/// <inheritdoc/>
protected override double FromLong(long backingValue) =>
BitConverter.Int64BitsToDouble(backingValue);
/// <inheritdoc/>
protected override long ToLong(double value) =>
BitConverter.DoubleToInt64Bits(value);
}
/// <param name="initialValue">if set to <c>true</c> [initial value].</param>
public AtomicDouble(Double initialValue = default) : base(BitConverter.DoubleToInt64Bits(initialValue)) {
// placeholder
}
/// <inheritdoc/>
protected override Double FromLong(Int64 backingValue) => BitConverter.Int64BitsToDouble(backingValue);
/// <inheritdoc/>
protected override Int64 ToLong(Double value) => BitConverter.DoubleToInt64Bits(value);
}
}

View File

@ -1,43 +1,38 @@
using System;
using System.Threading;
namespace Swan.Threading
{
namespace Swan.Threading {
/// <summary>
/// Defines an atomic generic Enum.
/// </summary>
/// <typeparam name="T">The type of enum.</typeparam>
public sealed class AtomicEnum<T> where T : struct, IConvertible {
private Int64 _backingValue;
/// <summary>
/// Defines an atomic generic Enum.
/// Initializes a new instance of the <see cref="AtomicEnum{T}"/> class.
/// </summary>
/// <typeparam name="T">The type of enum.</typeparam>
public sealed class AtomicEnum<T>
where T : struct, IConvertible
{
private long _backingValue;
/// <summary>
/// Initializes a new instance of the <see cref="AtomicEnum{T}"/> class.
/// </summary>
/// <param name="initialValue">The initial value.</param>
/// <exception cref="ArgumentException">T must be an enumerated type.</exception>
public AtomicEnum(T initialValue)
{
if (!Enum.IsDefined(typeof(T), initialValue))
throw new ArgumentException("T must be an enumerated type");
Value = initialValue;
}
/// <summary>
/// Gets or sets the value.
/// </summary>
public T Value
{
get => (T)Enum.ToObject(typeof(T), BackingValue);
set => BackingValue = Convert.ToInt64(value);
}
private long BackingValue
{
get => Interlocked.Read(ref _backingValue);
set => Interlocked.Exchange(ref _backingValue, value);
}
}
/// <param name="initialValue">The initial value.</param>
/// <exception cref="ArgumentException">T must be an enumerated type.</exception>
public AtomicEnum(T initialValue) {
if(!Enum.IsDefined(typeof(T), initialValue)) {
throw new ArgumentException("T must be an enumerated type");
}
this.Value = initialValue;
}
/// <summary>
/// Gets or sets the value.
/// </summary>
public T Value {
get => (T)Enum.ToObject(typeof(T), this.BackingValue);
set => this.BackingValue = Convert.ToInt64(value);
}
private Int64 BackingValue {
get => Interlocked.Read(ref this._backingValue);
set => Interlocked.Exchange(ref this._backingValue, value);
}
}
}

View File

@ -1,28 +1,22 @@
using System;
namespace Swan.Threading
{
namespace Swan.Threading {
/// <summary>
/// Represents an atomically readable or writable integer.
/// </summary>
public class AtomicInteger : AtomicTypeBase<Int32> {
/// <summary>
/// Represents an atomically readable or writable integer.
/// Initializes a new instance of the <see cref="AtomicInteger"/> class.
/// </summary>
public class AtomicInteger : AtomicTypeBase<int>
{
/// <summary>
/// Initializes a new instance of the <see cref="AtomicInteger"/> class.
/// </summary>
/// <param name="initialValue">if set to <c>true</c> [initial value].</param>
public AtomicInteger(int initialValue = default)
: base(Convert.ToInt64(initialValue))
{
// placeholder
}
/// <inheritdoc/>
protected override int FromLong(long backingValue) =>
Convert.ToInt32(backingValue);
/// <inheritdoc/>
protected override long ToLong(int value) =>
Convert.ToInt64(value);
}
/// <param name="initialValue">if set to <c>true</c> [initial value].</param>
public AtomicInteger(Int32 initialValue = default) : base(Convert.ToInt64(initialValue)) {
// placeholder
}
/// <inheritdoc/>
protected override Int32 FromLong(Int64 backingValue) => Convert.ToInt32(backingValue);
/// <inheritdoc/>
protected override Int64 ToLong(Int32 value) => Convert.ToInt64(value);
}
}

View File

@ -1,24 +1,22 @@
namespace Swan.Threading
{
using System;
namespace Swan.Threading {
/// <summary>
/// Fast, atomic long combining interlocked to write value and volatile to read values.
/// </summary>
public sealed class AtomicLong : AtomicTypeBase<Int64> {
/// <summary>
/// Fast, atomic long combining interlocked to write value and volatile to read values.
/// Initializes a new instance of the <see cref="AtomicLong"/> class.
/// </summary>
public sealed class AtomicLong : AtomicTypeBase<long>
{
/// <summary>
/// Initializes a new instance of the <see cref="AtomicLong"/> class.
/// </summary>
/// <param name="initialValue">if set to <c>true</c> [initial value].</param>
public AtomicLong(long initialValue = default)
: base(initialValue)
{
// placeholder
}
/// <inheritdoc />
protected override long FromLong(long backingValue) => backingValue;
/// <inheritdoc />
protected override long ToLong(long value) => value;
}
/// <param name="initialValue">if set to <c>true</c> [initial value].</param>
public AtomicLong(Int64 initialValue = default) : base(initialValue) {
// placeholder
}
/// <inheritdoc />
protected override Int64 FromLong(Int64 backingValue) => backingValue;
/// <inheritdoc />
protected override Int64 ToLong(Int64 value) => value;
}
}

View File

@ -1,26 +1,22 @@
using System;
namespace Swan.Threading
{
namespace Swan.Threading {
/// <summary>
/// Represents an atomic TimeSpan type.
/// </summary>
public sealed class AtomicTimeSpan : AtomicTypeBase<TimeSpan> {
/// <summary>
/// Represents an atomic TimeSpan type.
/// Initializes a new instance of the <see cref="AtomicTimeSpan" /> class.
/// </summary>
public sealed class AtomicTimeSpan : AtomicTypeBase<TimeSpan>
{
/// <summary>
/// Initializes a new instance of the <see cref="AtomicTimeSpan" /> class.
/// </summary>
/// <param name="initialValue">The initial value.</param>
public AtomicTimeSpan(TimeSpan initialValue)
: base(initialValue.Ticks)
{
// placeholder
}
/// <inheritdoc />
protected override TimeSpan FromLong(long backingValue) => TimeSpan.FromTicks(backingValue);
/// <inheritdoc />
protected override long ToLong(TimeSpan value) => value.Ticks < 0 ? 0 : value.Ticks;
}
/// <param name="initialValue">The initial value.</param>
public AtomicTimeSpan(TimeSpan initialValue) : base(initialValue.Ticks) {
// placeholder
}
/// <inheritdoc />
protected override TimeSpan FromLong(Int64 backingValue) => TimeSpan.FromTicks(backingValue);
/// <inheritdoc />
protected override Int64 ToLong(TimeSpan value) => value.Ticks < 0 ? 0 : value.Ticks;
}
}

View File

@ -1,243 +1,219 @@
using System;
using System.Threading;
namespace Swan.Threading
{
namespace Swan.Threading {
/// <summary>
/// Provides a generic implementation of an Atomic (interlocked) type
///
/// Idea taken from Memory model and .NET operations in article:
/// http://igoro.com/archive/volatile-keyword-in-c-memory-model-explained/.
/// </summary>
/// <typeparam name="T">The structure type backed by a 64-bit value.</typeparam>
public abstract class AtomicTypeBase<T> : IComparable, IComparable<T>, IComparable<AtomicTypeBase<T>>, IEquatable<T>, IEquatable<AtomicTypeBase<T>> where T : struct, IComparable, IComparable<T>, IEquatable<T> {
private Int64 _backingValue;
/// <summary>
/// Provides a generic implementation of an Atomic (interlocked) type
///
/// Idea taken from Memory model and .NET operations in article:
/// http://igoro.com/archive/volatile-keyword-in-c-memory-model-explained/.
/// Initializes a new instance of the <see cref="AtomicTypeBase{T}"/> class.
/// </summary>
/// <typeparam name="T">The structure type backed by a 64-bit value.</typeparam>
public abstract class AtomicTypeBase<T> : IComparable, IComparable<T>, IComparable<AtomicTypeBase<T>>, IEquatable<T>, IEquatable<AtomicTypeBase<T>>
where T : struct, IComparable, IComparable<T>, IEquatable<T>
{
private long _backingValue;
/// <summary>
/// Initializes a new instance of the <see cref="AtomicTypeBase{T}"/> class.
/// </summary>
/// <param name="initialValue">The initial value.</param>
protected AtomicTypeBase(long initialValue)
{
BackingValue = initialValue;
}
/// <summary>
/// Gets or sets the value.
/// </summary>
public T Value
{
get => FromLong(BackingValue);
set => BackingValue = ToLong(value);
}
/// <summary>
/// Gets or sets the backing value.
/// </summary>
protected long BackingValue
{
get => Interlocked.Read(ref _backingValue);
set => Interlocked.Exchange(ref _backingValue, value);
}
/// <summary>
/// Implements the operator ==.
/// </summary>
/// <param name="a">a.</param>
/// <param name="b">The b.</param>
/// <returns>
/// The result of the operator.
/// </returns>
public static bool operator ==(AtomicTypeBase<T> a, T b) => a?.Equals(b) == true;
/// <summary>
/// Implements the operator !=.
/// </summary>
/// <param name="a">a.</param>
/// <param name="b">The b.</param>
/// <returns>
/// The result of the operator.
/// </returns>
public static bool operator !=(AtomicTypeBase<T> a, T b) => a?.Equals(b) == false;
/// <summary>
/// Implements the operator &gt;.
/// </summary>
/// <param name="a">a.</param>
/// <param name="b">The b.</param>
/// <returns>
/// The result of the operator.
/// </returns>
public static bool operator >(AtomicTypeBase<T> a, T b) => a.CompareTo(b) > 0;
/// <summary>
/// Implements the operator &lt;.
/// </summary>
/// <param name="a">a.</param>
/// <param name="b">The b.</param>
/// <returns>
/// The result of the operator.
/// </returns>
public static bool operator <(AtomicTypeBase<T> a, T b) => a.CompareTo(b) < 0;
/// <summary>
/// Implements the operator &gt;=.
/// </summary>
/// <param name="a">a.</param>
/// <param name="b">The b.</param>
/// <returns>
/// The result of the operator.
/// </returns>
public static bool operator >=(AtomicTypeBase<T> a, T b) => a.CompareTo(b) >= 0;
/// <summary>
/// Implements the operator &lt;=.
/// </summary>
/// <param name="a">a.</param>
/// <param name="b">The b.</param>
/// <returns>
/// The result of the operator.
/// </returns>
public static bool operator <=(AtomicTypeBase<T> a, T b) => a.CompareTo(b) <= 0;
/// <summary>
/// Implements the operator ++.
/// </summary>
/// <param name="instance">The instance.</param>
/// <returns>
/// The result of the operator.
/// </returns>
public static AtomicTypeBase<T> operator ++(AtomicTypeBase<T> instance)
{
Interlocked.Increment(ref instance._backingValue);
return instance;
}
/// <summary>
/// Implements the operator --.
/// </summary>
/// <param name="instance">The instance.</param>
/// <returns>
/// The result of the operator.
/// </returns>
public static AtomicTypeBase<T> operator --(AtomicTypeBase<T> instance)
{
Interlocked.Decrement(ref instance._backingValue);
return instance;
}
/// <summary>
/// Implements the operator -&lt;.
/// </summary>
/// <param name="instance">The instance.</param>
/// <param name="operand">The operand.</param>
/// <returns>
/// The result of the operator.
/// </returns>
public static AtomicTypeBase<T> operator +(AtomicTypeBase<T> instance, long operand)
{
instance.BackingValue = instance.BackingValue + operand;
return instance;
}
/// <summary>
/// Implements the operator -.
/// </summary>
/// <param name="instance">The instance.</param>
/// <param name="operand">The operand.</param>
/// <returns>
/// The result of the operator.
/// </returns>
public static AtomicTypeBase<T> operator -(AtomicTypeBase<T> instance, long operand)
{
instance.BackingValue = instance.BackingValue - operand;
return instance;
}
/// <summary>
/// Compares the value to the other instance.
/// </summary>
/// <param name="other">The other instance.</param>
/// <returns>0 if equal, 1 if this instance is greater, -1 if this instance is less than.</returns>
/// <exception cref="ArgumentException">When types are incompatible.</exception>
public int CompareTo(object other)
{
switch (other)
{
case null:
return 1;
case AtomicTypeBase<T> atomic:
return BackingValue.CompareTo(atomic.BackingValue);
case T variable:
return Value.CompareTo(variable);
}
throw new ArgumentException("Incompatible comparison types");
}
/// <summary>
/// Compares the value to the other instance.
/// </summary>
/// <param name="other">The other instance.</param>
/// <returns>0 if equal, 1 if this instance is greater, -1 if this instance is less than.</returns>
public int CompareTo(T other) => Value.CompareTo(other);
/// <summary>
/// Compares the value to the other instance.
/// </summary>
/// <param name="other">The other instance.</param>
/// <returns>0 if equal, 1 if this instance is greater, -1 if this instance is less than.</returns>
public int CompareTo(AtomicTypeBase<T> other) => BackingValue.CompareTo(other?.BackingValue ?? default);
/// <summary>
/// Determines whether the specified <see cref="object" />, is equal to this instance.
/// </summary>
/// <param name="other">The <see cref="object" /> to compare with this instance.</param>
/// <returns>
/// <c>true</c> if the specified <see cref="object" /> is equal to this instance; otherwise, <c>false</c>.
/// </returns>
public override bool Equals(object other)
{
switch (other)
{
case AtomicTypeBase<T> atomic:
return Equals(atomic);
case T variable:
return Equals(variable);
}
return false;
}
/// <summary>
/// Returns a hash code for this instance.
/// </summary>
/// <returns>
/// A hash code for this instance, suitable for use in hashing algorithms and data structures like a hash table.
/// </returns>
public override int GetHashCode() => BackingValue.GetHashCode();
/// <inheritdoc />
public bool Equals(AtomicTypeBase<T> other) =>
BackingValue == (other?.BackingValue ?? default);
/// <inheritdoc />
public bool Equals(T other) => Equals(Value, other);
/// <summary>
/// Converts from a long value to the target type.
/// </summary>
/// <param name="backingValue">The backing value.</param>
/// <returns>The value converted form a long value.</returns>
protected abstract T FromLong(long backingValue);
/// <summary>
/// Converts from the target type to a long value.
/// </summary>
/// <param name="value">The value.</param>
/// <returns>The value converted to a long value.</returns>
protected abstract long ToLong(T value);
}
/// <param name="initialValue">The initial value.</param>
protected AtomicTypeBase(Int64 initialValue) => this.BackingValue = initialValue;
/// <summary>
/// Gets or sets the value.
/// </summary>
public T Value {
get => this.FromLong(this.BackingValue);
set => this.BackingValue = this.ToLong(value);
}
/// <summary>
/// Gets or sets the backing value.
/// </summary>
protected Int64 BackingValue {
get => Interlocked.Read(ref this._backingValue);
set => Interlocked.Exchange(ref this._backingValue, value);
}
/// <summary>
/// Implements the operator ==.
/// </summary>
/// <param name="a">a.</param>
/// <param name="b">The b.</param>
/// <returns>
/// The result of the operator.
/// </returns>
public static Boolean operator ==(AtomicTypeBase<T> a, T b) => a?.Equals(b) == true;
/// <summary>
/// Implements the operator !=.
/// </summary>
/// <param name="a">a.</param>
/// <param name="b">The b.</param>
/// <returns>
/// The result of the operator.
/// </returns>
public static Boolean operator !=(AtomicTypeBase<T> a, T b) => a?.Equals(b) == false;
/// <summary>
/// Implements the operator &gt;.
/// </summary>
/// <param name="a">a.</param>
/// <param name="b">The b.</param>
/// <returns>
/// The result of the operator.
/// </returns>
public static Boolean operator >(AtomicTypeBase<T> a, T b) => a.CompareTo(b) > 0;
/// <summary>
/// Implements the operator &lt;.
/// </summary>
/// <param name="a">a.</param>
/// <param name="b">The b.</param>
/// <returns>
/// The result of the operator.
/// </returns>
public static Boolean operator <(AtomicTypeBase<T> a, T b) => a.CompareTo(b) < 0;
/// <summary>
/// Implements the operator &gt;=.
/// </summary>
/// <param name="a">a.</param>
/// <param name="b">The b.</param>
/// <returns>
/// The result of the operator.
/// </returns>
public static Boolean operator >=(AtomicTypeBase<T> a, T b) => a.CompareTo(b) >= 0;
/// <summary>
/// Implements the operator &lt;=.
/// </summary>
/// <param name="a">a.</param>
/// <param name="b">The b.</param>
/// <returns>
/// The result of the operator.
/// </returns>
public static Boolean operator <=(AtomicTypeBase<T> a, T b) => a.CompareTo(b) <= 0;
/// <summary>
/// Implements the operator ++.
/// </summary>
/// <param name="instance">The instance.</param>
/// <returns>
/// The result of the operator.
/// </returns>
public static AtomicTypeBase<T> operator ++(AtomicTypeBase<T> instance) {
_ = Interlocked.Increment(ref instance._backingValue);
return instance;
}
/// <summary>
/// Implements the operator --.
/// </summary>
/// <param name="instance">The instance.</param>
/// <returns>
/// The result of the operator.
/// </returns>
public static AtomicTypeBase<T> operator --(AtomicTypeBase<T> instance) {
_ = Interlocked.Decrement(ref instance._backingValue);
return instance;
}
/// <summary>
/// Implements the operator -&lt;.
/// </summary>
/// <param name="instance">The instance.</param>
/// <param name="operand">The operand.</param>
/// <returns>
/// The result of the operator.
/// </returns>
public static AtomicTypeBase<T> operator +(AtomicTypeBase<T> instance, Int64 operand) {
instance.BackingValue += operand;
return instance;
}
/// <summary>
/// Implements the operator -.
/// </summary>
/// <param name="instance">The instance.</param>
/// <param name="operand">The operand.</param>
/// <returns>
/// The result of the operator.
/// </returns>
public static AtomicTypeBase<T> operator -(AtomicTypeBase<T> instance, Int64 operand) {
instance.BackingValue -= operand;
return instance;
}
/// <summary>
/// Compares the value to the other instance.
/// </summary>
/// <param name="other">The other instance.</param>
/// <returns>0 if equal, 1 if this instance is greater, -1 if this instance is less than.</returns>
/// <exception cref="ArgumentException">When types are incompatible.</exception>
public Int32 CompareTo(Object other) => other switch
{
null => 1,
AtomicTypeBase<T> atomic => this.BackingValue.CompareTo(atomic.BackingValue),
T variable => this.Value.CompareTo(variable),
_ => throw new ArgumentException("Incompatible comparison types"),
};
/// <summary>
/// Compares the value to the other instance.
/// </summary>
/// <param name="other">The other instance.</param>
/// <returns>0 if equal, 1 if this instance is greater, -1 if this instance is less than.</returns>
public Int32 CompareTo(T other) => this.Value.CompareTo(other);
/// <summary>
/// Compares the value to the other instance.
/// </summary>
/// <param name="other">The other instance.</param>
/// <returns>0 if equal, 1 if this instance is greater, -1 if this instance is less than.</returns>
public Int32 CompareTo(AtomicTypeBase<T> other) => this.BackingValue.CompareTo(other?.BackingValue ?? default);
/// <summary>
/// Determines whether the specified <see cref="Object" />, is equal to this instance.
/// </summary>
/// <param name="other">The <see cref="Object" /> to compare with this instance.</param>
/// <returns>
/// <c>true</c> if the specified <see cref="Object" /> is equal to this instance; otherwise, <c>false</c>.
/// </returns>
public override Boolean Equals(Object other) => other switch
{
AtomicTypeBase<T> atomic => this.Equals(atomic),
T variable => this.Equals(variable),
_ => false,
};
/// <summary>
/// Returns a hash code for this instance.
/// </summary>
/// <returns>
/// A hash code for this instance, suitable for use in hashing algorithms and data structures like a hash table.
/// </returns>
public override Int32 GetHashCode() => this.BackingValue.GetHashCode();
/// <inheritdoc />
public Boolean Equals(AtomicTypeBase<T> other) => this.BackingValue == (other?.BackingValue ?? default);
/// <inheritdoc />
public Boolean Equals(T other) => Equals(this.Value, other);
/// <summary>
/// Converts from a long value to the target type.
/// </summary>
/// <param name="backingValue">The backing value.</param>
/// <returns>The value converted form a long value.</returns>
protected abstract T FromLong(Int64 backingValue);
/// <summary>
/// Converts from the target type to a long value.
/// </summary>
/// <param name="value">The value.</param>
/// <returns>The value converted to a long value.</returns>
protected abstract Int64 ToLong(T value);
}
}

View File

@ -1,68 +1,61 @@
using System;
using System.Threading;
namespace Swan.Threading
{
namespace Swan.Threading {
/// <summary>
/// Acts as a <see cref="CancellationTokenSource"/> but with reusable tokens.
/// </summary>
public sealed class CancellationTokenOwner : IDisposable {
private readonly Object _syncLock = new Object();
private Boolean _isDisposed;
private CancellationTokenSource _tokenSource = new CancellationTokenSource();
/// <summary>
/// Acts as a <see cref="CancellationTokenSource"/> but with reusable tokens.
/// Gets the token of the current.
/// </summary>
public sealed class CancellationTokenOwner : IDisposable
{
private readonly object _syncLock = new object();
private bool _isDisposed;
private CancellationTokenSource _tokenSource = new CancellationTokenSource();
/// <summary>
/// Gets the token of the current.
/// </summary>
public CancellationToken Token
{
get
{
lock (_syncLock)
{
return _isDisposed
? CancellationToken.None
: _tokenSource.Token;
}
}
}
/// <summary>
/// Cancels the last referenced token and creates a new token source.
/// </summary>
public void Cancel()
{
lock (_syncLock)
{
if (_isDisposed) return;
_tokenSource.Cancel();
_tokenSource.Dispose();
_tokenSource = new CancellationTokenSource();
}
}
/// <inheritdoc />
public void Dispose() => Dispose(true);
/// <summary>
/// Releases unmanaged and - optionally - managed resources.
/// </summary>
/// <param name="disposing"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.</param>
private void Dispose(bool disposing)
{
lock (_syncLock)
{
if (_isDisposed) return;
if (disposing)
{
_tokenSource.Cancel();
_tokenSource.Dispose();
}
_isDisposed = true;
}
}
}
public CancellationToken Token {
get {
lock(this._syncLock) {
return this._isDisposed ? CancellationToken.None : this._tokenSource.Token;
}
}
}
/// <summary>
/// Cancels the last referenced token and creates a new token source.
/// </summary>
public void Cancel() {
lock(this._syncLock) {
if(this._isDisposed) {
return;
}
this._tokenSource.Cancel();
this._tokenSource.Dispose();
this._tokenSource = new CancellationTokenSource();
}
}
/// <inheritdoc />
public void Dispose() => this.Dispose(true);
/// <summary>
/// Releases unmanaged and - optionally - managed resources.
/// </summary>
/// <param name="disposing"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.</param>
private void Dispose(Boolean disposing) {
lock(this._syncLock) {
if(this._isDisposed) {
return;
}
if(disposing) {
this._tokenSource.Cancel();
this._tokenSource.Dispose();
}
this._isDisposed = true;
}
}
}
}

View File

@ -1,236 +1,206 @@
using System;
using System.Threading;
namespace Swan.Threading
{
namespace Swan.Threading {
/// <summary>
/// A threading <see cref="_backingTimer"/> implementation that executes at most one cycle at a time
/// in a <see cref="ThreadPool"/> thread. Callback execution is NOT guaranteed to be carried out
/// on the same <see cref="ThreadPool"/> thread every time the timer fires.
/// </summary>
public sealed class ExclusiveTimer : IDisposable {
private readonly Object _syncLock = new Object();
private readonly ManualResetEventSlim _cycleDoneEvent = new ManualResetEventSlim(true);
private readonly Timer _backingTimer;
private readonly TimerCallback _userCallback;
private readonly AtomicBoolean _isDisposing = new AtomicBoolean();
private readonly AtomicBoolean _isDisposed = new AtomicBoolean();
private Int32 _period;
/// <summary>
/// A threading <see cref="_backingTimer"/> implementation that executes at most one cycle at a time
/// in a <see cref="ThreadPool"/> thread. Callback execution is NOT guaranteed to be carried out
/// on the same <see cref="ThreadPool"/> thread every time the timer fires.
/// Initializes a new instance of the <see cref="ExclusiveTimer"/> class.
/// </summary>
public sealed class ExclusiveTimer : IDisposable
{
private readonly object _syncLock = new object();
private readonly ManualResetEventSlim _cycleDoneEvent = new ManualResetEventSlim(true);
private readonly Timer _backingTimer;
private readonly TimerCallback _userCallback;
private readonly AtomicBoolean _isDisposing = new AtomicBoolean();
private readonly AtomicBoolean _isDisposed = new AtomicBoolean();
private int _period;
/// <summary>
/// Initializes a new instance of the <see cref="ExclusiveTimer"/> class.
/// </summary>
/// <param name="timerCallback">The timer callback.</param>
/// <param name="state">The state.</param>
/// <param name="dueTime">The due time.</param>
/// <param name="period">The period.</param>
public ExclusiveTimer(TimerCallback timerCallback, object state, int dueTime, int period)
{
_period = period;
_userCallback = timerCallback;
_backingTimer = new Timer(InternalCallback, state ?? this, dueTime, Timeout.Infinite);
}
/// <summary>
/// Initializes a new instance of the <see cref="ExclusiveTimer"/> class.
/// </summary>
/// <param name="timerCallback">The timer callback.</param>
/// <param name="state">The state.</param>
/// <param name="dueTime">The due time.</param>
/// <param name="period">The period.</param>
public ExclusiveTimer(TimerCallback timerCallback, object state, TimeSpan dueTime, TimeSpan period)
: this(timerCallback, state, Convert.ToInt32(dueTime.TotalMilliseconds), Convert.ToInt32(period.TotalMilliseconds))
{
// placeholder
}
/// <summary>
/// Initializes a new instance of the <see cref="ExclusiveTimer"/> class.
/// </summary>
/// <param name="timerCallback">The timer callback.</param>
public ExclusiveTimer(TimerCallback timerCallback)
: this(timerCallback, null, Timeout.Infinite, Timeout.Infinite)
{
// placeholder
}
/// <summary>
/// Initializes a new instance of the <see cref="ExclusiveTimer"/> class.
/// </summary>
/// <param name="timerCallback">The timer callback.</param>
/// <param name="dueTime">The due time.</param>
/// <param name="period">The period.</param>
public ExclusiveTimer(Action timerCallback, int dueTime, int period)
: this(s => { timerCallback?.Invoke(); }, null, dueTime, period)
{
// placeholder
}
/// <summary>
/// Initializes a new instance of the <see cref="ExclusiveTimer"/> class.
/// </summary>
/// <param name="timerCallback">The timer callback.</param>
/// <param name="dueTime">The due time.</param>
/// <param name="period">The period.</param>
public ExclusiveTimer(Action timerCallback, TimeSpan dueTime, TimeSpan period)
: this(s => { timerCallback?.Invoke(); }, null, dueTime, period)
{
// placeholder
}
/// <summary>
/// Initializes a new instance of the <see cref="ExclusiveTimer"/> class.
/// </summary>
/// <param name="timerCallback">The timer callback.</param>
public ExclusiveTimer(Action timerCallback)
: this(timerCallback, Timeout.Infinite, Timeout.Infinite)
{
// placeholder
}
/// <summary>
/// Gets a value indicating whether this instance is disposing.
/// </summary>
/// <value>
/// <c>true</c> if this instance is disposing; otherwise, <c>false</c>.
/// </value>
public bool IsDisposing => _isDisposing.Value;
/// <summary>
/// Gets a value indicating whether this instance is disposed.
/// </summary>
/// <value>
/// <c>true</c> if this instance is disposed; otherwise, <c>false</c>.
/// </value>
public bool IsDisposed => _isDisposed.Value;
/// <summary>
/// Waits until the time is elapsed.
/// </summary>
/// <param name="untilDate">The until date.</param>
/// <param name="cancellationToken">The cancellation token.</param>
public static void WaitUntil(DateTime untilDate, CancellationToken cancellationToken = default)
{
void Callback(IWaitEvent waitEvent)
{
try
{
waitEvent.Complete();
waitEvent.Begin();
}
catch
{
// ignore
}
}
using (var delayLock = WaitEventFactory.Create(true))
{
using (var _ = new ExclusiveTimer(() => Callback(delayLock), 0, 15))
{
while (!cancellationToken.IsCancellationRequested && DateTime.UtcNow < untilDate)
delayLock.Wait();
}
}
}
/// <summary>
/// Waits the specified wait time.
/// </summary>
/// <param name="waitTime">The wait time.</param>
/// <param name="cancellationToken">The cancellation token.</param>
public static void Wait(TimeSpan waitTime, CancellationToken cancellationToken = default) =>
WaitUntil(DateTime.UtcNow.Add(waitTime), cancellationToken);
/// <summary>
/// Changes the start time and the interval between method invocations for the internal timer.
/// </summary>
/// <param name="dueTime">The due time.</param>
/// <param name="period">The period.</param>
public void Change(int dueTime, int period)
{
_period = period;
_backingTimer.Change(dueTime, Timeout.Infinite);
}
/// <summary>
/// Changes the start time and the interval between method invocations for the internal timer.
/// </summary>
/// <param name="dueTime">The due time.</param>
/// <param name="period">The period.</param>
public void Change(TimeSpan dueTime, TimeSpan period)
=> Change(Convert.ToInt32(dueTime.TotalMilliseconds), Convert.ToInt32(period.TotalMilliseconds));
/// <summary>
/// Changes the interval between method invocations for the internal timer.
/// </summary>
/// <param name="period">The period.</param>
public void Resume(int period) => Change(0, period);
/// <summary>
/// Changes the interval between method invocations for the internal timer.
/// </summary>
/// <param name="period">The period.</param>
public void Resume(TimeSpan period) => Change(TimeSpan.Zero, period);
/// <summary>
/// Pauses this instance.
/// </summary>
public void Pause() => Change(Timeout.Infinite, Timeout.Infinite);
/// <inheritdoc />
public void Dispose()
{
lock (_syncLock)
{
if (_isDisposed == true || _isDisposing == true)
return;
_isDisposing.Value = true;
}
try
{
_cycleDoneEvent.Wait();
_cycleDoneEvent.Dispose();
Pause();
_backingTimer.Dispose();
}
finally
{
_isDisposed.Value = true;
_isDisposing.Value = false;
}
}
/// <summary>
/// Logic that runs every time the timer hits the due time.
/// </summary>
/// <param name="state">The state.</param>
private void InternalCallback(object state)
{
lock (_syncLock)
{
if (IsDisposed || IsDisposing)
return;
}
if (_cycleDoneEvent.IsSet == false)
return;
_cycleDoneEvent.Reset();
try
{
_userCallback(state);
}
finally
{
_cycleDoneEvent?.Set();
_backingTimer?.Change(_period, Timeout.Infinite);
}
}
}
/// <param name="timerCallback">The timer callback.</param>
/// <param name="state">The state.</param>
/// <param name="dueTime">The due time.</param>
/// <param name="period">The period.</param>
public ExclusiveTimer(TimerCallback timerCallback, Object state, Int32 dueTime, Int32 period) {
this._period = period;
this._userCallback = timerCallback;
this._backingTimer = new Timer(this.InternalCallback, state ?? this, dueTime, Timeout.Infinite);
}
/// <summary>
/// Initializes a new instance of the <see cref="ExclusiveTimer"/> class.
/// </summary>
/// <param name="timerCallback">The timer callback.</param>
/// <param name="state">The state.</param>
/// <param name="dueTime">The due time.</param>
/// <param name="period">The period.</param>
public ExclusiveTimer(TimerCallback timerCallback, Object state, TimeSpan dueTime, TimeSpan period) : this(timerCallback, state, Convert.ToInt32(dueTime.TotalMilliseconds), Convert.ToInt32(period.TotalMilliseconds)) {
// placeholder
}
/// <summary>
/// Initializes a new instance of the <see cref="ExclusiveTimer"/> class.
/// </summary>
/// <param name="timerCallback">The timer callback.</param>
public ExclusiveTimer(TimerCallback timerCallback) : this(timerCallback, null, Timeout.Infinite, Timeout.Infinite) {
// placeholder
}
/// <summary>
/// Initializes a new instance of the <see cref="ExclusiveTimer"/> class.
/// </summary>
/// <param name="timerCallback">The timer callback.</param>
/// <param name="dueTime">The due time.</param>
/// <param name="period">The period.</param>
public ExclusiveTimer(Action timerCallback, Int32 dueTime, Int32 period) : this(s => timerCallback?.Invoke(), null, dueTime, period) {
// placeholder
}
/// <summary>
/// Initializes a new instance of the <see cref="ExclusiveTimer"/> class.
/// </summary>
/// <param name="timerCallback">The timer callback.</param>
/// <param name="dueTime">The due time.</param>
/// <param name="period">The period.</param>
public ExclusiveTimer(Action timerCallback, TimeSpan dueTime, TimeSpan period) : this(s => timerCallback?.Invoke(), null, dueTime, period) {
// placeholder
}
/// <summary>
/// Initializes a new instance of the <see cref="ExclusiveTimer"/> class.
/// </summary>
/// <param name="timerCallback">The timer callback.</param>
public ExclusiveTimer(Action timerCallback) : this(timerCallback, Timeout.Infinite, Timeout.Infinite) {
// placeholder
}
/// <summary>
/// Gets a value indicating whether this instance is disposing.
/// </summary>
/// <value>
/// <c>true</c> if this instance is disposing; otherwise, <c>false</c>.
/// </value>
public Boolean IsDisposing => this._isDisposing.Value;
/// <summary>
/// Gets a value indicating whether this instance is disposed.
/// </summary>
/// <value>
/// <c>true</c> if this instance is disposed; otherwise, <c>false</c>.
/// </value>
public Boolean IsDisposed => this._isDisposed.Value;
/// <summary>
/// Waits until the time is elapsed.
/// </summary>
/// <param name="untilDate">The until date.</param>
/// <param name="cancellationToken">The cancellation token.</param>
public static void WaitUntil(DateTime untilDate, CancellationToken cancellationToken = default) {
static void Callback(IWaitEvent waitEvent) {
try {
waitEvent.Complete();
waitEvent.Begin();
} catch {
// ignore
}
}
using IWaitEvent delayLock = WaitEventFactory.Create(true);
using ExclusiveTimer _ = new ExclusiveTimer(() => Callback(delayLock), 0, 15);
while(!cancellationToken.IsCancellationRequested && DateTime.UtcNow < untilDate) {
delayLock.Wait();
}
}
/// <summary>
/// Waits the specified wait time.
/// </summary>
/// <param name="waitTime">The wait time.</param>
/// <param name="cancellationToken">The cancellation token.</param>
public static void Wait(TimeSpan waitTime, CancellationToken cancellationToken = default) =>
WaitUntil(DateTime.UtcNow.Add(waitTime), cancellationToken);
/// <summary>
/// Changes the start time and the interval between method invocations for the internal timer.
/// </summary>
/// <param name="dueTime">The due time.</param>
/// <param name="period">The period.</param>
public void Change(Int32 dueTime, Int32 period) {
this._period = period;
_ = this._backingTimer.Change(dueTime, Timeout.Infinite);
}
/// <summary>
/// Changes the start time and the interval between method invocations for the internal timer.
/// </summary>
/// <param name="dueTime">The due time.</param>
/// <param name="period">The period.</param>
public void Change(TimeSpan dueTime, TimeSpan period) => this.Change(Convert.ToInt32(dueTime.TotalMilliseconds), Convert.ToInt32(period.TotalMilliseconds));
/// <summary>
/// Changes the interval between method invocations for the internal timer.
/// </summary>
/// <param name="period">The period.</param>
public void Resume(Int32 period) => this.Change(0, period);
/// <summary>
/// Changes the interval between method invocations for the internal timer.
/// </summary>
/// <param name="period">The period.</param>
public void Resume(TimeSpan period) => this.Change(TimeSpan.Zero, period);
/// <summary>
/// Pauses this instance.
/// </summary>
public void Pause() => this.Change(Timeout.Infinite, Timeout.Infinite);
/// <inheritdoc />
public void Dispose() {
lock(this._syncLock) {
if(this._isDisposed == true || this._isDisposing == true) {
return;
}
this._isDisposing.Value = true;
}
try {
this._cycleDoneEvent.Wait();
this._cycleDoneEvent.Dispose();
this.Pause();
this._backingTimer.Dispose();
} finally {
this._isDisposed.Value = true;
this._isDisposing.Value = false;
}
}
/// <summary>
/// Logic that runs every time the timer hits the due time.
/// </summary>
/// <param name="state">The state.</param>
private void InternalCallback(Object state) {
lock(this._syncLock) {
if(this.IsDisposed || this.IsDisposing) {
return;
}
}
if(this._cycleDoneEvent.IsSet == false) {
return;
}
this._cycleDoneEvent.Reset();
try {
this._userCallback(state);
} finally {
this._cycleDoneEvent?.Set();
_ = this._backingTimer?.Change(this._period, Timeout.Infinite);
}
}
}
}

View File

@ -1,24 +1,22 @@
using System;
namespace Swan.Threading
{
namespace Swan.Threading {
/// <summary>
/// Defines a generic interface for synchronized locking mechanisms.
/// </summary>
public interface ISyncLocker : IDisposable {
/// <summary>
/// Defines a generic interface for synchronized locking mechanisms.
/// Acquires a writer lock.
/// The lock is released when the returned locking object is disposed.
/// </summary>
public interface ISyncLocker : IDisposable
{
/// <summary>
/// Acquires a writer lock.
/// The lock is released when the returned locking object is disposed.
/// </summary>
/// <returns>A disposable locking object.</returns>
IDisposable AcquireWriterLock();
/// <summary>
/// Acquires a reader lock.
/// The lock is released when the returned locking object is disposed.
/// </summary>
/// <returns>A disposable locking object.</returns>
IDisposable AcquireReaderLock();
}
/// <returns>A disposable locking object.</returns>
IDisposable AcquireWriterLock();
/// <summary>
/// Acquires a reader lock.
/// The lock is released when the returned locking object is disposed.
/// </summary>
/// <returns>A disposable locking object.</returns>
IDisposable AcquireReaderLock();
}
}

View File

@ -1,57 +1,63 @@
using System;
namespace Swan.Threading
{
namespace Swan.Threading {
/// <summary>
/// Provides a generalized API for ManualResetEvent and ManualResetEventSlim.
/// </summary>
/// <seealso cref="IDisposable" />
public interface IWaitEvent : IDisposable {
/// <summary>
/// Provides a generalized API for ManualResetEvent and ManualResetEventSlim.
/// Gets a value indicating whether the event is in the completed state.
/// </summary>
/// <seealso cref="IDisposable" />
public interface IWaitEvent : IDisposable
{
/// <summary>
/// Gets a value indicating whether the event is in the completed state.
/// </summary>
bool IsCompleted { get; }
/// <summary>
/// Gets a value indicating whether the Begin method has been called.
/// It returns false after the Complete method is called.
/// </summary>
bool IsInProgress { get; }
/// <summary>
/// Returns true if the underlying handle is not closed and it is still valid.
/// </summary>
bool IsValid { get; }
/// <summary>
/// Gets a value indicating whether this instance is disposed.
/// </summary>
bool IsDisposed { get; }
/// <summary>
/// Enters the state in which waiters need to wait.
/// All future waiters will block when they call the Wait method.
/// </summary>
void Begin();
/// <summary>
/// Leaves the state in which waiters need to wait.
/// All current waiters will continue.
/// </summary>
void Complete();
/// <summary>
/// Waits for the event to be completed.
/// </summary>
void Wait();
/// <summary>
/// Waits for the event to be completed.
/// Returns <c>true</c> when there was no timeout. False if the timeout was reached.
/// </summary>
/// <param name="timeout">The maximum amount of time to wait for.</param>
/// <returns><c>true</c> when there was no timeout. <c>false</c> if the timeout was reached.</returns>
bool Wait(TimeSpan timeout);
}
Boolean IsCompleted {
get;
}
/// <summary>
/// Gets a value indicating whether the Begin method has been called.
/// It returns false after the Complete method is called.
/// </summary>
Boolean IsInProgress {
get;
}
/// <summary>
/// Returns true if the underlying handle is not closed and it is still valid.
/// </summary>
Boolean IsValid {
get;
}
/// <summary>
/// Gets a value indicating whether this instance is disposed.
/// </summary>
Boolean IsDisposed {
get;
}
/// <summary>
/// Enters the state in which waiters need to wait.
/// All future waiters will block when they call the Wait method.
/// </summary>
void Begin();
/// <summary>
/// Leaves the state in which waiters need to wait.
/// All current waiters will continue.
/// </summary>
void Complete();
/// <summary>
/// Waits for the event to be completed.
/// </summary>
void Wait();
/// <summary>
/// Waits for the event to be completed.
/// Returns <c>true</c> when there was no timeout. False if the timeout was reached.
/// </summary>
/// <param name="timeout">The maximum amount of time to wait for.</param>
/// <returns><c>true</c> when there was no timeout. <c>false</c> if the timeout was reached.</returns>
Boolean Wait(TimeSpan timeout);
}
}

View File

@ -1,69 +1,77 @@
using System;
using System.Threading.Tasks;
namespace Swan.Threading
{
namespace Swan.Threading {
/// <summary>
/// Defines a standard API to control background application workers.
/// </summary>
public interface IWorker {
/// <summary>
/// Defines a standard API to control background application workers.
/// Gets the current state of the worker.
/// </summary>
public interface IWorker
{
/// <summary>
/// Gets the current state of the worker.
/// </summary>
WorkerState WorkerState { get; }
/// <summary>
/// Gets a value indicating whether this instance is disposed.
/// </summary>
/// <value>
/// <c>true</c> if this instance is disposed; otherwise, <c>false</c>.
/// </value>
bool IsDisposed { get; }
/// <summary>
/// Gets a value indicating whether this instance is currently being disposed.
/// </summary>
/// <value>
/// <c>true</c> if this instance is disposing; otherwise, <c>false</c>.
/// </value>
bool IsDisposing { get; }
/// <summary>
/// Gets or sets the time interval used to execute cycles.
/// </summary>
TimeSpan Period { get; set; }
/// <summary>
/// Gets the name identifier of this worker.
/// </summary>
string Name { get; }
/// <summary>
/// Starts execution of worker cycles.
/// </summary>
/// <returns>The awaitable task.</returns>
Task<WorkerState> StartAsync();
/// <summary>
/// Pauses execution of worker cycles.
/// </summary>
/// <returns>The awaitable task.</returns>
Task<WorkerState> PauseAsync();
/// <summary>
/// Resumes execution of worker cycles.
/// </summary>
/// <returns>The awaitable task.</returns>
Task<WorkerState> ResumeAsync();
/// <summary>
/// Permanently stops execution of worker cycles.
/// An interrupt is always sent to the worker. If you wish to stop
/// the worker without interrupting then call the <see cref="PauseAsync"/>
/// method, await it, and finally call the <see cref="StopAsync"/> method.
/// </summary>
/// <returns>The awaitable task.</returns>
Task<WorkerState> StopAsync();
}
WorkerState WorkerState {
get;
}
/// <summary>
/// Gets a value indicating whether this instance is disposed.
/// </summary>
/// <value>
/// <c>true</c> if this instance is disposed; otherwise, <c>false</c>.
/// </value>
Boolean IsDisposed {
get;
}
/// <summary>
/// Gets a value indicating whether this instance is currently being disposed.
/// </summary>
/// <value>
/// <c>true</c> if this instance is disposing; otherwise, <c>false</c>.
/// </value>
Boolean IsDisposing {
get;
}
/// <summary>
/// Gets or sets the time interval used to execute cycles.
/// </summary>
TimeSpan Period {
get; set;
}
/// <summary>
/// Gets the name identifier of this worker.
/// </summary>
String Name {
get;
}
/// <summary>
/// Starts execution of worker cycles.
/// </summary>
/// <returns>The awaitable task.</returns>
Task<WorkerState> StartAsync();
/// <summary>
/// Pauses execution of worker cycles.
/// </summary>
/// <returns>The awaitable task.</returns>
Task<WorkerState> PauseAsync();
/// <summary>
/// Resumes execution of worker cycles.
/// </summary>
/// <returns>The awaitable task.</returns>
Task<WorkerState> ResumeAsync();
/// <summary>
/// Permanently stops execution of worker cycles.
/// An interrupt is always sent to the worker. If you wish to stop
/// the worker without interrupting then call the <see cref="PauseAsync"/>
/// method, await it, and finally call the <see cref="StopAsync"/> method.
/// </summary>
/// <returns>The awaitable task.</returns>
Task<WorkerState> StopAsync();
}
}

View File

@ -1,21 +1,20 @@
using System.Threading;
using System;
using System.Threading;
using System.Threading.Tasks;
namespace Swan.Threading
{
namespace Swan.Threading {
/// <summary>
/// An interface for a worker cycle delay provider.
/// </summary>
public interface IWorkerDelayProvider {
/// <summary>
/// An interface for a worker cycle delay provider.
/// Suspends execution queues a new cycle for execution. The delay is given in
/// milliseconds. When overridden in a derived class the wait handle will be set
/// whenever an interrupt is received.
/// </summary>
public interface IWorkerDelayProvider
{
/// <summary>
/// Suspends execution queues a new cycle for execution. The delay is given in
/// milliseconds. When overridden in a derived class the wait handle will be set
/// whenever an interrupt is received.
/// </summary>
/// <param name="wantedDelay">The remaining delay to wait for in the cycle.</param>
/// <param name="delayTask">Contains a reference to a task with the scheduled period delay.</param>
/// <param name="token">The cancellation token to cancel waiting.</param>
void ExecuteCycleDelay(int wantedDelay, Task delayTask, CancellationToken token);
}
/// <param name="wantedDelay">The remaining delay to wait for in the cycle.</param>
/// <param name="delayTask">Contains a reference to a task with the scheduled period delay.</param>
/// <param name="token">The cancellation token to cancel waiting.</param>
void ExecuteCycleDelay(Int32 wantedDelay, Task delayTask, CancellationToken token);
}
}

Some files were not shown because too many files have changed in this diff Show More