Ejemplo n.º 1
0
        /// <summary>
        /// Obtains cached constructor info or creates a new entry, if none is found.
        /// </summary>
        private static ConstructorDelegate GetOrCreateDynamicConstructor(ConstructorInfo constructorInfo)
        {
            ConstructorDelegate method;

            if (!constructorCache.TryGetValue(constructorInfo, out method))
            {
                method = DynamicReflectionManager.CreateConstructor(constructorInfo);
                lock (constructorCache)
                {
                    constructorCache[constructorInfo] = method;
                }
            }
            return(method);
        }
Ejemplo n.º 2
0
        /// <summary>
        ///     Obtains cached property info or creates a new entry, if none is found.
        /// </summary>
        private static DynamicPropertyCacheEntry GetOrCreateDynamicProperty(PropertyInfo property)
        {
            DynamicPropertyCacheEntry propertyInfo;

            if (!_propertyCache.TryGetValue(property, out propertyInfo))
            {
                propertyInfo = new DynamicPropertyCacheEntry(DynamicReflectionManager.CreatePropertyGetter(property),
                                                             DynamicReflectionManager.CreatePropertySetter(property));
                lock (_propertyCache)
                {
                    _propertyCache[property] = propertyInfo;
                }
            }
            return(propertyInfo);
        }
Ejemplo n.º 3
0
        /// <summary>
        ///     Obtains cached fieldInfo or creates a new entry, if none is found.
        /// </summary>
        private static DynamicFieldCacheEntry GetOrCreateDynamicField(FieldInfo field)
        {
            DynamicFieldCacheEntry fieldInfo;

            if (!_fieldCache.TryGetValue(field, out fieldInfo))
            {
                fieldInfo = new DynamicFieldCacheEntry(DynamicReflectionManager.CreateFieldGetter(field),
                                                       DynamicReflectionManager.CreateFieldSetter(field));
                lock (_fieldCache)
                {
                    _fieldCache[field] = fieldInfo;
                }
            }
            return(fieldInfo);
        }
Ejemplo n.º 4
0
        /// <summary>
        ///     Creates a new instance of the safe method wrapper.
        /// </summary>
        /// <param name="methodInfo">Method to wrap.</param>
        public SafeMethod(MethodInfo methodInfo)
        {
            AssertUtils.ArgumentNotNull(methodInfo, "You cannot create a dynamic method for a null value.");

            _state = (SafeMethodState)_stateCache[methodInfo];
            if (_state == null)
            {
                var newState = new SafeMethodState(DynamicReflectionManager.CreateMethod(methodInfo),
                                                   new object[methodInfo.GetParameters().Length]);

                lock (_stateCache.SyncRoot)
                {
                    _state = (SafeMethodState)_stateCache[methodInfo];
                    if (_state == null)
                    {
                        _state = newState;
                        _stateCache[methodInfo] = _state;
                    }
                }
            }

            _methodInfo = methodInfo;
        }