Exemplo n.º 1
0
        private static TypeMetaData GetTypeMetaData(Type itemType)
        {
            TypeMetaData typeMetaData;
            typeMetaDataCache.TryGetValue(itemType.FullName, out typeMetaData);

            //If a cache read miss occured, build a TypeMetaData object for the type and add to the cashe
            if (typeMetaData == null)
            {
                typeMetaData = new TypeMetaData(itemType);

                AddToTypeMetaDataCache(itemType.FullName, typeMetaData);
            }
            return typeMetaData;
        }
Exemplo n.º 2
0
        /// <summary>
        /// Adds an item to TypeMetaDataCache in a non-locking way but still thread-safe.
        /// </summary>
        /// <param name="key"></param>
        private static void AddToTypeMetaDataCache(string key, TypeMetaData typeMetaData)
        {
            Dictionary<string, TypeMetaData> snapshot, newCache;

            do
            {
                snapshot = typeMetaDataCache;
                newCache = new Dictionary<string, TypeMetaData>(typeMetaDataCache);
                newCache[key] = typeMetaData;
                //If the Interlocked.CompareExchange fails, it means we lost the cache update race with another thread, 
                //therefore just try again.
            } while (!ReferenceEquals(
                Interlocked.CompareExchange(ref typeMetaDataCache, newCache, snapshot),
                snapshot));
        }
Exemplo n.º 3
0
 public bool CanHandle(TypeMetaData type)
 {
     return !type.IsCollection;
 }
Exemplo n.º 4
0
 public bool CanHandle(TypeMetaData type)
 {
     return type.IsDictionary;
 }
Exemplo n.º 5
0
            public object Handle(object item, TypeMetaData type, Func<object, Hashtable, object> filter, Hashtable visitedObjects)
            {
                if (visitedObjects.ContainsKey(item)) return visitedObjects[item];
                visitedObjects[item] = item;

                // need to go through each property on the object and see if the value needs to be filtered
                foreach (var field in type.FieldAccessors)
                {
                    var value = field.GetAccessor(item);

                    if (value == null)
                        continue;

                    var filteredValue = filter(value, visitedObjects);

                    try
                    {
                        field.SetAccessor(item, filteredValue);
                    }
                    catch (InvalidCastException e)
                    {
                        throw new InvalidCastException(string.Format("{0} was able to filter type {1} but failed to assign the filtered data back to the object", typeof(StudentFilter).Name, type.FullName), e);
                    }
                }

                return item;
            }
Exemplo n.º 6
0
 public bool CanHandle(TypeMetaData type)
 {
     return type.IsQueryable;
 }
Exemplo n.º 7
0
            public object Handle(object item, TypeMetaData type, Func<object, Hashtable, object> filter, Hashtable visitedObjects)
            {
                var dictionary = (IDictionary)item;

                //Add the items that user is not allowed to view to the black list
                var blackList = new HashSet<object>();
                foreach (var key in dictionary.Keys)
                {
                    var filterKey = filter(key, visitedObjects);
                    if (filterKey == null)
                        blackList.Add(key);
                    else
                    {
                        var value = dictionary[key];
                        var filterValue = filter(value, visitedObjects);
                        if (filterValue == null)
                         blackList.Add(key);
                    }
                }

                //Remove the items that are in the black list
                foreach (var key in blackList)
                {
                    dictionary.Remove(key);
                }

                return item;
            }
Exemplo n.º 8
0
 public bool CanHandle(TypeMetaData type)
 {
     return type.IsArray;
 }
Exemplo n.º 9
0
 public object Handle(object item, TypeMetaData type, Func<object, Hashtable, object> filter, Hashtable visitedObjects)
 {
     // Convert item to a list for processing, and refilter it
     return filter(type.ToListMethod.Invoke(null, new[] { item }), visitedObjects);
 }
Exemplo n.º 10
0
 public bool CanHandle(TypeMetaData type)
 {
     return type.IsHashtable;
 }
Exemplo n.º 11
0
            public object Handle(object item, TypeMetaData type, Func<object, Hashtable, object> filter, Hashtable visitedObjects)
            {
                //Create a list and add all the items user have access to.
                var filteredList = (IList)Activator.CreateInstance(type.ListType);

                foreach (var arrayItem in (IEnumerable<object>)item)
                {
                    var filtered = filter(arrayItem, visitedObjects);

                    if (filtered != null)
                        filteredList.Add(filtered);
                }

                //Convert the list to array so that we can assign it back to the field
                return type.ListTypeToArrayMethod.Invoke(filteredList, null);
            }
Exemplo n.º 12
0
            public object Handle(object item, TypeMetaData type, Func<object, Hashtable, object> filter, Hashtable visitedObjects)
            {
                var hashTable = (Hashtable)item;

                //Add the items that user is not allowed to view to the black list
                var blackList = new Hashtable();
                foreach (var key in hashTable.Keys)
                {
                    var filterKey = filter(key, visitedObjects);
                    if (filterKey == null)
                        blackList[key] = "";
                    else
                    {
                        var value = hashTable[key];
                        var filterValue = filter(value, visitedObjects);
                        if (filterValue == null)
                            blackList[key] = value;
                    }
                }

                //Remove the items that are in the black list
                foreach (var key in blackList)
                {
                    hashTable.Remove(key);
                }

                return item;
            }
Exemplo n.º 13
0
 public bool CanHandle(TypeMetaData type)
 {
     return type.IsSet;
 }
Exemplo n.º 14
0
            public object Handle(object item, TypeMetaData type, Func<object, Hashtable, object> filter, Hashtable visitedObjects)
            {
                var blackList = (IList)Activator.CreateInstance(type.ListType);

                //Add the items that user is not allowed to view to the black list
                foreach (var setItem in (IEnumerable<object>)item)
                {
                    var filteredItem = filter(setItem, visitedObjects);

                    if (filteredItem == null)
                        blackList.Add(setItem);
                }

                //Remove the items that are in the black list
                if (blackList.Count > 0)
                    type.ExceptWithMethod(item, new[] {blackList});

                return item;
            }
Exemplo n.º 15
0
 public bool CanHandle(TypeMetaData type)
 {
     return type.ImplementsIList && !type.IsArray;
 }
Exemplo n.º 16
0
            public object Handle(object item, TypeMetaData type, Func<object, Hashtable, object> filter, Hashtable visitedObjects)
            {
                var list = (IList)item  ;
               
                for (int i = list.Count - 1; i >= 0; i--)
                {
                    var listItemFiltered = filter(list[i], visitedObjects);
                    if (listItemFiltered == null)
                        list.RemoveAt(i);
                }

                return list;
            }