public int expand(object o, OnChildNode listener)
        {
            if (o == null) { return 0; }
            if (o.GetType() == (typeof(DateTime)))
            {
                Int64 ewokTime = NodeExpanderConstants.extractEpochTimeMillis((DateTime)o);
                listener(o, NodeExpanderConstants.unixEpochTimeMillisFieldName, ewokTime);
                return 1;
            }

            Type theclass = o.GetType();
            FieldInfo[] fis = theclass.GetFields(BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Default);
            foreach (FieldInfo fi in fis)
            {
                // ignore property backing fields!
                if (isPropertyAutoField(fi))
                {
                    continue;
                }
                Object value = fi.GetValue(o);
                if (fi.FieldType.IsEnum)
                {
                    value = Enum.GetName(fi.FieldType, value);
                }

                listener(o, fi.Name, value);
            }
            return fis.Length;
        }
        public int expand(object o, OnChildNode listener)
        {
            if (o == null)
            {
                return(0);
            }
            if (o.GetType() == (typeof(DateTime)))
            {
                Int64 ewokTime = NodeExpanderConstants.extractEpochTimeMillis((DateTime)o);
                listener(o, NodeExpanderConstants.unixEpochTimeMillisFieldName, ewokTime);
                return(1);
            }

            Type theclass = o.GetType();

            FieldInfo[] fis = theclass.GetFields(BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Default);
            foreach (FieldInfo fi in fis)
            {
                // ignore property backing fields!
                if (isPropertyAutoField(fi))
                {
                    continue;
                }
                Object value = fi.GetValue(o);
                if (fi.FieldType.IsEnum)
                {
                    value = Enum.GetName(fi.FieldType, value);
                }

                listener(o, fi.Name, value);
            }
            return(fis.Length);
        }
        public int expand(object o, OnChildNode listener)
        {
            if (o == null) { return 0; }
            Type theclass = o.GetType();
            BindingFlags bf = BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Default;
            FieldInfo[] fis = theclass.GetFields( bf );
            List<DataMember> dataMembers = new List<DataMember>();
            Dictionary<string, FieldInfo> dataMemberName2FieldInfo = new Dictionary<string, FieldInfo>();

            foreach (FieldInfo fi in fis)
            {
                 object[] customAttributes =fi.GetCustomAttributes(typeof(DataMember), true);
                if (customAttributes.Length == 0) continue;
                DataMember dm = (DataMember)customAttributes[0];

                if (dm.Name == null) dm.Name = fi.Name;
                dataMembers.Add(dm);
                dataMemberName2FieldInfo[dm.Name] = fi;
            }

            Dictionary<string, PropertyInfo> dataMemberName2PropertyInfo = new Dictionary<string, PropertyInfo>();
            PropertyInfo[] pis = theclass.GetProperties(bf);
            foreach (PropertyInfo pi in pis)
            {
                object[] customAttributes = pi.GetCustomAttributes(typeof(DataMember), true);
                if (customAttributes.Length == 0) continue;
                DataMember dm = (DataMember)customAttributes[0];

                if (dm.Name == null) dm.Name = pi.Name;
                dataMembers.Add(dm);
                dataMemberName2PropertyInfo[dm.Name] = pi;
            }

            Comparison<DataMember> sorter = (dm, dm2) =>
               {
                   if ( dm.Order == dm2.Order)  return 0;
                   else return  dm.Order>dm2.Order ? 1 : -1;
                   };
            dataMembers.Sort(sorter);
            for ( int done=0 ; done<dataMembers.Count; done++)
            {
                DataMember dm = dataMembers[done];
                Object value;
                if (dataMemberName2FieldInfo.ContainsKey(dm.Name))
                {
                    FieldInfo fi = dataMemberName2FieldInfo[dm.Name];
                    value = fi.GetValue(o);
                }
                else
                {
                    PropertyInfo pi = dataMemberName2PropertyInfo[dm.Name];
                    value = pi.GetValue(o);
                }
                listener(o, dm.Name, value);
            }

            return dataMembers.Count;
        }
Esempio n. 4
0
        public void simpleExpand(NodeExpander nodeExpander, string propname)
        {
            Data        data    = new Data();
            bool        foundhi = false;
            OnChildNode l       =
                delegate(Object from, String name, Object to)
            {
                if (name.Equals(propname) && to.Equals(data.Greeting))
                {
                    foundhi = true;
                }
            };

            nodeExpander.expand(data, l);
            Assert.AreEqual(true, foundhi, "expected to find greeting");
        }
Esempio n. 5
0
        public int expand(object o, OnChildNode onChildNode)
        {
            //treat dates differently !
            if (o.GetType() == (typeof(DateTime)))
            {
                if (o.GetType() == (typeof(DateTime)))
                {
                    Int64 ewokTime = NodeExpanderConstants.extractEpochTimeMillis((DateTime)o);
                    onChildNode(o, NodeExpanderConstants.unixEpochTimeMillisPropertyName, ewokTime);
                    return(1);
                }
            }
            PropertyInfo [] pis          = o.GetType().GetProperties();
            int             setAbleCount = 0;

            foreach (PropertyInfo pi in pis)
            {
                if (ExcludeReadOnlyProperties && pi.GetSetMethod() == null)
                {
                    continue;
                }
                Object value = null;
                try
                {
                    value = pi.GetValue(o);
                    if (pi.PropertyType.IsEnum)
                    {
                        value = Enum.GetName(pi.PropertyType, value);
                    }
                    setAbleCount++;
                }
                catch (Exception ex)
                {
                    Exception exx = new Exception("failed to get value for property " + pi.Name + " declared in " + pi.DeclaringType, ex);
                    throw exx;
                }

                String name = Char.ToLower(pi.Name[0]) + pi.Name.Substring(1);
                name = pi.Name;
                onChildNode(o, name, value);
            }
            return(setAbleCount);
        }
        public int expand(object o, OnChildNode onChildNode)
        {
            //treat dates differently !
            if (o.GetType() == (typeof(DateTime)))
            {
                if (o.GetType() == (typeof(DateTime)))
                {
                    Int64 ewokTime = NodeExpanderConstants.extractEpochTimeMillis((DateTime)o);
                    onChildNode(o, NodeExpanderConstants.unixEpochTimeMillisPropertyName, ewokTime);
                    return 1;
                }
            }
            PropertyInfo []pis= o.GetType().GetProperties();
            int setAbleCount = 0;
            foreach (PropertyInfo pi in pis)
            {
                if (ExcludeReadOnlyProperties && pi.GetSetMethod() == null) { continue; }
                Object value = null;
                try
                {
                    value = pi.GetValue(o);
                    if (pi.PropertyType.IsEnum)
                    {
                        value = Enum.GetName(pi.PropertyType, value);
                    }
                    setAbleCount++;
                }
                catch (Exception ex)
                {
                    Exception exx = new Exception("failed to get value for property " + pi.Name + " declared in "+ pi.DeclaringType, ex);
                    throw exx;
                }

                String name = Char.ToLower(pi.Name[0]) + pi.Name.Substring(1);
                name = pi.Name;
                onChildNode(o, name, value);
            }
            return setAbleCount;
        }
Esempio n. 7
0
        public int expand(object o, OnChildNode listener)
        {
            if (o == null)
            {
                return(0);
            }
            Type         theclass = o.GetType();
            BindingFlags bf       = BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Default;

            FieldInfo[]       fis         = theclass.GetFields(bf);
            List <DataMember> dataMembers = new List <DataMember>();
            Dictionary <string, FieldInfo> dataMemberName2FieldInfo = new Dictionary <string, FieldInfo>();

            foreach (FieldInfo fi in fis)
            {
                object[] customAttributes = fi.GetCustomAttributes(typeof(DataMember), true);
                if (customAttributes.Length == 0)
                {
                    continue;
                }
                DataMember dm = (DataMember)customAttributes[0];

                if (dm.Name == null)
                {
                    dm.Name = fi.Name;
                }
                dataMembers.Add(dm);
                dataMemberName2FieldInfo[dm.Name] = fi;
            }

            Dictionary <string, PropertyInfo> dataMemberName2PropertyInfo = new Dictionary <string, PropertyInfo>();

            PropertyInfo[] pis = theclass.GetProperties(bf);
            foreach (PropertyInfo pi in pis)
            {
                object[] customAttributes = pi.GetCustomAttributes(typeof(DataMember), true);
                if (customAttributes.Length == 0)
                {
                    continue;
                }
                DataMember dm = (DataMember)customAttributes[0];

                if (dm.Name == null)
                {
                    dm.Name = pi.Name;
                }
                dataMembers.Add(dm);
                dataMemberName2PropertyInfo[dm.Name] = pi;
            }

            Comparison <DataMember> sorter = (dm, dm2) =>
            {
                if (dm.Order == dm2.Order)
                {
                    return(0);
                }
                else
                {
                    return(dm.Order > dm2.Order ? 1 : -1);
                }
            };

            dataMembers.Sort(sorter);
            for (int done = 0; done < dataMembers.Count; done++)
            {
                DataMember dm = dataMembers[done];
                Object     value;
                if (dataMemberName2FieldInfo.ContainsKey(dm.Name))
                {
                    FieldInfo fi = dataMemberName2FieldInfo[dm.Name];
                    value = fi.GetValue(o);
                }
                else
                {
                    PropertyInfo pi = dataMemberName2PropertyInfo[dm.Name];
                    value = pi.GetValue(o);
                }
                listener(o, dm.Name, value);
            }

            return(dataMembers.Count);
        }
        /** carries out exploration
         * if o is a leaf leaf is called
         * if o is a standard object down is called the item is expanded anf then up is called
         * if o is a dictionary or indexed object (e.g. array) every contained object is treated as a
         * leaf or standard object
         *
         * determination of leaf is in isLeaf and is based on c# properties
         *
         * determination if indexed type is in getEnumeratorIfIndexedType
         *
         * determination of map type is in toDictionary
         **/
        public void explore(object o, MoveAway down, MoveBack up, OnLeaf leaf)
        {
            if (NodeExpander == null)
            {
                throw new Exception("explore is not possible unless a node expander is specified");
            }
            OnChildNode onChildNode = null;

            onChildNode = delegate(Object from, String name, Object to)
            {
                IEnumerator           en           = null;
                Type                  toType       = to == null?null:to.GetType();
                IDictionaryNonGeneric asDictionary = null;
                if (isLeaf(to, toType))
                {
                    leaf(from, name, to);
                }
                else if (null != (asDictionary = toDictionary(to, toType)))
                {
                    bool doExpand =
                        down(from, name, to, false);
                    en = asDictionary.Keys.GetEnumerator();
                    for (int done = 0; doExpand && en.MoveNext(); done++)
                    {
                        Object oKey     = en.Current;
                        string strKey   = dictionaryKey2String(oKey);
                        Object oVal     = asDictionary[oKey];
                        Type   oValType = oVal == null ? null : oVal.GetType();
                        if (isLeaf(oVal, oValType))
                        {
                            leaf(to, strKey, oVal);
                        }
                        else
                        {
                            bool doSubExpand = down(to, strKey, oVal, false, done);
                            if (doSubExpand)
                            {
                                NodeExpander.expand(oVal, onChildNode);
                            }
                            up(from, strKey, oVal, false);
                        }
                    }
                    up(from, name, to, false);
                }
                else if (null != (en = getEnumeratorIfIndexedType(to)))
                {
                    bool doExpand =
                        down(from, name, to, true);

                    for (int done = 0; doExpand && en.MoveNext(); done++)
                    {
                        Object oVal = en.Current;
                        if (isLeaf(oVal, oVal == null?null:oVal.GetType()))
                        {
                            leaf(to, null, oVal);
                        }
                        else
                        {
                            bool doSubExpand = down(null, null, oVal, false, done);
                            if (doSubExpand)
                            {
                                NodeExpander.expand(oVal, onChildNode);
                            }
                            up(null, null, oVal, false);
                        }
                    }
                    up(from, name, to, true);
                }
                else
                {
                    bool doExpand = down(from, name, to, false);
                    if (doExpand)
                    {
                        NodeExpander.expand(to, onChildNode);
                    }
                    up(from, name, to, false);
                }
            };
            onChildNode(null, null, o);
        }