Ejemplo n.º 1
0
        public object Clone(Pointer _pointer, SerialAttribute _attribute, object _original, Dictionary <Pointer, string> _delayBindingTable)
        {
            // get list
            IList list = (IList)(_pointer.GetValue());

            if (list == null)
            {
                Type            fieldType       = _original.GetType();
                ConstructorInfo listConstructor = fieldType.GetConstructor(new Type[0]);
                Debug.Assert(listConstructor != null, "Connot find valid constructor for list");
                list = (IList)(listConstructor.Invoke(new object[0]));
            }
            // get value type
            Type           valueType    = list.GetType().GetGenericArguments()[0];
            ISerializeType valueIType   = Serialable.FindSuitableSerialType(valueType);
            IList          originalList = (IList)_original;

            IEnumerator it    = originalList.GetEnumerator();
            int         index = 0;

            while (it.MoveNext())
            {
                object valueObject = valueIType.Clone(new Pointer(list, index), _attribute, it.Current, _delayBindingTable);
                if (valueObject != null)
                {
                    list.Add(valueObject);
                }
                ++index;
            }
            return(list);
        }
Ejemplo n.º 2
0
        public System.Xml.XmlNode Serial(object _object, SerialAttribute _attribute, System.Xml.XmlDocument _doc, string _nameField)
        {
            if (_object != null)
            {
                IList          list       = (IList)(_object);
                Type           valueType  = _object.GetType().GetGenericArguments()[0];
                ISerializeType valueIType = Serialable.FindSuitableSerialType(valueType);

                XmlElement root = _doc.CreateElement(typeof(IList).ToString());
                root.SetAttribute("name", _nameField);
                root.SetAttribute("typeinfo", _object.GetType().ToString());

                IEnumerator it = list.GetEnumerator();
                while (it.MoveNext())
                {
//                     XmlNode valueNodeHead = _doc.CreateElement("Value");
//                     root.AppendChild(valueNodeHead);
                    XmlNode valueNode = valueIType.Serial(it.Current, _attribute, _doc, "");
                    root.AppendChild(valueNode);
                }
                return(root);
            }
            else
            {
                return(null);
            }
        }
Ejemplo n.º 3
0
        public object Unserial(Pointer _pointer, SerialAttribute _attribute, System.Xml.XmlNode _fieldNode, Dictionary <Pointer, string> _delayBindingTable)
        {
            // get list
            IList list = (IList)(_pointer.GetValue());

            if (list == null)
            {
                Type            fieldType       = Type.GetType(((XmlElement)_fieldNode).GetAttribute("typeinfo"));
                ConstructorInfo listConstructor = fieldType.GetConstructor(new Type[0]);
                Debug.Assert(listConstructor != null, "Cannot find valid constructor for list");
                list = (IList)(listConstructor.Invoke(new object[0]));
            }
            // get value type
            Type           valueType  = list.GetType().GetGenericArguments()[0];
            ISerializeType valueIType = Serialable.FindSuitableSerialType(valueType);

            int index = 0;

            foreach (XmlNode valueNode in _fieldNode.ChildNodes)
            {
                //XmlNode valueContentNode = valueNode.FirstChild;
                object valueObject = valueIType.Unserial(new Pointer(list, index), _attribute, valueNode, _delayBindingTable);
                if (valueObject != null)
                {
                    list.Add(valueObject);
                }
                ++index;
            }
            return(list);
        }
Ejemplo n.º 4
0
        /**
         * @brief core part of automate serialization
         *
         * @param _doc XmlDocument
         *
         * @result
         * */
        private XmlNode Serial(XmlDocument _doc)
        {
            // name of object
            Type       type = GetThisType();
            XmlElement root = _doc.CreateElement(type.ToString());

            FieldInfo[] fields = type.GetFields(BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public);
            foreach (FieldInfo field in fields)
            {
                // get attributes
                SerialAttribute[] attributes = (SerialAttribute[])(field.GetCustomAttributes(typeof(SerialAttribute), true));
                if (attributes.Length == 0)
                {
                    continue;
                }
                // get other
                string         fieldName   = field.Name;
                Type           fieldType   = field.FieldType;
                ISerializeType iserializer = FindSuitableSerialType(fieldType);
                if (iserializer != null)
                {
                    XmlNode node = iserializer.Serial(field.GetValue(this), attributes[0], _doc, fieldName);
                    if (node != null)
                    {
                        root.AppendChild(node);
                    }
                }
            }
            return(root);
        }
Ejemplo n.º 5
0
        public Object Clone(Pointer _pointer, SerialAttribute _attribute, object _original, Dictionary <Pointer, string> _delayBindingTable)
        {
            // get dictionary
            IDictionary dictionary = (IDictionary)(_pointer.GetValue());

            if (dictionary == null)
            {
                Type            fieldType             = _original.GetType();
                ConstructorInfo dictionaryConstructor = fieldType.GetConstructor(new Type[0]);
                Debug.Assert(dictionaryConstructor != null, "Cannot find valid constructor for dictionary");
                dictionary = (IDictionary)(dictionaryConstructor.Invoke(new object[0]));
            }
            // get key value type
            Type[]         keyValueType       = dictionary.GetType().GetGenericArguments();
            ISerializeType keyIType           = Serialable.FindSuitableSerialType(keyValueType[0]);
            ISerializeType valueIType         = Serialable.FindSuitableSerialType(keyValueType[1]);
            IDictionary    orignialDictionary = (IDictionary)_original;

            IDictionaryEnumerator it = orignialDictionary.GetEnumerator();

            while (it.MoveNext())
            {
                object keyObject   = it.Key;
                object valueObject = valueIType.Clone(new Pointer(dictionary, keyObject), _attribute, it.Value, _delayBindingTable);
                if (valueObject != null)
                {
                    dictionary.Add(keyObject, valueObject);
                }
            }
            return(dictionary);
        }
Ejemplo n.º 6
0
        public override void Initialize()
        {
            var type = typeof(T);

            _elementType      = type.GenericTypeArguments[0];
            _elementSerialize = BinarySerialize.From(_elementType);
        }
Ejemplo n.º 7
0
        public override void Initialize()
        {
            var type = typeof(T);

            _elementType      = type.GetElementType();
            _elementSerialize = BinarySerialize.From(_elementType);
        }
Ejemplo n.º 8
0
        public override void Initialize()
        {
            var type      = typeof(T);
            var underType = Enum.GetUnderlyingType(type);

            _underSerialize = BinarySerialize.From(underType);
        }
Ejemplo n.º 9
0
        public override void Initialize()
        {
            _type = typeof(T);

            _keyElementType      = _type.GetGenericArguments()[0];
            _keyElementSerialize = BinarySerialize.From(_keyElementType);

            _valueElementType      = _type.GetGenericArguments()[1];
            _valueElementSerialize = BinarySerialize.From(_valueElementType);
        }
Ejemplo n.º 10
0
        /**
         * @brief core part of automate unserialization
         *
         * @param _node the XmlNode
         * */
        private void Unserial(XmlNode _node)
        {
            Type objectType = GetThisType();

            // set property
            FieldInfo[] fields = objectType.GetFields(BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public);
            // build xml tree map
            Dictionary <string, XmlNode> xmlNodeDictionary = new Dictionary <string, XmlNode>();

            foreach (XmlNode node in _node.ChildNodes)
            {
                string name = ((XmlElement)(node)).GetAttribute("name");
                if (name != null)
                {
                    xmlNodeDictionary.Add(name, node);
                }
            }
            // scan field
            foreach (FieldInfo field in fields)
            {
                // get attributes
                SerialAttribute[] attributes = (SerialAttribute[])(field.GetCustomAttributes(typeof(SerialAttribute), true));
                if (attributes.Length == 0)
                {
                    continue;
                }
                // get other
                string fieldName = field.Name;
                Type   fieldType = field.FieldType;
                // find fields in node
                if (!xmlNodeDictionary.ContainsKey(fieldName))
                {
                    //Debug.Assert(false, "Cannot find field information in xml. Field name: " + fieldName);
                    continue;
                }
                XmlElement fieldNode = (XmlElement)(xmlNodeDictionary[fieldName]);
                string     xmlType   = fieldNode.Name;

                // find a suitable unserializor to deal with it
                ISerializeType iserializer = FindSuitableSerialType(fieldType);
                if (iserializer != null)
                {
                    Object resultObject = iserializer.Unserial(new Pointer(this, field), attributes[0], fieldNode, m_delayBindingTable);
                    if (resultObject != null)
                    {
                        field.SetValue(this, resultObject);
                    }
                }
            }
        }
Ejemplo n.º 11
0
        static private Dictionary <string, Serialable> guidTable;  // for delay binding guid -> serialable
        #endregion

        /**
         * @brief initialize serialize/unserilize solver table
         * */
        public static void InitializeSerializeTypeTable()
        {
            iserializeType = new List <ISerializeType>();
            Assembly assembly = Assembly.GetAssembly(typeof(ISerializeType));

            Type[] types = assembly.GetTypes();
            foreach (Type type in types)
            {
                if (type.GetInterface(typeof(ISerializeType).ToString()) != null)
                {
                    ConstructorInfo constructor      = type.GetConstructor(new Type[0]);
                    ISerializeType  iserializeObject = (ISerializeType)(constructor.Invoke(new object[0]));
                    iserializeType.Add(iserializeObject);
                }
            }
        }
Ejemplo n.º 12
0
        private void Clone(Serialable _object)
        {
            Type objectType = GetThisType();

            // set property
            FieldInfo[] fields = objectType.GetFields(BindingFlags.Instance |
                                                      BindingFlags.NonPublic |
                                                      BindingFlags.Public);
            // scan field
            foreach (FieldInfo field in fields)
            {
                // get attributes
                SerialAttribute[] attributes =
                    (SerialAttribute[])(field.GetCustomAttributes(typeof(SerialAttribute), true));
                if (attributes.Length == 0)
                {
                    continue;
                }
                // get other
                string fieldName = field.Name;
                Type   fieldType = field.FieldType;

                // find a suitable unserializor to deal with it
                ISerializeType iserializer = FindSuitableSerialType(fieldType);
                if (iserializer != null)
                {
                    // the field may be null
                    Object fieldObject = field.GetValue(_object);
                    if (fieldObject != null)
                    {
                        Object resultObject = iserializer.Clone(
                            new Pointer(this, field),
                            attributes[0],
                            fieldObject,
                            m_delayBindingTable);
                        if (resultObject != null)
                        {
                            field.SetValue(this, resultObject);
                        }
                    }
                }
            }
        }
Ejemplo n.º 13
0
        public object Unserial(Pointer _pointer, SerialAttribute _attribute, XmlNode _fieldNode, Dictionary <Pointer, string> _delayBindingTable)
        {
            // get dictionary
            IDictionary dictionary = (IDictionary)(_pointer.GetValue());

            if (dictionary == null)
            {
                Type            fieldType             = Type.GetType(((XmlElement)_fieldNode).GetAttribute("typeinfo"));
                ConstructorInfo dictionaryConstructor = fieldType.GetConstructor(new Type[0]);
                Debug.Assert(dictionaryConstructor != null, "Cannot find valid constructor for dictionary");
                dictionary = (IDictionary)(dictionaryConstructor.Invoke(new object[0]));
            }
            // get key value type
            Type[]         keyValueType = dictionary.GetType().GetGenericArguments();
            ISerializeType keyIType     = Serialable.FindSuitableSerialType(keyValueType[0]);
            ISerializeType valueIType   = Serialable.FindSuitableSerialType(keyValueType[1]);

            foreach (XmlNode keyValueNode in _fieldNode.ChildNodes)
            {
                // foreach keyValue
                // key
                XmlNode keyNode        = keyValueNode.SelectSingleNode("Key");
                XmlNode keyContentNode = keyNode.FirstChild;
                // do not support delay binding for key
                object keyObject = keyIType.Unserial(null, _attribute, keyContentNode, _delayBindingTable);
                // value
                XmlNode valueNode        = keyValueNode.SelectSingleNode("Value");
                XmlNode valueContentNode = valueNode.FirstChild;
                object  valueObject      = valueIType.Unserial(new Pointer(dictionary, keyObject), _attribute, valueContentNode, _delayBindingTable);
                // insert into dictionary
                if (valueObject != null)
                {
                    dictionary.Add(keyObject, valueObject);
                }
            }
            return(dictionary);
        }
Ejemplo n.º 14
0
        public XmlNode Serial(Object _object, SerialAttribute _attribute, XmlDocument _doc, string _nameField)
        {
            if (_object != null)
            {
                // get value <key, value>
                IDictionary    dictionary   = (IDictionary)(_object);
                Type[]         keyValueType = _object.GetType().GetGenericArguments();
                ISerializeType keyIType     = Serialable.FindSuitableSerialType(keyValueType[0]);
                ISerializeType valueIType   = Serialable.FindSuitableSerialType(keyValueType[1]);

                XmlElement root = _doc.CreateElement(typeof(IDictionary).ToString()); // FROM .Name
                root.SetAttribute("name", _nameField);
                root.SetAttribute("typeinfo", _object.GetType().ToString());

                IDictionaryEnumerator it = dictionary.GetEnumerator();
                while (it.MoveNext())
                {
                    XmlNode keyValueNode = _doc.CreateElement("KeyValue");
                    root.AppendChild(keyValueNode);
                    // key
                    XmlNode keyNodeHead = _doc.CreateElement("Key");
                    keyValueNode.AppendChild(keyNodeHead);
                    XmlNode keyNode = keyIType.Serial(it.Key, _attribute, _doc, "");
                    keyNodeHead.AppendChild(keyNode);
                    // value
                    XmlNode valueNodeHead = _doc.CreateElement("Value");
                    keyValueNode.AppendChild(valueNodeHead);
                    XmlNode valueNode = valueIType.Serial(it.Value, _attribute, _doc, "");
                    valueNodeHead.AppendChild(valueNode);
                }
                return(root);
            }
            else
            {
                return(null);
            }
        }
Ejemplo n.º 15
0
        private void TestIfValueIsRight(ISerializeType serialize, int size)
        {
            var fromCalculate = serialize.CalculateSize();

            Assert.AreEqual(fromCalculate, size);
        }