Example #1
0
        public Action <object, object[]> GetVoidMethodInvoker(
            MethodInfo methodInfo)
        {
            Action <object, object[]> action;

            var lockSlim   = voidMethodLockSlim;
            var dictionary = voidMethodDictionary;

            lockSlim.EnterUpgradeableReadLock();
            try
            {
                if (!dictionary.TryGetValue(methodInfo, out action))
                {
                    lockSlim.EnterWriteLock();
                    try
                    {
                        if (!dictionary.TryGetValue(methodInfo, out action))
                        {
                            action = ReflectionCompiler.CreateMethodAction(methodInfo);
                            dictionary[methodInfo] = action;
                        }
                    }
                    finally
                    {
                        lockSlim.ExitWriteLock();
                    }
                }
            }
            finally
            {
                lockSlim.ExitUpgradeableReadLock();
            }

            return(action);
        }
Example #2
0
        public Action <object, object> GetPropertySetter(
            PropertyInfo propertyInfo)
        {
            Action <object, object> setter;

            var lockSlim   = setterLockSlim;
            var dictionary = setterDictionary;

            lockSlim.EnterUpgradeableReadLock();
            try
            {
                if (!dictionary.TryGetValue(propertyInfo, out setter))
                {
                    lockSlim.EnterWriteLock();
                    try
                    {
                        if (!dictionary.TryGetValue(propertyInfo, out setter))
                        {
                            setter = ReflectionCompiler.CreatePropertySetter(propertyInfo);
                            dictionary[propertyInfo] = setter;
                        }
                    }
                    finally
                    {
                        lockSlim.ExitWriteLock();
                    }
                }
            }
            finally
            {
                lockSlim.ExitUpgradeableReadLock();
            }

            return(setter);
        }
Example #3
0
        public Func <object, object[], object> GetMethodInvoker(
            MethodInfo methodInfo)
        {
            Func <object, object[], object> func;

            var lockSlim   = nonVoidMethodLockSlim;
            var dictionary = nonVoidMethodDictionary;

            lockSlim.EnterUpgradeableReadLock();
            try
            {
                if (!dictionary.TryGetValue(methodInfo, out func))
                {
                    lockSlim.EnterWriteLock();
                    try
                    {
                        if (!dictionary.TryGetValue(methodInfo, out func))
                        {
                            func = ReflectionCompiler.CreateMethodFunc(methodInfo);
                            dictionary[methodInfo] = func;
                        }
                    }
                    finally
                    {
                        lockSlim.ExitWriteLock();
                    }
                }
            }
            finally
            {
                lockSlim.ExitUpgradeableReadLock();
            }

            return(func);
        }
Example #4
0
        /* The Get* methods in this class are deliberately more verbose
         * than they need to be. We could remove the duplication within
         * each code block, however the code needs to be fast,
         * and the overhead of creating an Action for each call
         * would not be worth it. */

        public Func <object, object> GetPropertyGetter(
            PropertyInfo propertyInfo)
        {
            Func <object, object> getter;

            var lockSlim   = getterLockSlim;
            var dictionary = getterGenericDictionary;

            lockSlim.EnterUpgradeableReadLock();
            try
            {
                if (!dictionary.TryGetValue(propertyInfo, out getter))
                {
                    lockSlim.EnterWriteLock();
                    try
                    {
                        if (!dictionary.TryGetValue(propertyInfo, out getter))
                        {
                            getter = ReflectionCompiler.CreatePropertyGetter(propertyInfo);
                            dictionary[propertyInfo] = getter;
                        }
                    }
                    finally
                    {
                        lockSlim.ExitWriteLock();
                    }
                }
            }
            finally
            {
                lockSlim.ExitUpgradeableReadLock();
            }

            return(getter);
        }
Example #5
0
        public Action <object, object> GetPropertySetter(
            PropertyInfo propertyInfo,
            DelegateCreationMode creationMode)
        {
            var dictionary = setterDictionary;

            if (!dictionary.TryGetValue(propertyInfo, out var setter))
            {
                switch (creationMode)
                {
                case DelegateCreationMode.SlowCreationFastPerformance:
                    setter = ReflectionCompiler.CreatePropertySetter(propertyInfo);
                    break;

                default:
                    var setMethod = propertyInfo.SetMethod;
                    setter = (owner, newValue) => setMethod.Invoke(owner, new [] { newValue });
                    break;
                }

                dictionary[propertyInfo] = setter;
            }

            return(setter);
        }
Example #6
0
        public Action <object, TProperty> GetPropertySetter <TProperty>(
            PropertyInfo propertyInfo,
            DelegateCreationMode creationMode)
        {
            var dictionary = setterGenericDictionary;

            if (!dictionary.TryGetValue(propertyInfo, out object result))
            {
                switch (creationMode)
                {
                case DelegateCreationMode.SlowCreationFastPerformance:
                    result = ReflectionCompiler.CreatePropertySetter <TProperty>(propertyInfo);
                    break;

                default:
                    var setMethod = propertyInfo.SetMethod;
                    result = new Action <object, TProperty>(
                        (owner, newValue) => setMethod.Invoke(owner, new object[] { newValue }));
                    break;
                }

                dictionary[propertyInfo] = result;
            }

            return((Action <object, TProperty>)result);
        }
Example #7
0
        public Func <object, object[], TReturn> GetMethodInvoker <TReturn>(
            MethodInfo methodInfo,
            DelegateCreationMode creationMode)
        {
            var dictionary = nonVoidMethodGenericDictionary;

            if (!dictionary.TryGetValue(methodInfo, out object result))
            {
                switch (creationMode)
                {
                case DelegateCreationMode.SlowCreationFastPerformance:
                    result = ReflectionCompiler.CreateMethodFunc <TReturn>(methodInfo);
                    break;

                default:
                    result = new Func <object, object[], TReturn>(
                        (owner, args) => (TReturn)methodInfo.Invoke(owner, args));
                    break;
                }

                dictionary[methodInfo] = result;
            }

            return((Func <object, object[], TReturn>)result);
        }
Example #8
0
        public Func <object, TProperty> GetPropertyGetter <TProperty>(
            PropertyInfo propertyInfo,
            DelegateCreationMode creationMode)
        {
            Func <object, TProperty> result;

            var dictionary = getterDictionary;

            if (!dictionary.TryGetValue(propertyInfo, out object getter))
            {
                switch (creationMode)
                {
                case DelegateCreationMode.SlowCreationFastPerformance:
                    result = ReflectionCompiler.CreatePropertyGetter <TProperty>(propertyInfo);
                    break;

                default:
                    var getMethod = propertyInfo.GetMethod;
                    result = owner => (TProperty)getMethod.Invoke(owner, null);
                    break;
                }

                dictionary[propertyInfo] = result;
                return(result);
            }

            result = (Func <object, TProperty>)getter;
            return(result);
        }
Example #9
0
        public Func <TOwner, TProperty> GetPropertyGetter <TOwner, TProperty>(
            Expression <Func <TOwner, TProperty> > propertyExpression)
        {
            /* TODO: extract property name and cache item in dictionary. */
            Func <TOwner, TProperty> result = ReflectionCompiler.CreatePropertyGetter(propertyExpression);

            return(result);
        }
Example #10
0
        public Func <object, TProperty> GetPropertyGetter <TProperty>(
            PropertyInfo propertyInfo)
        {
            Func <object, TProperty> result;
            object getter;

            var lockSlim   = getterGenericLockSlim;
            var dictionary = getterDictionary;

            lockSlim.EnterUpgradeableReadLock();
            try
            {
                if (!dictionary.TryGetValue(propertyInfo, out getter))
                {
                    lockSlim.EnterWriteLock();
                    try
                    {
                        if (!dictionary.TryGetValue(propertyInfo, out getter))
                        {
                            result = ReflectionCompiler.CreatePropertyGetter <TProperty>(propertyInfo);
                            dictionary[propertyInfo] = result;
                            return(result);
                        }
                    }
                    finally
                    {
                        lockSlim.ExitWriteLock();
                    }
                }
            }
            finally
            {
                lockSlim.ExitUpgradeableReadLock();
            }

            result = (Func <object, TProperty>)getter;
            return(result);
        }
Example #11
0
        public Func <object[], object> GetConstructorFunc(
            ConstructorInfo info, DelegateCreationMode creationMode)
        {
            var dictionary = constructorDictionary;

            if (!dictionary.TryGetValue(info, out Func <object[], object> result))
            {
                switch (creationMode)
                {
                case DelegateCreationMode.SlowCreationFastPerformance:
                    result = ReflectionCompiler.CreateConstructorFunc(info);
                    break;

                default:
                    result = info.Invoke;
                    break;
                }

                dictionary[info] = result;
            }

            return(result);
        }
Example #12
0
        public Action <object, TProperty> GetPropertySetter <TProperty>(
            PropertyInfo propertyInfo)
        {
            object setter;

            var lockSlim   = setterGenericLockSlim;
            var dictionary = setterGenericDictionary;

            lockSlim.EnterUpgradeableReadLock();
            try
            {
                if (!dictionary.TryGetValue(propertyInfo, out setter))
                {
                    lockSlim.EnterWriteLock();
                    try
                    {
                        if (!dictionary.TryGetValue(propertyInfo, out setter))
                        {
                            var result = ReflectionCompiler.CreatePropertySetter <TProperty>(propertyInfo);
                            dictionary[propertyInfo] = result;
                            return(result);
                        }
                    }
                    finally
                    {
                        lockSlim.ExitWriteLock();
                    }
                }
            }
            finally
            {
                lockSlim.ExitUpgradeableReadLock();
            }

            return((Action <object, TProperty>)setter);
        }
Example #13
0
        public Func <object, object[], TReturn> GetMethodInvoker <TReturn>(
            MethodInfo methodInfo)
        {
            object func;

            var lockSlim   = nonVoidMethodGenericLockSlim;
            var dictionary = nonVoidMethodGenericDictionary;

            lockSlim.EnterUpgradeableReadLock();
            try
            {
                if (!dictionary.TryGetValue(methodInfo, out func))
                {
                    lockSlim.EnterWriteLock();
                    try
                    {
                        if (!dictionary.TryGetValue(methodInfo, out func))
                        {
                            var result = ReflectionCompiler.CreateMethodFunc <TReturn>(methodInfo);
                            dictionary[methodInfo] = result;
                            return(result);
                        }
                    }
                    finally
                    {
                        lockSlim.ExitWriteLock();
                    }
                }
            }
            finally
            {
                lockSlim.ExitUpgradeableReadLock();
            }

            return((Func <object, object[], TReturn>)func);
        }
Example #14
0
        /* The Get* methods in this class are deliberately more verbose
         * than they need to be. We could remove the duplication within
         * each code block, however the code needs to be fast,
         * and the overhead of creating an Action for each call
         * would not be worth it. */

        public Func <object, object> GetPropertyGetter(
            PropertyInfo propertyInfo,
            DelegateCreationMode creationMode)
        {
            var dictionary = getterGenericDictionary;

            if (!dictionary.TryGetValue(propertyInfo, out var getter))
            {
                switch (creationMode)
                {
                case DelegateCreationMode.SlowCreationFastPerformance:
                    getter = ReflectionCompiler.CreatePropertyGetter(propertyInfo);
                    break;

                default:
                    getter = propertyInfo.GetValue;
                    break;
                }

                dictionary[propertyInfo] = getter;
            }

            return(getter);
        }
Example #15
0
        public Func <object, object[], object> GetMethodInvoker(
            MethodInfo methodInfo,
            DelegateCreationMode creationMode)
        {
            var dictionary = nonVoidMethodDictionary;

            if (!dictionary.TryGetValue(methodInfo, out var result))
            {
                switch (creationMode)
                {
                case DelegateCreationMode.SlowCreationFastPerformance:
                    result = ReflectionCompiler.CreateMethodFunc(methodInfo);
                    break;

                default:
                    result = methodInfo.Invoke;
                    break;
                }

                dictionary[methodInfo] = result;
            }

            return(result);
        }
Example #16
0
        public Action <object, object[]> GetVoidMethodInvoker(
            MethodInfo methodInfo,
            DelegateCreationMode creationMode)
        {
            var dictionary = voidMethodDictionary;

            if (!dictionary.TryGetValue(methodInfo, out var result))
            {
                switch (creationMode)
                {
                case DelegateCreationMode.SlowCreationFastPerformance:
                    result = ReflectionCompiler.CreateMethodAction(methodInfo);
                    break;

                default:
                    result = (owner, args) => methodInfo.Invoke(owner, args);
                    break;
                }

                dictionary[methodInfo] = result;
            }

            return(result);
        }