예제 #1
0
        private void PushTopSearchAttributeToStack(BlobFieldAttribute attrib)
        {
            ProcessState state = new ProcessState(EProcessingState.TopSearch, depth, Target);

            state.Attributes.Add(attrib);

            processStack.Push(state);
        }
예제 #2
0
        private void PushListTypeAttributesToStack(object target, BlobFieldAttribute absorb, PropertyInfo prop)
        {
            ProcessState state = new ProcessState(EProcessingState.EveryFieldSearch, depth, target);

            state.Attributes.Add(absorb);
            state.Properties.Add(prop);

            processStack.Push(state);
        }
예제 #3
0
        private void PushSubSearchAttributeToStack(ProcessState top, BlobFieldAttribute attrib, PropertyInfo prop, object target)
        {
            ProcessState state = new ProcessState(EProcessingState.SubFieldSearch, depth, target);

            state.Attributes.Add(attrib);
            state.Properties.Add(prop);

            state.WorkingType = top.WorkingType;

            processStack.Push(state);
        }
예제 #4
0
        private void BuildParserState()
        {
            Type type = typeof(T);

            BlobFieldAttribute topLevelAttribute = type.GetAttribute <BlobFieldAttribute>(TypedCache);

            if (topLevelAttribute != null)
            {
                PushTopSearchAttributeToStack(topLevelAttribute);
            }
            else
            {
                PushTypeAttributesToStack(type, Target);
            }
        }
예제 #5
0
        private void PushTypeAttributesToStack(Type t, object target)
        {
            ProcessState state = new ProcessState(EProcessingState.FieldSearch, depth, target);

            foreach (var field in t.GetCachedPropertyInfo(TypedCache))
            {
                BlobFieldAttribute fattrib = field.GetAttribute <BlobFieldAttribute>(TypedCache);

                if (fattrib == null)
                {
                    continue;
                }

                state.Attributes.Add(fattrib);
                state.Properties.Add(field);
            }

            processStack.Push(state);
        }
예제 #6
0
        private bool TestSubFieldAttributeState(ProcessState top, string fieldKey, out int index)
        {
            List <BlobFieldAttribute> attributes = top.Attributes;

            for (int i = 0; i < attributes.Count; i++)
            {
                BlobFieldAttribute attrib = attributes[i];

                if (attrib.SubFieldKey == fieldKey &&
                    (attrib.SubFieldDepth == 0 || depth - top.Depth == attrib.Depth))
                {
                    index = i;
                    return(true);
                }
            }

            index = -1;
            return(false);
        }
예제 #7
0
        public TypeSerializer(Type targetType)
        {
            var fields = new List <BlobFieldAttribute>();

            this.targetType = targetType;

            foreach (MemberInfo member in targetType.GetMembers(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance))
            {
                BlobFieldAttribute[] attribs = (BlobFieldAttribute[])member.GetCustomAttributes(typeof(BlobFieldAttribute), false);
                if (attribs.Length == 0)
                {
                    continue;
                }

                BlobFieldAttribute attrib = attribs[0];

                switch (member.MemberType)
                {
                case MemberTypes.Field:
                    attrib.Serializer = new FieldSerializer((FieldInfo)member);
                    break;

                case MemberTypes.Property:
                    attrib.Serializer = new PropertySerializer((PropertyInfo)member);
                    break;

                default:
                    throw new NotImplementedException();
                }


                fields.Add(attrib);
            }

            this.fields = fields.ToArray();
        }
예제 #8
0
        private void TypeStartField(FieldKeyType type, byte[] key, int fieldSize)
        {
            string keyValue;

            if (BlobUtil.IsIntDescriptor(key))
            {
                keyValue = Convert.ToString(BitConverter.ToUInt32(key, 0));
            }
            else
            {
                keyValue = Encoding.UTF8.GetString(key);
            }

            int          fieldIndex;
            ProcessState top = PeekStackTop();

            if (top.State == EProcessingState.TopSearch &&
                TestFieldAttributeState(top, keyValue, out fieldIndex))
            {
                PushTypeAttributesToStack(typeof(T), Target);
            }
            else if (top.State == EProcessingState.FieldSearch &&
                     TestFieldAttributeState(top, keyValue, out fieldIndex))
            {
                PropertyInfo       prop   = top.Properties[fieldIndex];
                BlobFieldAttribute attrib = top.Attributes[fieldIndex];

                Type subType = prop.PropertyType;
                Type genericType;
                int  genericCount;

                if (subType.IsTypeListOrDictionary(out genericType, out genericCount))
                {
                    object subInstance = CacheUtil.FastConstruct(TypedCache, subType); //Activator.CreateInstance(subType);

                    prop.SetValue(top.WorkingType, subInstance, null);

                    PushListTypeAttributesToStack(subInstance, attrib, prop);
                }
                else if (attrib.Complex == true)
                {
                    object subInstance = CacheUtil.FastConstruct(TypedCache, subType); //Activator.CreateInstance(subType);

                    prop.SetValue(top.WorkingType, subInstance, null);

                    PushTypeAttributesToStack(subType, subInstance);
                }
                else if (attrib.SubFieldDepth > 0 ||
                         attrib.SubFieldKey != null)
                {
                    PushSubSearchAttributeToStack(top, attrib, prop, top.WorkingType);
                }
                else
                {
                    expectingIndex = fieldIndex;
                }
            }
            else if (top.State == EProcessingState.SubFieldSearch &&
                     TestSubFieldAttributeState(top, keyValue, out fieldIndex))
            {
                expectingIndex = fieldIndex;
            }
            else if (top.State == EProcessingState.EveryFieldSearch &&
                     depth - top.Depth == 1)
            {
                PropertyInfo prop = top.Properties[0];

                Type subType;
                int  genericCount;

                bool genericType = prop.PropertyType.IsTypeListOrDictionary(out subType, out genericCount);

                if (top.Attributes[0].Complex == true && genericType)
                {
                    object subInstance = CacheUtil.FastConstruct(TypedCache, subType); //Activator.CreateInstance(subType);

                    CacheUtil.HandleAddCall(prop, genericCount, top.WorkingType, keyValue, subInstance);

                    PushTypeAttributesToStack(subType, subInstance);
                }
                else if (fieldSize == 0 && genericType)
                {
                    // key is the actual field.
                    object value = GetDataForProp(key, subType);

                    CacheUtil.HandleAddCall(prop, genericCount, top.WorkingType, expectedKey, value);
                }
                else
                {
                    expectedKey    = keyValue;
                    expectingIndex = 0;
                }
            }
        }