internal static string GetParameterNameStatic(Enum property, PropertyCache cache)
        {
            var    attribute = cache.GetAttribute <XmlElementAttribute>();
            string name;

            if (attribute == null)
            {
                var description = property.GetDescription(false);

                if (description == null)
                {
                    throw new MissingAttributeException(typeof(TObjectProperty), property.ToString(), typeof(DescriptionAttribute));
                }

                name = description;
            }
            else
            {
                name = attribute.ElementName.Substring("injected_".Length);
            }

            if (property.GetEnumAttribute <LiteralValueAttribute>() == null)
            {
                name += "_";
            }

            return(name);
        }
Ejemplo n.º 2
0
        internal static string GetObjectPropertyNameViaCache(Enum property, PropertyCache cache)
        {
            var mergeable = property.GetEnumAttribute <MergeableAttribute>();

            if (mergeable != null)
            {
                throw new InvalidOperationException($"'{property}' is a virtual property and cannot be retrieved directly. To access this value, property '{mergeable.Dependency}' should be retrieved instead.");
            }

            var    attribute = cache?.GetAttribute <XmlElementAttribute>();
            string name;

            if (attribute == null)
            {
                var description = property.GetDescription(false);

                if (description == null)
                {
                    throw new MissingAttributeException(property.GetType(), property.ToString(), typeof(DescriptionAttribute));
                }

                name = description;
            }
            else
            {
                name = attribute.ElementName.Substring("injected_".Length);
            }

            if (property.GetEnumAttribute <LiteralValueAttribute>() == null)
            {
                name += "_";
            }

            return(name);
        }
        private bool HasRequireValueTrue(PropertyCache property)
        {
            var attrib = property.GetAttribute <RequireValueAttribute>();

            if (attrib != null)
            {
                return(attrib.ValueRequired);
            }

            return(false);
        }
        /// <summary>
        /// Generate an expression like
        ///
        /// void InitSensor(object[] nameArray, XmlReader reader)
        /// {
        ///     nameArray[0] = reader.NameTable.Add("name");
        ///     nameArray[1] = reader.NameTable.Add("objid");
        /// }
        /// </summary>
        /// <returns>The invocation of the generated lambda.</returns>
        private Expression PopulateNameTable(PropertyCache parentProperty)
        {
            var nameTable = XmlExpressionConstants.XmlReader_NameTable;

            var names = Mappings.SelectMany(m => m.AttributeValue).ToArray();

            var assignments = new List <BinaryExpression>();

            //We don't have any deserializable elements (e.g. GetTotalObjects which just asks for a TableData<object>)
            if (names.Length == 0)
            {
                return(null);
            }

            for (var i = 0; i < names.Length; i++)
            {
                //We're probably an XmlTextAttribute; get our parent's name
                if (names[i] == null)
                {
                    names[i] = parentProperty.GetAttribute <XmlElementAttribute>().ElementName;
                }

                //XmlTextAttribute items don't have an AttributeValue
                if (names[i] != null)
                {
                    var name = Expression.ArrayAccess(                                                                //nameArray[0]
                        XmlExpressionConstants.SerializerNames,
                        Expression.Constant(i)
                        );

                    var add = Expression.Call(                                                                        //reader.NameTable.Add("name")
                        nameTable,
                        XmlSerializerMembers.XmlNameTable_Add,
                        Expression.Constant(names[i])
                        );

                    var assign = Expression.Assign(name, add);                                                        //nameArray[0] = reader.NameTable.Add("name")

                    assignments.Add(assign);
                }
            }

            var lambda = Expression.Lambda(
                Expression.Block(typeof(void), assignments.ToArray()),                                                //Body
                $"Init{trueType.Name}",                                                                               //Name
                new[] { XmlExpressionConstants.SerializerNames, XmlExpressionConstants.XmlReader }                    //Parameters
                );

            var expr = LambdaOrDelegate(lambda);

            return(Expression.Invoke(expr, XmlExpressionConstants.SerializerNames, XmlExpressionConstants.XmlReader)); //InitSensor(nameArray, serializer.reader)
        }
        private int GetPosition(PropertyCache property, bool isNameParameter, NewSensorAttribute parameterConfig, int?position)
        {
            if (isNameParameter)
            {
                if (parameterConfig.DynamicName)
                {
                    return(int.MinValue);
                }

                return(1);
            }

            if (property.GetAttribute <RequireValueAttribute>()?.ValueRequired == true)
            {
                //If we have an invoker, don't give positions to required values in case the type of parameter
                //at this position conflicts with the parameter that might be used in the cmdlet parameter set
                if (makeCmdlet != null)
                {
                    return(int.MinValue);
                }

                if (parameterConfig.DynamicName)
                {
                    if (position == null)
                    {
                        position = 1; //Set it to the first position after the SensorType SwitchParameter
                    }
                    else
                    {
                        position++;
                    }
                }
                else
                {
                    if (position == null)
                    {
                        position = 2; //Set it to be the first position after the SensorType SwitchParameter and Name
                    }
                    else
                    {
                        position++;
                    }
                }

                return(position.Value);
            }

            return(int.MinValue);
        }
Ejemplo n.º 6
0
        private PropertyCache GetInternalProperty(PropertyCache cache, PropertyCache[] internalProperties)
        {
            var property = cache.GetAttribute <PropertyParameterAttribute>().Name.ToEnum <Property>();

            var description = property.GetDescription().ToLower();

            var prop = internalProperties.First(p =>
            {
                var name = p.GetAttributes <XmlElementAttribute>().FirstOrDefault()?.ElementName;
                return(name == description || name == $"{description}_raw");
            });

            internalPropertyMap[prop.Property] = cache;

            return(prop);
        }
        private bool PropertyIsAllowedInSet(PropertyCache property, ParameterSetDescriptor set)
        {
            if (set.ExcludedParameters.Length == 0)
            {
                return(true);
            }

            var attrib = property.GetAttribute <PropertyParameterAttribute>();

            if (set.ExcludedParameters.Contains((attrib.Property)))
            {
                return(false);
            }

            return(true);
        }
Ejemplo n.º 8
0
        private static Tuple <PropertyInfo, TEnum, PropertyParameterAttribute> GetTuple <TEnum>(PropertyCache cache) where TEnum : struct
        {
            var attr = cache.GetAttribute <PropertyParameterAttribute>();

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

            if (attr.Property.GetType() == typeof(TEnum))
            {
                return(Tuple.Create(cache.Property, (TEnum)(object)attr.Property, attr));
            }

            return(null);
        }
        private static Tuple <PropertyInfo, TEnum, PropertyParameterAttribute> GetTuple <TEnum>(PropertyCache cache) where TEnum : struct
        {
            var attr = cache.GetAttribute <PropertyParameterAttribute>();

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

            TEnum prop;

            if (Enum.TryParse(attr.Name, out prop))
            {
                return(Tuple.Create(cache.Property, prop, attr));
            }

            return(null);
        }
Ejemplo n.º 10
0
        private object GetValue(XObject mandatory, PropertyCache propertyCache, object value, XElement elm)
        {
            var final = mandatory == null ? null : GetValueInternal(propertyCache.Property.PropertyType, value, elm);

            var attrib = propertyCache.GetAttribute <PropertyParameterAttribute>();

            if (attrib != null)
            {
                if (attrib.Property.GetType() == typeof(Property))
                {
                    var converter = attrib.Property.GetEnumFieldCache().GetAttributes <ValueConverterAttribute>().FirstOrDefault();

                    if (converter != null)
                    {
                        return(converter.Converter.Deserialize(final));
                    }
                }
            }

            return(final);
        }
Ejemplo n.º 11
0
        private object GetValue(XObject mandatory, PropertyCache propertyCache, object value, XElement elm)
        {
            var final = mandatory == null ? null : GetValueInternal(propertyCache.Property.PropertyType, value, elm);

            var attrib = propertyCache.GetAttribute <PropertyParameterAttribute>();

            if (attrib != null)
            {
                Enum prop;

                if (ReflectionCacheManager.GetEnumName(typeof(Property)).Cache.NameCache.TryGetValue(attrib.Name, out prop))
                {
                    var converter = prop.GetEnumFieldCache().GetAttributes <ValueConverterAttribute>().FirstOrDefault();

                    if (converter != null)
                    {
                        return(converter.Converter.Deserialize(final));
                    }
                }
            }

            return(final);
        }