Esempio n. 1
0
        private static Dictionary <Period, T> GetPeriodData <T>(
            XElement container,
            Dictionary <string, List <Period> > namedPeriods,
            string valueElementName     = "value",
            Func <XElement, T> getValue = null)
        {
            Dictionary <Period, T> result = null;

            if (container != null)
            {
                string periodName = container.GetAttributeValue("time-layout", null);
                if (!string.IsNullOrEmpty(periodName) && namedPeriods.TryGetValue(periodName, out List <Period> periods))
                {
                    var valueElements = container.Elements(valueElementName);
                    if (valueElements.Count() == periods.Count)
                    {
                        result = new Dictionary <Period, T>(periods.Count);

                        foreach (var pair in valueElements.Zip(periods, (item, period) => Tuple.Create(item, period)))
                        {
                            T value = getValue != null?getValue(pair.Item1) : ConvertUtility.ConvertValue <T>(pair.Item1.Value);

                            result.Add(pair.Item2, value);
                        }
                    }
                }
            }

            return(result);
        }
Esempio n. 2
0
        public void ConvertValueTest()
        {
            object?actualObject = ConvertUtility.ConvertValue("1, 2", typeof(Point));

            actualObject.ShouldNotBeNull();
            Point actual   = (Point)actualObject !;
            Point expected = new(1, 2);

            Assert.AreEqual(expected, actual);

            actual   = ConvertUtility.ConvertValue <Point>("3, 4");
            expected = new Point(3, 4);
            Assert.AreEqual(expected, actual);
        }