Code:
/ DotNET / DotNET / 8.0 / untmp / WIN_WINDOWS / lh_tools_devdiv_wpf / Windows / wcp / Base / MS / Internal / TraceLevelStore.cs / 2 / TraceLevelStore.cs
//---------------------------------------------------------------------------- // //// Copyright (C) Microsoft Corporation. All rights reserved. // // // Description: Storage for the "TraceLevel" attached property. // //--------------------------------------------------------------------------- using System; using System.Collections.Generic; using System.Diagnostics; namespace MS.Internal { ////// This class stores values for the attached property /// PresentationTraceSources.TraceLevel. /// internal static class TraceLevelStore { #region Constructors // // Constructors // #endregion Constructors #region Internal Methods // // Internal Methods // ////// Reads the attached property TraceLevel from the given element. /// internal static PresentationTraceLevel GetTraceLevel(object element) { PresentationTraceLevel result; if (element == null || _dictionary.Count == 0) { result = PresentationTraceLevel.None; } else { lock (_dictionary) { using (SetLookupKey(element)) { if (!_dictionary.TryGetValue(_lookupKey, out result)) { result = PresentationTraceLevel.None; } } } } return result; } ////// Writes the attached property TraceLevel to the given element. /// internal static void SetTraceLevel(object element, PresentationTraceLevel traceLevel) { if (element == null) return; lock (_dictionary) { Key key = new Key(element); if (traceLevel > PresentationTraceLevel.None) { _dictionary[key] = traceLevel; } else { _dictionary.Remove(key); } } } #endregion Internal Methods #region Private Methods // set the lookup key - IDisposable ensures it gets cleared after // use, so that it doesn't hold a reference to the element static IDisposable SetLookupKey(object element) { _lookupKey.Set(element); return _lookupKeyDisposable; } #endregion Private Methods #region Private Fields // // Private Fields // private static Dictionary_dictionary = new Dictionary (); private static Key _lookupKey; private static LookupKeyDisposable _lookupKeyDisposable = new LookupKeyDisposable(); #endregion Private Fields #region Table Keys // the key for the dictionary: <((element)), hashcode> private struct Key { internal Key(object element) { _element = new WeakReference(element); _hashcode = element.GetHashCode(); } internal void Set(object element) { _element = element; _hashcode = element.GetHashCode(); } internal void Clear() { _element = null; _hashcode = 0; } public override int GetHashCode() { #if DEBUG WeakReference wr = _element as WeakReference; object element = (wr != null) ? wr.Target : _element; if (element != null) { int hashcode = element.GetHashCode(); Debug.Assert(hashcode == _hashcode, "hashcodes disagree"); } #endif return _hashcode; } public override bool Equals(object o) { if (o is Key) { WeakReference wr; Key that = (Key)o; if (this._hashcode != that._hashcode) return false; wr = this._element as WeakReference; object s1 = (wr != null) ? wr.Target : this._element; wr = that._element as WeakReference; object s2 = (wr != null) ? wr.Target : that._element; if (s1!=null && s2!=null) return (s1 == s2); else return (this._element == that._element); } else { return false; } } public static bool operator==(Key key1, Key key2) { return key1.Equals(key2); } public static bool operator!=(Key key1, Key key2) { return !key1.Equals(key2); } object _element; // lookup: direct ref. In table: WeakRef int _hashcode; // cached, in case source is GC'd } private class LookupKeyDisposable : IDisposable { public void Dispose() { TraceLevelStore._lookupKey.Clear(); } } #endregion Table Keys } } // File provided for Reference Use Only by Microsoft Corporation (c) 2007. // Copyright (c) Microsoft Corporation. All rights reserved.
Link Menu
This book is available now!
Buy at Amazon US or
Buy at Amazon UK
- Button.cs
- RectangleHotSpot.cs
- DesignerWidgets.cs
- DirectoryRedirect.cs
- BuildResultCache.cs
- CodeDOMProvider.cs
- ImageMap.cs
- PersonalizationDictionary.cs
- EntitySet.cs
- WebPartDisplayModeCollection.cs
- SinglePhaseEnlistment.cs
- XPathEmptyIterator.cs
- LogicalExpr.cs
- X500Name.cs
- SafeSystemMetrics.cs
- InstallerTypeAttribute.cs
- ProxyGenerator.cs
- Vector3DIndependentAnimationStorage.cs
- Html32TextWriter.cs
- DataSourceControl.cs
- OperatorExpressions.cs
- BindingMemberInfo.cs
- XPathNavigator.cs
- XmlAttributeProperties.cs
- Attributes.cs
- ExtendedPropertyCollection.cs
- HelpEvent.cs
- DataGridViewCellParsingEventArgs.cs
- PageBreakRecord.cs
- ScriptControlDescriptor.cs
- WindowsToolbar.cs
- ProviderCollection.cs
- DayRenderEvent.cs
- DoubleAnimationUsingKeyFrames.cs
- CodeVariableDeclarationStatement.cs
- WindowsPen.cs
- BuildResult.cs
- SequentialUshortCollection.cs
- ElementsClipboardData.cs
- EventProviderClassic.cs
- _ConnectStream.cs
- TemplateBamlTreeBuilder.cs
- ImageBrush.cs
- VectorCollectionConverter.cs
- DynamicFilter.cs
- ConstructorNeedsTagAttribute.cs
- Italic.cs
- WebPartAuthorizationEventArgs.cs
- IdentityElement.cs
- SecurityPermission.cs
- WindowsToolbar.cs
- UnitySerializationHolder.cs
- XPathNodeHelper.cs
- DBAsyncResult.cs
- PKCS1MaskGenerationMethod.cs
- ICspAsymmetricAlgorithm.cs
- XmlILStorageConverter.cs
- CheckBox.cs
- DesignRelation.cs
- assemblycache.cs
- EditCommandColumn.cs
- XmlSerializer.cs
- ConfigViewGenerator.cs
- DescriptionCreator.cs
- NamespaceQuery.cs
- DataGridViewControlCollection.cs
- MenuItemStyleCollection.cs
- EdmComplexPropertyAttribute.cs
- HttpFileCollectionWrapper.cs
- OleStrCAMarshaler.cs
- FilterFactory.cs
- DetailsView.cs
- ToolBarButtonDesigner.cs
- NotFiniteNumberException.cs
- DataControlImageButton.cs
- _SSPIWrapper.cs
- AttachedPropertyBrowsableWhenAttributePresentAttribute.cs
- AttributeData.cs
- ConstructorBuilder.cs
- LocatorGroup.cs
- SamlAuthenticationStatement.cs
- Propagator.JoinPropagator.JoinPredicateVisitor.cs
- UInt32Converter.cs
- X509CertificateCollection.cs
- OleAutBinder.cs
- QilLiteral.cs
- Cursor.cs
- StateDesigner.TransitionInfo.cs
- ExternalDataExchangeService.cs
- ISessionStateStore.cs
- Util.cs
- DynamicDataRouteHandler.cs
- SqlDependency.cs
- SecurityManager.cs
- FusionWrap.cs
- XmlAttribute.cs
- FollowerQueueCreator.cs
- WebEvents.cs
- printdlgexmarshaler.cs
- InputLanguage.cs