提交修改
This commit is contained in:
@@ -1,8 +0,0 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 40200134f8074457ab6709111b2d3046
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -1,37 +0,0 @@
|
||||
#if !FANTASY_WEBGL
|
||||
using System;
|
||||
using System.Collections.Concurrent;
|
||||
using System.Runtime.CompilerServices;
|
||||
#pragma warning disable CS8603 // Possible null reference return.
|
||||
|
||||
namespace NBC.Pool
|
||||
{
|
||||
/// <summary>
|
||||
/// 线程安全的静态通用对象池。
|
||||
/// </summary>
|
||||
internal static class MultiThreadPool
|
||||
{
|
||||
private static readonly ConcurrentDictionary<Type, MultiThreadPoolQueue> ObjectPools = new ConcurrentDictionary<Type, MultiThreadPoolQueue>();
|
||||
|
||||
public static T Rent<T>() where T : IPool, new()
|
||||
{
|
||||
return ObjectPools.GetOrAdd(typeof(T), t => new MultiThreadPoolQueue(2000, () => new T())).Rent<T>();
|
||||
}
|
||||
|
||||
public static IPool Rent(Type type)
|
||||
{
|
||||
return ObjectPools.GetOrAdd(type, t => new MultiThreadPoolQueue(2000, CreateInstance.CreateIPool(type))).Rent();
|
||||
}
|
||||
|
||||
public static void Return<T>(T obj) where T : IPool, new()
|
||||
{
|
||||
if (!obj.IsPool())
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
ObjectPools.GetOrAdd(typeof(T), t => new MultiThreadPoolQueue(2000, () => new T())).Return(obj);
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
@@ -1,11 +0,0 @@
|
||||
fileFormatVersion: 2
|
||||
guid: f4295246cc4ad4e9ab1c09acbc4bbbc1
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -1,76 +0,0 @@
|
||||
#if !FANTASY_WEBGL
|
||||
using System;
|
||||
using System.Collections.Concurrent;
|
||||
using System.Threading;
|
||||
// ReSharper disable ConditionIsAlwaysTrueOrFalseAccordingToNullableAPIContract
|
||||
#pragma warning disable CS8601 // Possible null reference assignment.
|
||||
#pragma warning disable CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider declaring as nullable.
|
||||
#pragma warning disable CS8603 // Possible null reference return.
|
||||
|
||||
namespace NBC.Pool
|
||||
{
|
||||
/// <summary>
|
||||
/// 线程安全的对象池。
|
||||
/// </summary>
|
||||
internal class MultiThreadPoolQueue
|
||||
{
|
||||
private int _poolCount;
|
||||
private readonly int _maxCapacity;
|
||||
private readonly Func<IPool> _createInstance;
|
||||
private readonly ConcurrentQueue<IPool> _poolQueue = new ConcurrentQueue<IPool>();
|
||||
private MultiThreadPoolQueue() { }
|
||||
|
||||
public MultiThreadPoolQueue(int maxCapacity, Func<IPool> createInstance)
|
||||
{
|
||||
_maxCapacity = maxCapacity;
|
||||
_createInstance = createInstance;
|
||||
}
|
||||
|
||||
public T Rent<T>() where T : IPool, new()
|
||||
{
|
||||
if (!_poolQueue.TryDequeue(out var t))
|
||||
{
|
||||
var pool = new T();
|
||||
pool.SetIsPool(true);
|
||||
return pool;
|
||||
}
|
||||
|
||||
t.SetIsPool(true);
|
||||
Interlocked.Decrement(ref _poolCount);
|
||||
return (T)t;
|
||||
}
|
||||
|
||||
public IPool Rent()
|
||||
{
|
||||
if (!_poolQueue.TryDequeue(out var t))
|
||||
{
|
||||
var instance = _createInstance();
|
||||
instance.SetIsPool(true);
|
||||
return instance;
|
||||
}
|
||||
|
||||
t.SetIsPool(true);
|
||||
Interlocked.Decrement(ref _poolCount);
|
||||
return t;
|
||||
}
|
||||
|
||||
public void Return(IPool obj)
|
||||
{
|
||||
if (!obj.IsPool())
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
obj.SetIsPool(false);
|
||||
|
||||
if (Interlocked.Increment(ref _poolCount) <= _maxCapacity)
|
||||
{
|
||||
_poolQueue.Enqueue(obj);
|
||||
return;
|
||||
}
|
||||
|
||||
Interlocked.Decrement(ref _poolCount);
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
@@ -1,11 +0,0 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 5520348a236554f5380103f259f87dfd
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -1,8 +0,0 @@
|
||||
fileFormatVersion: 2
|
||||
guid: e11c514da50264cc986ac5c6a85ea450
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -1,18 +0,0 @@
|
||||
namespace NBC.Pool
|
||||
{
|
||||
/// <summary>
|
||||
/// 实现了这个接口代表支持对象池
|
||||
/// </summary>
|
||||
public interface IPool
|
||||
{
|
||||
/// <summary>
|
||||
/// 是否从池里创建的
|
||||
/// </summary>
|
||||
bool IsPool();
|
||||
/// <summary>
|
||||
/// 设置是否从池里创建的
|
||||
/// </summary>
|
||||
/// <param name="isPool"></param>
|
||||
void SetIsPool(bool isPool);
|
||||
}
|
||||
}
|
||||
@@ -1,11 +0,0 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 91cd6661b42dc41cfaad00203a86ce3c
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -1,8 +0,0 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 554c5e3bc9c1844f99046f84eb4c2de2
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -1,77 +0,0 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
// ReSharper disable ConditionIsAlwaysTrueOrFalseAccordingToNullableAPIContract
|
||||
// ReSharper disable CheckNamespace
|
||||
|
||||
namespace NBC.Pool
|
||||
{
|
||||
/// <summary>
|
||||
/// 静态的对象池系统,不支持多线程。
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
public static class Pool<T> where T : IPool, new()
|
||||
{
|
||||
private static readonly Queue<T> PoolQueue = new Queue<T>();
|
||||
/// <summary>
|
||||
/// 池子里可用的数量
|
||||
/// </summary>
|
||||
public static int Count => PoolQueue.Count;
|
||||
|
||||
/// <summary>
|
||||
/// 租借
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public static T Rent()
|
||||
{
|
||||
return PoolQueue.Count == 0 ? new T() : PoolQueue.Dequeue();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 租借
|
||||
/// </summary>
|
||||
/// <param name="generator">如果池子里没有,会先执行这个委托。</param>
|
||||
/// <returns></returns>
|
||||
public static T Rent(Func<T> generator)
|
||||
{
|
||||
return PoolQueue.Count == 0 ? generator() : PoolQueue.Dequeue();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 返还
|
||||
/// </summary>
|
||||
/// <param name="t"></param>
|
||||
public static void Return(T t)
|
||||
{
|
||||
if (t == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
PoolQueue.Enqueue(t);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 返还
|
||||
/// </summary>
|
||||
/// <param name="t">返还的东西</param>
|
||||
/// <param name="reset">返还后执行的委托</param>
|
||||
public static void Return(T t, Action<T> reset)
|
||||
{
|
||||
if (t == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
reset(t);
|
||||
PoolQueue.Enqueue(t);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 清空池子
|
||||
/// </summary>
|
||||
public static void Clear()
|
||||
{
|
||||
PoolQueue.Clear();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,11 +0,0 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 6bf3e1dd4e39a4ae4ababd3770ed431d
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -1,203 +0,0 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Runtime.CompilerServices;
|
||||
using NBC.DataStructure.Collection;
|
||||
|
||||
// ReSharper disable ConditionIsAlwaysTrueOrFalseAccordingToNullableAPIContract
|
||||
#pragma warning disable CS8600 // Converting null literal or possible null value to non-nullable type.
|
||||
#pragma warning disable CS8603 // Possible null reference return.
|
||||
|
||||
namespace NBC.Pool
|
||||
{
|
||||
/// <summary>
|
||||
/// 对象池抽象接口,用于创建和管理可重复使用的对象实例。
|
||||
/// </summary>
|
||||
public abstract class PoolCore : IDisposable
|
||||
{
|
||||
private int _poolCount;
|
||||
private readonly int _maxCapacity;
|
||||
/// <summary>
|
||||
/// 池子里可用的数量
|
||||
/// </summary>
|
||||
public int Count => _poolQueue.Count;
|
||||
private readonly OneToManyQueue<Type, IPool> _poolQueue = new OneToManyQueue<Type, IPool>();
|
||||
private readonly Dictionary<Type, Func<IPool>> _typeCheckCache = new Dictionary<Type, Func<IPool>>();
|
||||
|
||||
/// <summary>
|
||||
/// 构造函数
|
||||
/// </summary>
|
||||
/// <param name="maxCapacity">初始的容量</param>
|
||||
protected PoolCore(int maxCapacity)
|
||||
{
|
||||
_maxCapacity = maxCapacity;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 租借
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <returns></returns>
|
||||
public T Rent<T>() where T : IPool, new()
|
||||
{
|
||||
if (!_poolQueue.TryDequeue(typeof(T), out var queue))
|
||||
{
|
||||
queue = new T();
|
||||
}
|
||||
|
||||
queue.SetIsPool(true);
|
||||
_poolCount--;
|
||||
return (T)queue;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 租借
|
||||
/// </summary>
|
||||
/// <param name="type">租借的类型</param>
|
||||
/// <returns></returns>
|
||||
/// <exception cref="NotSupportedException"></exception>
|
||||
public IPool Rent(Type type)
|
||||
{
|
||||
if (!_poolQueue.TryDequeue(type, out var queue))
|
||||
{
|
||||
if (!_typeCheckCache.TryGetValue(type, out var createInstance))
|
||||
{
|
||||
if (!typeof(IPool).IsAssignableFrom(type))
|
||||
{
|
||||
throw new NotSupportedException($"{this.GetType().FullName} Type:{type.FullName} must inherit from IPool");
|
||||
}
|
||||
else
|
||||
{
|
||||
createInstance = CreateInstance.CreateIPool(type);
|
||||
_typeCheckCache[type] = createInstance;
|
||||
}
|
||||
}
|
||||
|
||||
var instance = createInstance();
|
||||
instance.SetIsPool(true);
|
||||
return instance;
|
||||
}
|
||||
|
||||
queue.SetIsPool(true);
|
||||
_poolCount--;
|
||||
return queue;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 返还
|
||||
/// </summary>
|
||||
/// <param name="type"></param>
|
||||
/// <param name="obj"></param>
|
||||
public void Return(Type type, IPool obj)
|
||||
{
|
||||
if (obj == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (!obj.IsPool())
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (_poolCount >= _maxCapacity)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
_poolCount++;
|
||||
obj.SetIsPool(false);
|
||||
_poolQueue.Enqueue(type, obj);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 销毁方法
|
||||
/// </summary>
|
||||
public virtual void Dispose()
|
||||
{
|
||||
_poolCount = 0;
|
||||
_poolQueue.Clear();
|
||||
_typeCheckCache.Clear();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 泛型对象池核心类,用于创建和管理可重复使用的对象实例。
|
||||
/// </summary>
|
||||
/// <typeparam name="T">要池化的对象类型</typeparam>
|
||||
public abstract class PoolCore<T> where T : IPool, new()
|
||||
{
|
||||
private int _poolCount;
|
||||
private readonly int _maxCapacity;
|
||||
private readonly Queue<T> _poolQueue = new Queue<T>();
|
||||
/// <summary>
|
||||
/// 池子里可用的数量
|
||||
/// </summary>
|
||||
public int Count => _poolQueue.Count;
|
||||
|
||||
/// <summary>
|
||||
/// 构造函数
|
||||
/// </summary>
|
||||
/// <param name="maxCapacity">初始的容量</param>
|
||||
protected PoolCore(int maxCapacity)
|
||||
{
|
||||
_maxCapacity = maxCapacity;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 租借
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public virtual T Rent()
|
||||
{
|
||||
T dequeue;
|
||||
|
||||
if (_poolQueue.Count == 0)
|
||||
{
|
||||
dequeue = new T();
|
||||
}
|
||||
else
|
||||
{
|
||||
_poolCount--;
|
||||
dequeue = _poolQueue.Dequeue();
|
||||
}
|
||||
|
||||
dequeue.SetIsPool(true);
|
||||
return dequeue;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 返还
|
||||
/// </summary>
|
||||
/// <param name="item"></param>
|
||||
public virtual void Return(T item)
|
||||
{
|
||||
if (item == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (!item.IsPool())
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (_poolCount >= _maxCapacity)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
_poolCount++;
|
||||
item.SetIsPool(false);
|
||||
_poolQueue.Enqueue(item);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 销毁方法
|
||||
/// </summary>
|
||||
public virtual void Dispose()
|
||||
{
|
||||
_poolCount = 0;
|
||||
_poolQueue.Clear();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,11 +0,0 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 12336238185b447c1af2fbd1eb1f2b7f
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -1,140 +0,0 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
// ReSharper disable ConditionIsAlwaysTrueOrFalseAccordingToNullableAPIContract
|
||||
|
||||
namespace NBC.Pool
|
||||
{
|
||||
/// <summary>
|
||||
/// 静态通用对象池,用于存储实现了 IDisposable 接口的对象。
|
||||
/// </summary>
|
||||
/// <typeparam name="T">要存储在对象池中的对象类型,必须实现 IDisposable 接口。</typeparam>
|
||||
public abstract class PoolWithDisposable<T> : IDisposable where T : IPool, IDisposable, new()
|
||||
{
|
||||
private int _poolCount;
|
||||
private readonly int _maxCapacity;
|
||||
private readonly Queue<T> _poolQueue = new Queue<T>();
|
||||
/// <summary>
|
||||
/// 池子里可用的数量
|
||||
/// </summary>
|
||||
public int Count => _poolQueue.Count;
|
||||
|
||||
/// <summary>
|
||||
/// 构造函数
|
||||
/// </summary>
|
||||
/// <param name="maxCapacity">初始的容量</param>
|
||||
protected PoolWithDisposable(int maxCapacity)
|
||||
{
|
||||
_maxCapacity = maxCapacity;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 租借
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public T Rent()
|
||||
{
|
||||
T dequeue;
|
||||
if (_poolQueue.Count == 0)
|
||||
{
|
||||
dequeue = new T();
|
||||
}
|
||||
else
|
||||
{
|
||||
_poolCount--;
|
||||
dequeue = _poolQueue.Dequeue();
|
||||
}
|
||||
|
||||
dequeue.SetIsPool(true);
|
||||
return dequeue;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 租借
|
||||
/// </summary>
|
||||
/// <param name="generator"></param>
|
||||
/// <returns></returns>
|
||||
public T Rent(Func<T> generator)
|
||||
{
|
||||
T dequeue;
|
||||
|
||||
if (_poolQueue.Count == 0)
|
||||
{
|
||||
dequeue = generator();
|
||||
}
|
||||
else
|
||||
{
|
||||
_poolCount--;
|
||||
dequeue = _poolQueue.Dequeue();
|
||||
}
|
||||
|
||||
dequeue.SetIsPool(true);
|
||||
return dequeue;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 返还
|
||||
/// </summary>
|
||||
/// <param name="t"></param>
|
||||
public void Return(T t)
|
||||
{
|
||||
if (t == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (!t.IsPool())
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (_poolCount >= _maxCapacity)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
_poolCount++;
|
||||
t.SetIsPool(true);
|
||||
_poolQueue.Enqueue(t);
|
||||
t.Dispose();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 返还
|
||||
/// </summary>
|
||||
/// <param name="t"></param>
|
||||
/// <param name="reset"></param>
|
||||
public void Return(T t, Action<T> reset)
|
||||
{
|
||||
if (t == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (!t.IsPool())
|
||||
{
|
||||
reset(t);
|
||||
return;
|
||||
}
|
||||
|
||||
if (_poolCount >= _maxCapacity)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
reset(t);
|
||||
_poolCount++;
|
||||
t.SetIsPool(false);
|
||||
_poolQueue.Enqueue(t);
|
||||
t.Dispose();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 销毁方法
|
||||
/// </summary>
|
||||
public virtual void Dispose()
|
||||
{
|
||||
_poolCount = 0;
|
||||
_poolQueue.Clear();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,11 +0,0 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 37d96a9ea60d8412c9c549446e970aaf
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -1,79 +0,0 @@
|
||||
using System;
|
||||
using System.Reflection.Emit;
|
||||
using NBC.Serialize;
|
||||
|
||||
#pragma warning disable CS8604 // Possible null reference argument.
|
||||
|
||||
namespace NBC.Pool
|
||||
{
|
||||
internal static class CreateInstance<T> where T : IPool
|
||||
{
|
||||
public static Func<T> Create { get; }
|
||||
|
||||
static CreateInstance()
|
||||
{
|
||||
var type = typeof(T);
|
||||
var dynamicMethod = new DynamicMethod($"CreateInstance_{type.Name}", type, Type.EmptyTypes, true);
|
||||
var il = dynamicMethod.GetILGenerator();
|
||||
il.Emit(OpCodes.Newobj, type.GetConstructor(Type.EmptyTypes));
|
||||
il.Emit(OpCodes.Ret);
|
||||
Create = (Func<T>) dynamicMethod.CreateDelegate(typeof(Func<T>));
|
||||
}
|
||||
}
|
||||
|
||||
internal static class CreateInstance
|
||||
{
|
||||
public static Func<IPool> CreateIPool(Type type)
|
||||
{
|
||||
var dynamicMethod = new DynamicMethod($"CreateInstance_{type.Name}", type, Type.EmptyTypes, true);
|
||||
var il = dynamicMethod.GetILGenerator();
|
||||
il.Emit(OpCodes.Newobj, type.GetConstructor(Type.EmptyTypes));
|
||||
il.Emit(OpCodes.Ret);
|
||||
return (Func<IPool>)dynamicMethod.CreateDelegate(typeof(Func<IPool>));
|
||||
}
|
||||
|
||||
public static Func<object> CreateObject(Type type)
|
||||
{
|
||||
var dynamicMethod = new DynamicMethod($"CreateInstance_{type.Name}", type, Type.EmptyTypes, true);
|
||||
var il = dynamicMethod.GetILGenerator();
|
||||
il.Emit(OpCodes.Newobj, type.GetConstructor(Type.EmptyTypes));
|
||||
il.Emit(OpCodes.Ret);
|
||||
return (Func<object>)dynamicMethod.CreateDelegate(typeof(Func<object>));
|
||||
}
|
||||
|
||||
public static Func<AMessage> CreateMessage(Type type)
|
||||
{
|
||||
var dynamicMethod = new DynamicMethod($"CreateInstance_{type.Name}", type, Type.EmptyTypes, true);
|
||||
var il = dynamicMethod.GetILGenerator();
|
||||
il.Emit(OpCodes.Newobj, type.GetConstructor(Type.EmptyTypes));
|
||||
il.Emit(OpCodes.Ret);
|
||||
return (Func<AMessage>)dynamicMethod.CreateDelegate(typeof(Func<AMessage>));
|
||||
}
|
||||
}
|
||||
|
||||
// public static class CreateInstance
|
||||
// {
|
||||
// public static Func<IPool> Create(Type type)
|
||||
// {
|
||||
// var dynamicMethod = new DynamicMethod($"CreateInstance_{type.Name}", type, Type.EmptyTypes, true);
|
||||
// var il = dynamicMethod.GetILGenerator();
|
||||
// il.Emit(OpCodes.Newobj, type.GetConstructor(Type.EmptyTypes));
|
||||
// il.Emit(OpCodes.Ret);
|
||||
// return (Func<IPool>)dynamicMethod.CreateDelegate(typeof(Func<IPool>));
|
||||
// }
|
||||
// }
|
||||
|
||||
// /// <summary>
|
||||
// /// 利用泛型的特性来减少反射的使用。
|
||||
// /// </summary>
|
||||
// /// <typeparam name="T"></typeparam>
|
||||
// public static class PoolChecker<T> where T : new()
|
||||
// {
|
||||
// public static bool IsPool { get; }
|
||||
//
|
||||
// static PoolChecker()
|
||||
// {
|
||||
// IsPool = typeof(IPool).IsAssignableFrom(typeof(T));
|
||||
// }
|
||||
// }
|
||||
}
|
||||
@@ -1,11 +0,0 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 4ad79f875091b4737a22bfd0e0e686ea
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Reference in New Issue
Block a user