Esempio n. 1
0
        public static FastProperty GetProperty(
            PropertyInfo propertyInfo,
            PropertyCachingStrategy cachingStrategy = PropertyCachingStrategy.Cached)
        {
            Guard.NotNull(propertyInfo, nameof(propertyInfo));

            FastProperty fastProperty = null;

            if (TryGetCachedProperty(propertyInfo.ReflectedType, propertyInfo.Name, cachingStrategy == PropertyCachingStrategy.EagerCached, out fastProperty))
            {
                return(fastProperty);
            }

            var key = new PropertyKey(propertyInfo.ReflectedType, propertyInfo.Name);

            if (!_singlePropertiesCache.TryGetValue(key, out fastProperty))
            {
                fastProperty = CreateInstance(propertyInfo);
                if (cachingStrategy > PropertyCachingStrategy.Uncached)
                {
                    _singlePropertiesCache.TryAdd(key, fastProperty);
                }
            }

            return(fastProperty);
        }
Esempio n. 2
0
        public void SetValue(object target, object value)
        {
            if (_inner != null)
            {
                _inner.SetValue(target, value);
            }

            if (_setter == null)
            {
                try
                {
                    var fastProp = FastProperty.GetProperty(_pi, PropertyCachingStrategy.EagerCached);
                    if (fastProp.IsPublicSettable)
                    {
                        _setter = fastProp.ValueSetter;
                    }
                    if (_setter == null)
                    {
                        throw new Exception();
                    }
                }
                catch
                {
                    CreateInnerProvider();
                    _inner.SetValue(target, value);
                    return;
                }
            }

            _setter(target, value);
        }
Esempio n. 3
0
        public static FastProperty GetProperty(
            Type type,
            string propertyName,
            PropertyCachingStrategy cachingStrategy = PropertyCachingStrategy.Cached)
        {
            Guard.NotNull(type, nameof(type));
            Guard.NotEmpty(propertyName, nameof(propertyName));

            FastProperty fastProperty = null;

            if (TryGetCachedProperty(type, propertyName, cachingStrategy == PropertyCachingStrategy.EagerCached, out fastProperty))
            {
                return(fastProperty);
            }

            var key = new PropertyKey(type, propertyName);

            if (!_singlePropertiesCache.TryGetValue(key, out fastProperty))
            {
                var pi = type.GetProperty(propertyName, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.Instance | BindingFlags.IgnoreCase);
                if (pi != null)
                {
                    fastProperty = CreateInstance(pi);
                    if (cachingStrategy > PropertyCachingStrategy.Uncached)
                    {
                        _singlePropertiesCache.TryAdd(key, fastProperty);
                    }
                }
            }

            return(fastProperty);
        }
Esempio n. 4
0
 private static FastProperty[] GetFastPropertiesFor(Type type)
 {
     return FastProperty.GetProperties(type, PropertyCachingStrategy.Uncached)
         .Values
         .Where(pi => pi.IsPublicSettable)
         .ToArray();
 }
Esempio n. 5
0
        public object GetValue(object target)
        {
            if (_inner != null)
            {
                return(_inner.GetValue(target));
            }

            if (_getter == null)
            {
                try
                {
                    _getter = FastProperty.GetProperty(_pi, PropertyCachingStrategy.EagerCached).ValueGetter;
                    if (_getter == null)
                    {
                        throw new Exception();
                    }
                }
                catch
                {
                    CreateInnerProvider();
                    return(_inner.GetValue(target));
                }
            }

            return(_getter(target));
        }
Esempio n. 6
0
        /// <summary>
        /// Reflection helper method to set a property value
        /// </summary>
        /// <param name="instance"></param>
        /// <param name="name"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        protected bool SetProperty(object instance, string name, object value)
        {
            var fastProp = _instanceType != null?FastProperty.GetProperty(_instanceType, name, PropertyCachingStrategy.EagerCached) : null;

            if (fastProp != null)
            {
                fastProp.SetValue(instance ?? this, value);
                return(true);
            }

            return(false);
        }
Esempio n. 7
0
        /// <summary>
        /// Checks whether a property exists in the Property collection
        /// or as a property on the instance
        /// </summary>
        /// <param name="propertyName"></param>
        /// <param name="includeInstanceProperties"></param>
        /// <returns></returns>
        public bool Contains(string propertyName, bool includeInstanceProperties = false)
        {
            if (Properties.ContainsKey(propertyName))
            {
                return(true);
            }

            if (includeInstanceProperties && _instance != null)
            {
                return(FastProperty.GetProperties(_instance).ContainsKey(propertyName));
            }

            return(false);
        }
Esempio n. 8
0
        public static void Map <TFrom, TTo>(TFrom from, TTo to, CultureInfo culture = null)
            where TFrom : class
            where TTo : class
        {
            Guard.NotNull(from, nameof(from));
            Guard.NotNull(to, nameof(to));

            if (object.ReferenceEquals(from, to))
            {
                // Cannot map the same instance
                return;
            }

            var fromType = from.GetType();
            var toType   = to.GetType();

            ValidateType(fromType);
            ValidateType(toType);

            if (culture == null)
            {
                culture = CultureInfo.CurrentCulture;
            }

            var toProps = GetFastPropertiesFor(toType).ToArray();

            foreach (var toProp in toProps)
            {
                var fromProp = FastProperty.GetProperty(fromType, toProp.Name, PropertyCachingStrategy.Uncached);
                if (fromProp == null)
                {
                    continue;
                }

                object value = null;
                try
                {
                    // Get the value from source instance and try to convert it to target props type
                    value = fromProp.GetValue(from).Convert(toProp.Property.PropertyType, culture);

                    // Set it
                    toProp.SetValue(to, value);
                }
                catch { }
            }
        }
Esempio n. 9
0
        /// <summary>
        /// Returns all properties
        /// </summary>
        /// <param name="includeInstanceProperties"></param>
        /// <returns></returns>
        public IEnumerable <KeyValuePair <string, object> > GetProperties(bool includeInstanceProperties = false)
        {
            foreach (var key in this.Properties.Keys)
            {
                yield return(new KeyValuePair <string, object>(key, this.Properties[key]));
            }

            if (includeInstanceProperties && _instance != null)
            {
                foreach (var prop in FastProperty.GetProperties(_instance).Values)
                {
                    if (!this.Properties.ContainsKey(prop.Name))
                    {
                        yield return(new KeyValuePair <string, object>(prop.Name, prop.GetValue(_instance)));
                    }
                }
            }
        }
Esempio n. 10
0
        private static bool TryGetCachedProperty(
            Type type,
            string propertyName,
            bool eagerCached,
            out FastProperty fastProperty)
        {
            fastProperty = null;
            IDictionary <string, FastProperty> allProperties;

            if (eagerCached)
            {
                allProperties = (IDictionary <string, FastProperty>)GetProperties(type);
                allProperties.TryGetValue(propertyName, out fastProperty);
            }

            if (fastProperty == null && _propertiesCache.TryGetValue(type, out allProperties))
            {
                allProperties.TryGetValue(propertyName, out fastProperty);
            }

            return(fastProperty != null);
        }
Esempio n. 11
0
        private static void MapComplex(object from, object to, Type fromType, CultureInfo culture)
        {
            if (object.ReferenceEquals(from, to))
            {
                // Cannot map same instance or null source
                return;
            }

            var toType = to.GetType();
            var isEntityMapping = typeof(BaseEntity).IsAssignableFrom(toType);
            var toProps = GetFastPropertiesFor(toType).ToArray();

            foreach (var toProp in toProps)
            {
                var fromProp = FastProperty.GetProperty(fromType, toProp.Name, PropertyCachingStrategy.Cached);
                if (fromProp == null)
                {
                    continue;
                }

                var fromPropType = fromProp.Property.PropertyType;
                var toPropType = toProp.Property.PropertyType;
                var sourceValue = fromProp.GetValue(from);

                if (isEntityMapping && (fromPropType == typeof(int) || fromPropType == typeof(int?)) && toPropType == typeof(int?) && object.Equals(0, sourceValue) && toProp.Name.EndsWith("Id"))
                {
                    // TODO: This is more a hack than a proper solution. Find a more generic way to convert int FK properties...
                    // Special mapper, that avoids DbUpdate exceptions in cases where
                    // optional (nullable) int FK properties are 0 instead of "null"
                    // after mapping model > entity.
                    toProp.SetValue(to, null);
                }
                else if (TryMap(sourceValue, fromPropType, toPropType, culture, out var targetValue))
                {
                    // Set it
                    toProp.SetValue(to, targetValue);
                }
            }
        }
Esempio n. 12
0
        private IDictionary <string, FastProperty> GetInstanceProperties()
        {
            if (_instance == null)
            {
                return(EmptyProps);
            }

            if (_instanceProps == null)
            {
                var props = FastProperty.GetProperties(_instance) as IDictionary <string, FastProperty>;

                if (_optMembers != null)
                {
                    props = props
                            .Where(x => _optMethod == MemberOptMethod.Allow ? _optMembers.Contains(x.Key) : !_optMembers.Contains(x.Key))
                            .ToDictionary(x => x.Key, x => x.Value);
                }

                _instanceProps = props;
            }

            return(_instanceProps);
        }
Esempio n. 13
0
 private static IEnumerable <FastProperty> GetFastPropertiesFor(Type type)
 {
     return(FastProperty.GetCandidateProperties(type)
            .Select(pi => FastProperty.GetProperty(pi, PropertyCachingStrategy.Uncached))
            .Where(pi => pi.IsPublicSettable));
 }