Beispiel #1
0
        private void TestDeserialization(FakeObjectProperty property, object value, object expected)
        {
            var parser = new DynamicPropertyTypeParser(property, ObjectPropertyParser.GetPropertyInfoViaTypeLookup(property), value);

            var result = parser.DeserializeValue();

            Assert.AreEqual(expected, result);
        }
        internal void AddDependentProperty(object val, Enum parent)
        {
            var cache = GetPropertyCache(parent);

            var dependencyParser = new DynamicPropertyTypeParser(parent, cache, val);

            AddParameter(parent, cache, dependencyParser.ParseValue());
        }
        internal void AddDependentProperty(object val, Enum parent)
        {
            var info = GetPropertyInfo(parent);

            var dependencyParser = new DynamicPropertyTypeParser(parent, info, val);

            AddParameter(parent, info, dependencyParser.ParseValue());
        }
        void AddDependents(DynamicPropertyTypeParser parser, bool disableDependentsOnNotReqiuiredValue)
        {
            var childrenOfParent = parser.Property.GetDependentProperties <TObjectProperty>().Cast <Enum>().ToList();

            bool isParent = childrenOfParent.Any();

            AddDependentPropertyRecursiveUp(parser.Property);

            if (isParent)
            {
                TryDisableDependentProperties(parser, disableDependentsOnNotReqiuiredValue, childrenOfParent);
            }
        }
        private void TryDisableDependentProperties(DynamicPropertyTypeParser parser, bool disableDependentsOnNotReqiuiredValue, List <Enum> childrenOfParent)
        {
            //If we reach this method, we know that our property is a parent

            if (parser != null)
            {
                TryDisableDependentPropertiesWithTypeCheck(parser, disableDependentsOnNotReqiuiredValue, childrenOfParent);
            }
            else
            {
                TryDisableDependentPropertiesWithoutTypeCheck(childrenOfParent);
            }
        }
        protected void AddTypeSafeValue(Enum property, object value, bool disableDependentsOnNotReqiuiredValue)
        {
            if (!paramsInitialized)
            {
                CustomParameters  = new List <CustomParameter>();
                paramsInitialized = true;
            }

            var cache = GetPropertyCache(property);

            var parser = new DynamicPropertyTypeParser(property, cache, value);

            AddValue(parser);
            AddDependents(parser, disableDependentsOnNotReqiuiredValue);
        }
        void AddValue(DynamicPropertyTypeParser parser)
        {
            var val = parser.ParseValue();

            AddParameter(parser.Property, parser.Cache, val);

            var secondaryPropertyAttrib = parser.Property.GetEnumAttribute <SecondaryPropertyAttribute>();

            if (secondaryPropertyAttrib != null)
            {
                if (parser.Value is IMultipleSerializable)
                {
                    var val2 = ((IMultipleSerializable)parser.Value).GetSerializedFormats().Last();

                    CustomParameters.Add(new CustomParameter($"{secondaryPropertyAttrib.Name}_", val2));
                }
            }
        }
Beispiel #8
0
        private void TestArrayDeserialization(FakeObjectProperty property, object value, object expected)
        {
            var parser = new DynamicPropertyTypeParser(property, ObjectPropertyParser.GetPropertyInfoViaTypeLookup(property), value);

            var result = parser.DeserializeValue();

            if (result == null)
            {
                Assert.AreEqual(expected, result);
            }
            else
            {
                Assert.IsInstanceOfType(expected, typeof(string[]));
                Assert.IsInstanceOfType(result, typeof(string[]));

                AssertEx.AreEqualLists(((string[])expected).ToList(), (((string[])result).ToList()), "Expected and result were not equal");
            }
        }
        private void TryDisableDependentPropertiesWithTypeCheck(DynamicPropertyTypeParser parser, bool disableDependentsOnNotReqiuiredValue, List <Enum> childrenOfParent)
        {
            var parentValueAsTypeRequiredByProperty = parser.ToPrimitivePropertyType();

            foreach (var child in childrenOfParent)
            {
                var attrib = child.GetEnumAttribute <DependentPropertyAttribute>(); //By virtue of the fact we're a child, we can guarantee we have a DependentPropertyAttribute
                var propertyRequiredValue = attrib.RequiredValue;

                if (propertyRequiredValue != null && propertyRequiredValue.GetType() != parentValueAsTypeRequiredByProperty.GetType())
                {
                    throw new InvalidTypeException($"Dependencies of property '{parser.Property}' should be of type {parser.PropertyType}, however property '{child}' is dependent on type '{propertyRequiredValue.GetType()}'");
                }

                if (parentValueAsTypeRequiredByProperty.Equals(propertyRequiredValue)) // == does not work since we are potentially comparing value types wrapped as type object
                {
                    //If a child has a reverse dependency (e.g. VerticalAxisMin must be included when VerticalAxisScaling = Manual)
                    //Then we should include those children, where they will be set to empty causing PRTG to throw an exception
                    if (attrib.ReverseDependency)
                    {
                        AddParameter(child, GetPropertyCache(child), string.Empty);
                        var newChildren = child.GetDependentProperties <TObjectProperty>().Cast <Enum>().ToList();
                        TryDisableDependentProperties(null, disableDependentsOnNotReqiuiredValue, newChildren);
                    }
                }
                else
                {
                    //If we wish to disable dependents when their parent value does not match their required value, add and assign them an empty value
                    if (disableDependentsOnNotReqiuiredValue)
                    {
                        AddParameter(child, GetPropertyCache(child), string.Empty);
                        var newChildren = child.GetDependentProperties <TObjectProperty>().Cast <Enum>().ToList();
                        TryDisableDependentProperties(null, disableDependentsOnNotReqiuiredValue, newChildren);
                    }
                }
            }
        }