﻿using System;
using System.Collections.Generic;
using System.Linq.Expressions;

namespace LiteNetLibManager.Utils
{
    public class Reflection
    {
        private static readonly Dictionary<string, ObjectActivator> objectActivators = new Dictionary<string, ObjectActivator>();
        private static string tempTypeName;

        // Improve reflection constructor performance with Linq expression (https://rogerjohansson.blog/2008/02/28/linq-expressions-creating-objects/)
        public delegate object ObjectActivator();
        public static ObjectActivator GetActivator(Type type)
        {
            tempTypeName = type.FullName;
            if (!objectActivators.ContainsKey(tempTypeName))
            {
                if (type.IsClass)
                    objectActivators.Add(tempTypeName, Expression.Lambda<ObjectActivator>(Expression.New(type)).Compile());
                else
                    objectActivators.Add(tempTypeName, Expression.Lambda<ObjectActivator>(Expression.Convert(Expression.New(type), typeof(object))).Compile());
            }
            return objectActivators[tempTypeName];
        }
    }
}

﻿using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Reflection.Emit;

namespace LiteNetLibManager.Utils
{
    public class Reflection
    {
        private static readonly Dictionary<string, Func<object>> expressionActivators = new Dictionary<string, Func<object>>();
        private static readonly Dictionary<string, DynamicMethod> dynamicMethodActivators = new Dictionary<string, DynamicMethod>();

        // Improve reflection constructor performance with Linq expression (https://rogerjohansson.blog/2008/02/28/linq-expressions-creating-objects/)
        public static object GetExpressionActivator(Type type)
        {
            if (!expressionActivators.ContainsKey(type.FullName))
            {
                if (type.IsClass)
                    expressionActivators.Add(type.FullName, Expression.Lambda<Func<object>>(Expression.New(type)).Compile());
                else
                    expressionActivators.Add(type.FullName, Expression.Lambda<Func<object>>(Expression.Convert(Expression.New(type), typeof(object))).Compile());
            }
            return expressionActivators[type.FullName].Invoke();
        }

        public static object GetDynamicMethodActivator(Type type)
        {
            if (!dynamicMethodActivators.ContainsKey(type.FullName))
            {
                DynamicMethod method = new DynamicMethod("", typeof(object), Type.EmptyTypes);
                ILGenerator il = method.GetILGenerator();

                if (type.IsValueType)
                {
                    var local = il.DeclareLocal(type);
                    il.Emit(OpCodes.Ldloc, local);
                    il.Emit(OpCodes.Box, type);
                    il.Emit(OpCodes.Ret);
                }
                else
                {
                    var ctor = type.GetConstructor(Type.EmptyTypes);
                    il.Emit(OpCodes.Newobj, ctor);
                    il.Emit(OpCodes.Ret);
                }
                dynamicMethodActivators.Add(type.FullName, method);
            }
            return dynamicMethodActivators[type.FullName].Invoke(null, null);
        }
    }
}

Add dynamic method constuctor codes (didn't use it yet, other developer may learn something from the function)
Add dynamic method constuctor codes (didn't use it yet, other developer may learn something from the function)
