Esempio n. 1
0
        public DefContext Define(object value)
        {
            if (value == null) return null;

            var type = value.GetTypeOfObj();
            if (_simpleTypeHelper.HasSupport(type))
                return new SimpleDefContext
                    {
                        SimpleTypeDef = new SimpleTypeDef
                            {
                                Value = value
                            }
                    };

            var objectEnumerator = new ObjectEnumerator(value);
            var typeEnumerator = new TypeEnumerator();

            var complexTypeDefs = new SimpleList<ComplexTypeDef>();

            while (objectEnumerator.Continue)
            {
                var current = objectEnumerator.GetNext();
                var currentType = current.GetType();
                var currentId = objectEnumerator.Get(current);
                var typeInfo = typeEnumerator.GetTypeInfo(currentType);

                if (currentType.IsImplOf<Array>())
                {
                    #region Array

                    var array = current.CastObj<Array>();
                    var arrayItems = _complexTypeHelper
                        .GetItems(array).CastObj<ArrayItems>();

                    var items = new SimpleList<object>();
                    foreach (var item in arrayItems.Items)
                        items.Add(CheckReference(item, objectEnumerator));

                    complexTypeDefs.Add(new ArrayDef
                        {
                            Id = currentId,
                            TypeInfoId = typeInfo.Id,
                            Items = items,
                            Indicies = array.GetIndicies()
                        });

                    #endregion
                }
                else if (currentType.IsImplOf<IList>())
                {
                    #region List

                    var list = current.CastObj<IList>();
                    var listItems = _complexTypeHelper
                        .GetItems(list).CastObj<ListItems>();

                    var items = new SimpleList<object>();
                    foreach (var item in listItems.Items)
                        items.Add(CheckReference(item, objectEnumerator));

                    complexTypeDefs.Add(new ListDef
                        {
                            Id = currentId,
                            TypeInfoId = typeInfo.Id,
                            Items = items
                        });

                    #endregion
                }
                else if (currentType.IsImplOf<IDictionary>())
                {
                    #region Dictionary

                    var dictionary = current.CastObj<IDictionary>();
                    var dictionaryItems = _complexTypeHelper
                        .GetItems(dictionary).CastObj<DictionaryItems>();

                    var keys = new SimpleList<object>();
                    foreach (var key in dictionaryItems.Keys)
                        keys.Add(CheckReference(key, objectEnumerator));

                    var values = new SimpleList<object>();
                    foreach (var val in dictionaryItems.Values)
                        values.Add(CheckReference(val, objectEnumerator));

                    complexTypeDefs.Add(new DictionaryDef
                        {
                            Id = currentId,
                            TypeInfoId = typeInfo.Id,
                            Keys = keys,
                            Values = values
                        });

                    #endregion
                }
                else // user defined tpye at last
                {
                    #region UserDefined

                    var userDefinedItems = _complexTypeHelper
                        .GetItems(current).CastObj<UserDefinedItems>();

                    var names = new SimpleList<byte>();
                    foreach (var name in userDefinedItems.Names)
                        names.Add(typeInfo.PropertyNameIndex[name]);

                    var values = new SimpleList<object>();
                    foreach (var val in userDefinedItems.Values)
                        values.Add(CheckReference(val, objectEnumerator));

                    complexTypeDefs.Add(new UserDefinedDef
                        {
                            Id = currentId,
                            TypeInfoId = typeInfo.Id,
                            Names = names,
                            Values = values
                        });

                    #endregion
                }

                objectEnumerator.DefinedANewOne();
            }

            return new ComplexDefContext
                {
                    ComplexTypeDefs = complexTypeDefs,
                    TypeInfos = typeEnumerator.GetTypeInfos()
                };
        }
Esempio n. 2
0
        private object CheckReference(object value, ObjectEnumerator objectEnumerator)
        {
            if (value == null) return null;

            var itemType = value.GetTypeOfObj();
            return _simpleTypeHelper.HasSupport(itemType) ?
                value : new Reference(objectEnumerator.Check(value));
        }