Added FisheryLib.Collections.FishTable and Mono.Cecil extension methods
A bit of refactoring has been done too
This commit is contained in:
@@ -4,8 +4,11 @@
|
||||
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
||||
|
||||
using System.Collections.Concurrent;
|
||||
using JetBrains.Annotations;
|
||||
|
||||
namespace FisheryLib;
|
||||
|
||||
[PublicAPI]
|
||||
public abstract class SingletonFactory<T_base> where T_base : notnull
|
||||
{
|
||||
/// <summary>
|
||||
@@ -13,8 +16,7 @@ public abstract class SingletonFactory<T_base> where T_base : notnull
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of the instance</typeparam>
|
||||
/// <returns>The Singleton instance</returns>
|
||||
public static T Get<T>() where T : T_base
|
||||
=> (T)GetInternal(typeof(T));
|
||||
public static T Get<T>() where T : T_base => (T)GetInternal(typeof(T));
|
||||
|
||||
/// <summary>
|
||||
/// Get a Singleton instance of a type derived from T_base
|
||||
@@ -24,21 +26,31 @@ public abstract class SingletonFactory<T_base> where T_base : notnull
|
||||
public static T_base Get(Type type)
|
||||
{
|
||||
if (!typeof(T_base).IsAssignableFrom(type))
|
||||
ThrowHelper.ThrowArgumentException($"Parameters for {typeof(T_base).Name}.Get must be assignable from Type {typeof(T_base).Name}. Got {type.FullDescription()} instead.");
|
||||
{
|
||||
ThrowHelper.ThrowArgumentException(
|
||||
$"Parameters for {typeof(T_base).Name}.Get must be assignable from Type {
|
||||
typeof(T_base).Name}. Got {type.FullDescription()} instead.");
|
||||
}
|
||||
|
||||
return GetInternal(type);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// protected to allow declaring of default values on derived types. Do not directly invoke this constructor or expose it as public.
|
||||
/// protected to allow declaring of default values on derived types. Do not directly invoke this constructor or
|
||||
/// expose it as public.
|
||||
/// </summary>
|
||||
protected SingletonFactory()
|
||||
{
|
||||
if (_instances.ContainsKey(GetType()))
|
||||
ThrowHelper.ThrowInvalidOperationException($"Tried creating a second instance of {GetType().Name}. Only the {typeof(T_base).Name}.Get method should be used for instantiation.");
|
||||
{
|
||||
ThrowHelper.ThrowInvalidOperationException(
|
||||
$"Tried creating a second instance of {GetType().Name}. Only the {
|
||||
typeof(T_base).Name}.Get method should be used for instantiation.");
|
||||
}
|
||||
}
|
||||
|
||||
private static T_base GetInternal(Type type) => _instances.GetOrAdd(type, type => (T_base)Activator.CreateInstance(type, true));
|
||||
private static T_base GetInternal(Type type)
|
||||
=> _instances.GetOrAdd(type, static type => (T_base)Activator.CreateInstance(type, true));
|
||||
|
||||
private static readonly ConcurrentDictionary<Type, T_base> _instances = new();
|
||||
}
|
||||
Reference in New Issue
Block a user