using System; using System.Reflection.Emit; using Fantasy.Serialize; #pragma warning disable CS8604 // Possible null reference argument. namespace Fantasy.Pool { internal static class CreateInstance where T : IPool { public static Func 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) dynamicMethod.CreateDelegate(typeof(Func)); } } internal static class CreateInstance { public static Func 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)dynamicMethod.CreateDelegate(typeof(Func)); } public static Func 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)dynamicMethod.CreateDelegate(typeof(Func)); } public static Func 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)dynamicMethod.CreateDelegate(typeof(Func)); } } // public static class CreateInstance // { // public static Func 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)dynamicMethod.CreateDelegate(typeof(Func)); // } // } // /// // /// 利用泛型的特性来减少反射的使用。 // /// // /// // public static class PoolChecker where T : new() // { // public static bool IsPool { get; } // // static PoolChecker() // { // IsPool = typeof(IPool).IsAssignableFrom(typeof(T)); // } // } }