예제 #1
0
        internal static void CacheReflection(Type item, DealComplexity complexity = DealComplexity.Standard)
        {
            if (_cache[complexity.ToString()].ContainsKey(item))
            {
                return;
            }

            PropertyInfo[] verified = new PropertyInfo[0];
            PropertyInfo[] prepare  = item.GetJsonProperties(complexity);
            if (prepare != null)
            {
                verified = prepare;
            }

            _cache[complexity.ToString()].Add(item, verified);
        }
예제 #2
0
        public static PropertyInfo[] GetJsonProperties <T>(DealComplexity complexity = DealComplexity.Standard)
        {
            Type   type  = typeof(T);
            string name  = type.FullName;
            string cname = "";

            if (complexity != DealComplexity.Standard)
            {
                cname = name + "_" + complexity.ToString();
            }
            else
            {
                cname = name;
            }

            if (Deck.ContainsKey(cname))
            {
                return(Deck[cname].Select(t => type.GetProperty(t)).ToArray());
            }
            else if (Deck.ContainsKey(name))
            {
                return(Deck[name].Select(t => type.GetProperty(t)).ToArray());
            }
            else
            {
                return(null);
            }
        }
예제 #3
0
        internal static IDictionary <string, object> GetBagForObject(Type type, object instance, DealComplexity complexity = DealComplexity.Standard)
        {
            CacheReflection(type, complexity);

            if (type.FullName == null)
            {
                return(null);
            }

            bool anonymous = type.FullName.Contains("__AnonymousType");

            PropertyInfo[] map = _cache[complexity.ToString()][type];

            IDictionary <string, object> bag = InitializeBag();

            foreach (PropertyInfo info in map)
            {
                if (info != null)
                {
                    var readWrite = (info.CanWrite && info.CanRead);
                    if (!readWrite && !anonymous)
                    {
                        continue;
                    }
                    object value = null;
                    try
                    {
                        value = info.GetValue(instance, null);
                    }
                    catch (Exception ex)
                    {
                        //ex.();
                    }
                    bag.Add(info.Name, value);
                }
            }

            return(bag);
        }