示例#1
0
        private BlockExpression GenerateNormalPropertyBlock(Expression flag)
        {
            var        deserializer   = new ValueDeserializer(mapping, generator);
            var        variables      = new List <ParameterExpression>();
            Expression initExpression = null;
            var        value          = deserializer.Deserialize(ref initExpression, ref variables);

            var memberAccess     = Expression.MakeMemberAccess(Target, mapping.PropertyCache.Property); //obj.Name
            var memberAssignment = Expression.Assign(memberAccess, value);                              //obj.Name = "Ping"
            var setFlag          = Expression.Assign(flag, Expression.Constant(true));                  //flagsArray[0] = true

            var body = new List <Expression>();

            if (initExpression != null)
            {
                body.Add(initExpression);
            }

            body.Add(memberAssignment);
            body.Add(setFlag);

            //Now put the expressions above together
            var block = Expression.Block(
                typeof(void),                                                                       // {
                variables,                                                                          //     obj.Name = "Ping";
                body                                                                                //     flagsArray[0] = true;
                );                                                                                  // }

            return(block);
        }
示例#2
0
        private BlockExpression GenerateNormalPropertyBlock(Expression flag)
        {
            var deserializer = new ValueDeserializer(mapping, generator);
            var value        = deserializer.Deserialize();

            var memberAccess     = Expression.MakeMemberAccess(Target, mapping.PropertyCache.Property); //obj.Name
            var memberAssignment = Expression.Assign(memberAccess, value);                              //obj.Name = "Ping"
            var setFlag          = Expression.Assign(flag, Expression.Constant(true));                  //flagsArray[0] = true

            var block = Expression.Block(                                                               // {
                typeof(void),
                memberAssignment,                                                                       //     obj.Name = "Ping";
                setFlag                                                                                 //     flagsArray[0] = true;
                );                                                                                      // }

            return(block);
        }
示例#3
0
        private Expression GetCaseBody(Enum property, Expression rawValue)
        {
            var viaObject = false;

            var typeLookup = property.GetEnumAttribute <TypeLookupAttribute>()?.Class; //ObjectPropertyInternal members don't necessarily have a type lookup

            if (typeLookup == null)
            {
                return(null);
            }

            var mappings   = ReflectionCacheManager.Map(typeLookup).Cache;
            var cache      = ObjectPropertyParser.GetPropertyInfoViaTypeLookup(property);
            var xmlElement = cache.GetAttribute <XmlElementAttribute>(); //todo: what if this objectproperty doesnt point to a member with an xmlelementattribute?

            XmlMapping mapping = null;

            if (xmlElement != null)
            {
                mapping = mappings.FirstOrDefault(m => m.AttributeValue[0] == xmlElement.ElementName);
            }
            else
            {
                var mergeAttribute = property.GetEnumAttribute <MergeableAttribute>();

                //If we're a property like LocationName, we don't exist server side - we're only used for constructing requests
                if (mergeAttribute != null)
                {
                    return(null);
                }

                //We have a property like Interval which uses a backing property instead.
                //Get the backing property so that we may extract the real value from the public
                //property
                var rawName     = ObjectPropertyParser.GetObjectPropertyNameViaCache(property, cache);
                var elementName = $"{HtmlParser.DefaultPropertyPrefix}{rawName.TrimEnd('_')}";

                mapping = mappings.FirstOrDefault(m => m.AttributeValue[0] == elementName);

                if (mapping != null)
                {
                    viaObject = true;
                }
            }

            if (mapping != null)
            {
                var deserializer = new ValueDeserializer(mapping, null, rawValue);
                var result       = deserializer.Deserialize();

                if (viaObject)
                {
                    //Construct an expression like return new TableSettings { intervalStr = "60|60 seconds" }.Interval;
                    var settingsObj    = Expression.Variable(typeLookup, "obj");
                    var assignObj      = settingsObj.Assign(Expression.New(typeLookup));
                    var internalProp   = Expression.MakeMemberAccess(settingsObj, mapping.PropertyCache.Property);
                    var assignInternal = internalProp.Assign(result);
                    var externalProp   = Expression.MakeMemberAccess(settingsObj, cache.Property);

                    return(Expression.Block(
                               new[] { settingsObj },
                               assignObj,
                               assignInternal,
                               Expression.Convert(externalProp, typeof(object))
                               ));
                }

                return(result);
            }

            //Property is not deserializable
            return(null);
        }