Beispiel #1
0
 /// <summary>
 /// Mapping in all tree
 /// </summary>
 /// <typeparam name="T">destination type</typeparam>
 /// <param name="obj">destination</param>
 /// <param name="map">transform function</param>
 /// <param name="names">selected names</param>
 public void Mapping(IMarshalling obj, Func <MarshallingType, IMarshalling, IMarshalling, IMarshalling> map, params string[] names)
 {
     foreach (string s in names)
     {
         var content = this.Data[s];
         Conversion(s, content.GetType(), obj, content, map);
     }
 }
Beispiel #2
0
 /// <summary>
 /// Extract a name from a source
 /// </summary>
 /// <param name="source">source</param>
 /// <param name="name">name to find</param>
 /// <returns>null if not exists</returns>
 public dynamic Extract(IMarshalling source, string name)
 {
     if (source is MarshallingHash)
     {
         return(source.Values.First(x => x.Name == name));
     }
     else if (source is MarshallingList)
     {
         return(source.Values.ElementAt(Convert.ToInt32(name)));
     }
     else
     {
         return(source.Value);
     }
 }
 /// <summary>
 /// Mapping in all tree
 /// </summary>
 /// <typeparam name="T">destination type</typeparam>
 /// <param name="obj">destination</param>
 /// <param name="map">transform function</param>
 /// <param name="names">selected names</param>
 public void Mapping(IMarshalling obj, Func <MarshallingType, IMarshalling, IMarshalling, IMarshalling> map, params string[] names)
 {
     if (this.Name == names[0])
     {
         this.Mapping(obj, map, names.Skip(1).ToArray());
     }
     else
     {
         foreach (string s in names)
         {
             var content = this.Data[s];
             Conversion(s, content.GetType(), obj, content, map);
         }
     }
 }
        /// <summary>
        /// Select a specific element in tree
        /// </summary>
        /// <param name="sequence">tree sequence</param>
        /// <returns>resulting object</returns>
        public IMarshalling Extract(IMarshalling source, string name, params string[] sequence)
        {
            dynamic content = Extract(source, name);

            foreach (string s in sequence)
            {
                dynamic subContent = Extract(content, name);
                if (subContent != null)
                {
                    content = subContent;
                }
                else
                {
                    new ArgumentException(String.Format("Key {0} is not found", s));
                }
            }
            return(content);
        }
 /// <summary>
 /// Extract a name from a source
 /// </summary>
 /// <param name="source">source</param>
 /// <param name="name">name to find</param>
 /// <returns>null if not exists</returns>
 public dynamic Extract(IMarshalling source, string name)
 {
     if (source.Name == name)
     {
         return(source);
     }
     else if (source is MarshallingHash)
     {
         return(source.Entries.First(x => x.Key == name));
     }
     else if (source is MarshallingList)
     {
         return(source.Values.First(x => x.Name == name));
     }
     else
     {
         return(source.Value);
     }
 }
 public OutboundMessage()
 {
     _marshalling = ApplicationRoot.Context.Resolve <IMarshalling>();
 }
Beispiel #7
0
 public OutboundMessage(IMarshalling marshalling)
 {
     _marshalling = marshalling;
 }
Beispiel #8
0
 /// <summary>
 /// Mapping in all tree
 /// </summary>
 /// <typeparam name="T">destination type</typeparam>
 /// <param name="obj">destination</param>
 /// <param name="map">transform function</param>
 public void Mapping(IMarshalling obj, Func <MarshallingType, IMarshalling, IMarshalling, IMarshalling> map)
 {
     Mapping(obj, map, (this as IMarshalling).HashKeys.ToArray());
 }
Beispiel #9
0
        /// <summary>
        /// Conversion of an object as a such type
        /// </summary>
        /// <typeparam name="T">destination object type</typeparam>
        /// <param name="name">name</param>
        /// <param name="t">type of input</param>
        /// <param name="obj">destination object</param>
        /// <param name="input">input object</param>
        /// <param name="transform">transform function</param>
        /// <returns>new content</returns>
        public void Conversion(string name, Type t, IMarshalling obj, IMarshalling input, Func <MarshallingType, IMarshalling, IMarshalling, IMarshalling> transform)
        {
            Dictionary <Type, Action <IMarshalling> > SWITCH = new Dictionary <Type, Action <IMarshalling> >()
            {
                {
                    typeof(MarshallingBoolValue), x => {
                        obj.Set(name, transform(MarshallingType.VALUE, obj, x));
                    }
                },
                {
                    typeof(MarshallingDoubleValue), x => {
                        obj.Set(name, transform(MarshallingType.VALUE, obj, x));
                    }
                },
                {
                    typeof(MarshallingHash), x => {
                        IMarshalling subContent = transform(MarshallingType.HASH, obj, x);
                        obj.Set(name, subContent);
                        x.Mapping(subContent, transform);
                    }
                },
                {
                    typeof(MarshallingEnumerationValue), x => {
                        obj.Set(name, transform(MarshallingType.VALUE, obj, x));
                    }
                },
                {
                    typeof(MarshallingIntValue), x => {
                        obj.Set(name, transform(MarshallingType.VALUE, obj, x));
                    }
                },
                {
                    typeof(MarshallingList), x => {
                        IMarshalling subContent = transform(MarshallingType.LIST, obj, x);
                        obj.Set(name, subContent);
                        x.Mapping(subContent, transform);
                    }
                },
                {
                    typeof(MarshallingObjectValue), x => {
                        obj.Set(name, transform(MarshallingType.VALUE, obj, x));
                    }
                },
                {
                    typeof(MarshallingRegexValue), x => {
                        obj.Set(name, transform(MarshallingType.VALUE, obj, x));
                    }
                }
            };
            bool found = false;

            foreach (KeyValuePair <Type, Action <IMarshalling> > kv in SWITCH)
            {
                if (kv.Key.IsEquivalentTo(input.GetType()))
                {
                    found = true;
                    kv.Value(input);
                }
            }
            if (!found)
            {
                throw new KeyNotFoundException(String.Format("Type {0} not found", input.GetType().Name));
            }
        }
Beispiel #10
0
 /// <summary>
 /// Implements bindings
 /// </summary>
 /// <param name="m">object to bind</param>
 public virtual void Bind(IMarshalling m)
 {
     throw new NotImplementedException();
 }
 /// <summary>
 /// Add a new element into list
 /// </summary>
 /// <param name="name">name</param>
 /// <param name="d">element</param>
 public virtual void Add(IMarshalling d)
 {
     throw new NotSupportedException();
 }