Exemplo n.º 1
0
        public static object DecodeMap(this MapSchema schema, JToken value)
        {
            var jobject = value as JObject;

            if (jobject != null)
            {
                var valueSchema = schema.ValueSchema;
                var valueType   = GetNativeType(valueSchema);
                var valueDict   = typeof(IDictionary <,>)
                                  .MakeGenericType(typeof(string), valueType)
                                  .GetConstructor(new Type[0])
                                  .Invoke(null);

                var magicDict = MagicMarker.GetStringDictionaryFactory(valueType).Invoke(valueDict);

                foreach (var property in jobject.Properties())
                {
                    magicDict[property.Name] = DecodeAny(valueSchema, property.Value);
                }

                return(valueDict);
            }

            throw new ArgumentException("invalid value type: " + value.GetType().FullName);
        }
Exemplo n.º 2
0
        protected override Object Call(DynamicPropertyDescriptor descriptor, Object underlying)
        {
            try
            {
                if (descriptor.HasParameters)
                {
                    return(descriptor.GetMethod().Invoke(underlying, _paramList));
                }

                var result = descriptor.GetMethod().Invoke(underlying, null);
                if (result == null)
                {
                    return(null);
                }

                if (result is DataMap)
                {
                    var resultDictionary = (DataMap)result;
                    return(resultDictionary.Get(_paramList[0]));
                }

                Func <object, DataMap> resultFactory = null;

                var resultType = result.GetType();
                if (ReferenceEquals(resultType, _lastResultType))
                {
                    resultFactory = _lastResultFactory;
                }
                else
                {
                    _lastResultFactory = resultFactory = MagicMarker.GetStringDictionaryFactory(resultType);
                    _lastResultType    = resultType;
                }

                if (resultFactory != null)
                {
                    var resultDictionary = resultFactory.Invoke(result);
                    return(resultDictionary.Get(_paramList[0]));
                }

                return(null);
            }
            catch (InvalidCastException e)
            {
                throw PropertyUtility.GetMismatchException(descriptor.GetMethod().Target, underlying, e);
            }
            catch (PropertyAccessException)
            {
                throw;
            }
            catch (Exception e)
            {
                throw new PropertyAccessException(e);
            }
        }
Exemplo n.º 3
0
        public static IDictionary <string, object> UnwrapStringDictionary(this object value)
        {
            if (value == null)
            {
                return(null);
            }

            if (value is IDictionary <string, object> )
            {
                return((IDictionary <string, object>)value);
            }

            var valueType = value.GetType();

            if (valueType.IsGenericStringDictionary())
            {
                return(MagicMarker.GetStringDictionaryFactory(valueType).Invoke(value));
            }

            if (value is IEnumerable <KeyValuePair <string, object> > )
            {
                var valueDataMap = new Dictionary <string, object>();
                foreach (var valueKeyValuePair in (IEnumerable <KeyValuePair <string, object> >)value)
                {
                    valueDataMap[valueKeyValuePair.Key] = valueKeyValuePair.Value;
                }

                return(valueDataMap);
            }

            if (value is KeyValuePair <string, object> )
            {
                var valueDataMap      = new Dictionary <string, object>();
                var valueKeyValuePair = (KeyValuePair <string, object>)value;
                valueDataMap[valueKeyValuePair.Key] = valueKeyValuePair.Value;
                return(valueDataMap);
            }

            // use this sparingly since its more expensive... we may need to write
            // a more generalized method if this becomes commonplace.

            var dictType = valueType.FindGenericInterface(typeof(IDictionary <,>));

            if (dictType != null)
            {
                var magicDictionary = MagicMarker.GetDictionaryFactory(valueType).Invoke(value);
                return(magicDictionary.Transform(
                           ki => Convert.ToString(ki),
                           ke => ke));
            }

            throw new ArgumentException("unable to convert input to string dictionary");
        }
Exemplo n.º 4
0
        public static IDictionary <string, object> UnwrapDictionary(this object value)
        {
            var valueDataMap = value as IDictionary <string, object>;

            if (valueDataMap == null)
            {
                var valueType = value.GetType();
                if (valueType.IsGenericStringDictionary())
                {
                    valueDataMap = MagicMarker.GetStringDictionaryFactory(valueType).Invoke(value);
                }
                else
                {
                    throw new ArgumentException("unable to convert input to string dictionary");
                }
            }

            return(valueDataMap);
        }
Exemplo n.º 5
0
        public static IDictionary <string, object> UnwrapDictionary(this object value)
        {
            if (value == null)
            {
                return(null);
            }
            if (value is IDictionary <string, object> )
            {
                return((IDictionary <string, object>)value);
            }

            var valueType = value.GetType();

            if (valueType.IsGenericStringDictionary())
            {
                return(MagicMarker.GetStringDictionaryFactory(valueType).Invoke(value));
            }

            if (value is IEnumerable <KeyValuePair <string, object> > )
            {
                var valueDataMap = new Dictionary <string, object>();
                foreach (var valueKeyValuePair in ((IEnumerable <KeyValuePair <string, object> >)value))
                {
                    valueDataMap[valueKeyValuePair.Key] = valueKeyValuePair.Value;
                }

                return(valueDataMap);
            }

            if (value is KeyValuePair <string, object> )
            {
                var valueDataMap      = new Dictionary <string, object>();
                var valueKeyValuePair = (KeyValuePair <string, object>)value;
                valueDataMap[valueKeyValuePair.Key] = valueKeyValuePair.Value;
                return(valueDataMap);
            }

            throw new ArgumentException("unable to convert input to string dictionary");
        }