private static PropertyInfoCache GetPropertyInfoCache(PropertyInfo propertyInfo)
        {
            // null check
            if (propertyInfo == null)
            {
                throw new ArgumentNullException("propertyInfo");
            }
            // for thread safe
            var cacheArray = _propertyInfoCacheArray;
            // get cache by index
            var cacheIndex = ObjectHeaderAccessor.GetIndex(propertyInfo);

            if (cacheIndex > 0 && cacheIndex < cacheArray.Length)
            {
                var cached = cacheArray[cacheIndex];
                if (cached != null)
                {
                    // cache array may replaced by other thread,
                    // it so we need create the cache entry again
                    return(cached);
                }
            }
            // get a new cache index
            if (cacheIndex == 0)
            {
                if (_propertyInfoCacheIndex > ObjectHeaderAccessor.MaxIndex)
                {
                    return(null);
                }
                cacheIndex = Interlocked.Increment(ref _propertyInfoCacheIndex);
                if (cacheIndex > ObjectHeaderAccessor.MaxIndex)
                {
                    return(null);
                }
            }
            // create new cache array if size not enough
            if (cacheIndex >= cacheArray.Length)
            {
                var newCacheArray = new PropertyInfoCache[
                    Math.Min(
                        Math.Max(cacheArray.Length * 2, cacheIndex + 1),
                        ObjectHeaderAccessor.MaxIndex + 1)];
                Array.Copy(cacheArray, newCacheArray, cacheArray.Length);
                cacheArray = newCacheArray;
                _propertyInfoCacheArray = newCacheArray;
            }
            // create cache entry
            var cache = new PropertyInfoCache(propertyInfo);

            cacheArray[cacheIndex] = cache;
            ObjectHeaderAccessor.SetIndex(propertyInfo, cacheIndex);
            return(cache);
        }
コード例 #2
0
        // ========================================
        // constructor
        // ========================================
        private TypeService()
        {
            _attrCache          = new AttributeCache();
            _propInfoCache      = new PropertyInfoCache();
            _accessorCache      = new AccessorCache();
            _actionInvokerCache = new ActionInvokerCache();

            _nameToType                   = new Dictionary <string, Type>();
            _stringConvertiblities        = new Dictionary <Type, bool>();
            _addMethodNameToMethod        = new Dictionary <string, MethodInfo>();
            _nameToPropInfo               = new Dictionary <string, PropertyInfo>();
            _typeToPublicInstancePropInfo = new Dictionary <Type, PropertyInfo[]>();
        }
コード例 #3
0
        public BaseEntityt(object instance)
        {
            _instance = instance;
            var entityType = instance != null && instance is IEnumerable <object>
                             ?instance.GetType().GetGenericArguments()[0]
                             : instance.GetType();

            PropertyInfoCache = entityType.GetProperties().ToDictionary(p => p.Name);

            EnumeratedPropertyCache =
                PropertyInfoCache.Select(p =>
                                         new EnumeratedProperty(p.Key,
                                                                instance == null ?
                                                                null :
                                                                p.Value.GetValue(instance, null)));

            _properties = GetPropertiesWithEditorsTypes(entityType);
        }
コード例 #4
0
        private static void SetPropertyValue(object instance, object value, Queue <string> names)
        {
            if (names.Count == 0)
            {
                return;
            }

            var properties = PropertyInfoCache.GetPropertyInfoCache(instance.GetType());

            string firstName = names.Peek();

            var prop = properties.FirstOrDefault(p => p.Name.Equals(firstName, StringComparison.OrdinalIgnoreCase));

            if (prop == null)
            {
                return;
            }

            if (names.Count == 1)
            {
                if (prop.PropertyType.IsPrimitive() || prop.PropertyType.IsNullable())
                {
                    prop.SetValue(instance, value);

                    return;
                }
                else
                {
                    return;
                }
            }

            if (prop.GetValue(instance) == null)
            {
                var newInstance = GetInstance(prop.PropertyType);
                prop.SetValue(instance, newInstance);
            }

            names.Dequeue();

            SetPropertyValue(prop.GetValue(instance), value, names);
        }
コード例 #5
0
        private object GetPropertyValueRec(object obj, string head, IEnumerable <string> tail)
        {
            var pi = PropertyInfoCache.GetPropertyInfoCache(obj.GetType()).FirstOrDefault(p => p.Name.Equals(head, StringComparison.OrdinalIgnoreCase));

            if (pi == null)
            {
                throw new MissingFieldException(head);
            }

            object innerObj = pi.GetValue(obj, null);

            if (innerObj == null || !tail.Any())
            {
                return(innerObj);
            }
            else
            {
                return(GetPropertyValueRec(innerObj, tail.First(), tail.Skip(1)));
            }
        }
コード例 #6
0
 public static PropertyInfo Property(this Type type, string name, params Type[] parameters)
 {
     return(PropertyInfoCache.ForType(type).GetIndexed(name, parameters));
 }
コード例 #7
0
 public static PropertyInfo Property(this Type type, string name)
 {
     return(PropertyInfoCache.ForType(type).First(name));
 }
コード例 #8
0
 public static PropertyInfo[] Properties(this Type type, string name)
 {
     return(PropertyInfoCache.ForType(type).Get(name));
 }
コード例 #9
0
 public static PropertyInfo[] Properties(this Type type)
 {
     return(PropertyInfoCache.ForType(type).Properties);
 }
コード例 #10
0
        public static PropertyInfo Indexer(this Type type, params Type[] parameters)
        {
            var cache = PropertyInfoCache.ForType(type);

            return(cache.IndexerName == null ? null : cache.GetIndexed(cache.IndexerName, parameters));
        }