Ejemplo n.º 1
0
        public void BothCounters()
        {
            using (XmlSerializerCache cache = new XmlSerializerCache())
            {
                string instanceName = PerfCounterManagerTests.GetCounterInstanceName(0);
                using (PerformanceCounter instanceCounter = new PerformanceCounter(PerfCounterManagerTests.CATEGORY
                                                                                   , PerfCounterManagerTests.CACHED_INSTANCES_NAME
                                                                                   , instanceName
                                                                                   , true))
                {
                    Assert.AreEqual(0, instanceCounter.RawValue);
                    using (PerformanceCounter hitCounter = new PerformanceCounter(PerfCounterManagerTests.CATEGORY
                                                                                  , PerfCounterManagerTests.SERIALIZER_HITS_NAME
                                                                                  , instanceName
                                                                                  , true))
                    {
                        Assert.AreEqual(0, hitCounter.RawValue);
                        XmlRootAttribute root = new XmlRootAttribute("theRoot");
                        XmlSerializer    ser  = cache.GetSerializer(typeof(SerializeMe), root);

                        Assert.AreEqual(1, instanceCounter.RawValue);
                        Assert.AreEqual(0, hitCounter.RawValue);
                        ser = cache.GetSerializer(typeof(SerializeMe), root);

                        Assert.AreEqual(1, instanceCounter.RawValue);
                        Assert.AreEqual(1, hitCounter.RawValue);
                    }
                }
            }
        }
        public void AllParams()
        {
            Type[] types1 = new Type[] { typeof(SerializeMe), typeof(SerializeMeToo) };
            Type[] types2 = new Type[] { typeof(SerializeMe), typeof(SerializeMeToo) };
            XmlAttributeOverrides over1 = new XmlAttributeOverrides();
            XmlAttributeOverrides over2 = new XmlAttributeOverrides();
            XmlAttributes         atts1 = new XmlAttributes();
            XmlAttributes         atts2 = new XmlAttributes();

            atts1.XmlType = new XmlTypeAttribute("mytype");
            atts2.XmlType = new XmlTypeAttribute("mytype");

            over1.Add(typeof(SerializeMe), atts1);
            over2.Add(typeof(SerializeMe), atts2);

            XmlRootAttribute root1 = new XmlRootAttribute("someelement");
            XmlRootAttribute root2 = new XmlRootAttribute("someelement");

            string namespace1 = "mynamespace";
            string namespace2 = "mynamespace";

            System.Xml.Serialization.XmlSerializer ser1 = cache.GetSerializer(typeof(SerializeMe)
                                                                              , over1
                                                                              , types1
                                                                              , root1
                                                                              , namespace1);


            Assert.AreEqual(false, CacheHit);
            Assert.AreEqual(true, NewInstaceCreated);
            ClearFlags();

            System.Xml.Serialization.XmlSerializer ser2 = cache.GetSerializer(typeof(SerializeMe)
                                                                              , over2
                                                                              , types2
                                                                              , root2
                                                                              , namespace2);


            Assert.AreEqual(true, CacheHit);
            Assert.AreEqual(false, NewInstaceCreated);

            Assert.AreSame(ser1, ser2);
        }
        private static Type DetermineMessageType(IEnumerable <Type> possibleTypes, string messageBody)
        {
            foreach (var type in possibleTypes)
            {
                var xmlSerializer = XmlSerializerCache.GetSerializer(type);

                using (var tmpStringReader = new StringReader(messageBody))
                {
                    if (xmlSerializer.CanDeserialize(XmlReader.Create(tmpStringReader)))
                    {
                        return(type);
                    }
                }
            }

            // If we got here, we didn't find a matching message type.
            return(null);
        }