public void CacheHit() { string instanceName = GetCounterInstanceName(0); using (PerfCounterManager manager = new PerfCounterManager()) { using (PerformanceCounter counter = new PerformanceCounter(CATEGORY , SERIALIZER_HITS_NAME , instanceName , true)) { Assert.IsNotNull(counter); Assert.AreEqual(0.0, (decimal)counter.NextValue()); manager.IncrementHitCount(); Assert.AreEqual(1.0, (decimal)counter.NextValue()); } } using (PerformanceCounter counter = new PerformanceCounter(CATEGORY , SERIALIZER_HITS_NAME , instanceName , true)) { try { long l = counter.RawValue; } catch (InvalidOperationException) { // the counter instance went away when we // disposed the manager object. // This exception is what we wanted. } } }
/// <summary> /// Get an XmlSerializer instance for the /// specified parameters. The method will check if /// any any previously cached instances are compatible /// with the parameters before constructing a new /// XmlSerializer instance. /// </summary> /// <param name="type"></param> /// <param name="overrides"></param> /// <param name="types"></param> /// <param name="root"></param> /// <param name="defaultNamespace"></param> /// <returns></returns> public System.Xml.Serialization.XmlSerializer GetSerializer(Type type , XmlAttributeOverrides overrides , Type[] types , XmlRootAttribute root , String defaultNamespace) { string key = CacheKeyFactory.MakeKey(type , overrides , types , root , defaultNamespace); XmlSerializer serializer; if (false == Serializers.ContainsKey(key)) { lock (SyncRoot) { if (false == Serializers.ContainsKey(key)) { #if USE_PERF_COUNTER stats.IncrementInstanceCount(); #endif serializer = new XmlSerializer(type , overrides , types , root , defaultNamespace); Serializers.Add(key, serializer); if (null != NewSerializer) { NewSerializer(type , overrides , types , root , defaultNamespace); } } else { serializer = Serializers[key]; } } // lock } // if( null == Serializers[key] ) else { serializer = Serializers[key]; #if USE_PERF_COUNTER stats.IncrementHitCount(); #endif // Tell the listeners that we already // had a serializer that matched the attributes if (null != CacheHit) { CacheHit(type , overrides , types , root , defaultNamespace); } } System.Diagnostics.Debug.Assert(null != serializer); return(serializer); }