Exemplo n.º 1
0
        public void CanAddConvertFuncByTargetType()
        {
            // Register a converter by target type when you want all properties of the
            // target type to use the same converter.

            // Call the Add method for each converter that needs to be registered.
            var first = new ValueConverters();

            first.Add(typeof(Bar), value => new Bar(int.Parse(value)));
            first.Add(typeof(Baz), ParseBaz);

            // The Add method returns 'this', so you can chain them together:
            var second = new ValueConverters()
                         .Add(typeof(Bar), value => new Bar(int.Parse(value)))
                         .Add(typeof(Baz), ParseBaz);

            // List initialization syntax works also.
            var third = new ValueConverters
            {
                { typeof(Bar), value => new Bar(int.Parse(value)) },
                { typeof(Baz), ParseBaz }
            };

            // All three instances represent the same thing. Verify that their
            // contents are the same.

            // ValueConverters implements IEnumerable<KeyValuePair<string, Type>>
            var firstList  = first.ToList();
            var secondList = second.ToList();
            var thirdList  = third.ToList();

            Assert.Equal(firstList.Count, secondList.Count);
            Assert.Equal(secondList.Count, thirdList.Count);

            for (int i = 0; i < firstList.Count; i++)
            {
                Assert.Equal(firstList[i].Key, secondList[i].Key);
                Assert.Equal(firstList[i].Value, secondList[i].Value);

                Assert.Equal(secondList[i].Key, thirdList[i].Key);
                Assert.Equal(secondList[i].Value, thirdList[i].Value);
            }
        }