예제 #1
0
        private Option <ILiquidValue> FromObject(Object obj)
        {
            var newHash = new LiquidHash();
            var kvps    = obj.GetType()
                          .GetProperties()
                          .Where(property => !(property.GetCustomAttributes <LiquidIgnoreAttribute>().Any() ||
                                               (property.GetCustomAttributes <LiquidIgnoreIfNullAttribute>().Any() &&
                                                ReferenceEquals(property.GetGetMethod().Invoke(obj, null), null))))
                          .Select(property => new KeyValuePair <String, Option <ILiquidValue> > (
                                      GetPropertyName(property),
                                      GetPropertyValue(obj, property)));

            foreach (var kvp in kvps)
            {
                // tolower may create a collision
                if (newHash.ContainsKey(kvp.Key))
                {
                    newHash[kvp.Key] = kvp.Value;
                }
                else
                {
                    newHash.Add(kvp);
                }
            }
            return(newHash);
        }
예제 #2
0
        private Option <ILiquidValue> CreateHash(IDictionary dict)
        {
            var newHash = new LiquidHash();

            foreach (var key in dict.Keys)
            {
                String keyAsStr = key.ToString();
                var    newValue = Convert(dict[key]); // may throw exception
                if (newHash.ContainsKey(keyAsStr))
                {
                    newHash[keyAsStr] = newValue;
                }
                else
                {
                    newHash.Add(keyAsStr, newValue);
                }
            }
            return(newHash);
        }