Code:
/ 4.0 / 4.0 / DEVDIV_TFS / Dev10 / Releases / RTMRel / wpf / src / Base / MS / Internal / TraceLevelStore.cs / 1305600 / 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)
{
Key key = new Key(element);
if (!_dictionary.TryGetValue(key, 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, true);
if (traceLevel > PresentationTraceLevel.None)
{
_dictionary[key] = traceLevel;
}
else
{
_dictionary.Remove(key);
}
}
}
#endregion Internal Methods
#region Private Fields
//
// Private Fields
//
private static Dictionary _dictionary = new Dictionary();
#endregion Private Fields
#region Table Keys
// the key for the dictionary: <((element)), hashcode>
private struct Key
{
internal Key(object element, bool useWeakRef)
{
_element = new WeakReference(element);
_hashcode = element.GetHashCode();
}
internal Key(object element)
{
_element = element;
_hashcode = element.GetHashCode();
}
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
}
#endregion Table Keys
}
}
// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
// Copyright (c) Microsoft Corporation. All rights reserved.
//----------------------------------------------------------------------------
//
//
// 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)
{
Key key = new Key(element);
if (!_dictionary.TryGetValue(key, 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, true);
if (traceLevel > PresentationTraceLevel.None)
{
_dictionary[key] = traceLevel;
}
else
{
_dictionary.Remove(key);
}
}
}
#endregion Internal Methods
#region Private Fields
//
// Private Fields
//
private static Dictionary _dictionary = new Dictionary();
#endregion Private Fields
#region Table Keys
// the key for the dictionary: <((element)), hashcode>
private struct Key
{
internal Key(object element, bool useWeakRef)
{
_element = new WeakReference(element);
_hashcode = element.GetHashCode();
}
internal Key(object element)
{
_element = element;
_hashcode = element.GetHashCode();
}
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
}
#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
- StrokeNode.cs
- Constraint.cs
- OpenFileDialog.cs
- UserPersonalizationStateInfo.cs
- TextProperties.cs
- ConnectionPointGlyph.cs
- formatstringdialog.cs
- DataSourceHelper.cs
- Baml6Assembly.cs
- CustomLineCap.cs
- AttributeCollection.cs
- NullableDoubleAverageAggregationOperator.cs
- MessageVersion.cs
- KeyedPriorityQueue.cs
- webeventbuffer.cs
- ContentPropertyAttribute.cs
- CodeLabeledStatement.cs
- LateBoundChannelParameterCollection.cs
- NumberFormatInfo.cs
- ElementsClipboardData.cs
- NamespaceQuery.cs
- HwndHostAutomationPeer.cs
- PolicyVersion.cs
- WindowsListViewItemStartMenu.cs
- CharEntityEncoderFallback.cs
- InvalidDataContractException.cs
- _ReceiveMessageOverlappedAsyncResult.cs
- ScrollBar.cs
- ValueSerializerAttribute.cs
- SHA256.cs
- FilterEventArgs.cs
- BlurBitmapEffect.cs
- DirectoryObjectSecurity.cs
- TextRenderer.cs
- SHA512.cs
- StreamInfo.cs
- DateTimeConverter2.cs
- SQLDouble.cs
- TailPinnedEventArgs.cs
- SocketElement.cs
- HatchBrush.cs
- LinkLabelLinkClickedEvent.cs
- FileSystemEnumerable.cs
- BinaryFormatter.cs
- PerfCounterSection.cs
- HttpListenerException.cs
- BindValidationContext.cs
- IisNotInstalledException.cs
- SettingsBase.cs
- ParenthesizePropertyNameAttribute.cs
- SecureConversationServiceElement.cs
- VersionPair.cs
- AnnouncementEventArgs.cs
- DataGridViewBand.cs
- Select.cs
- XPathAxisIterator.cs
- Assert.cs
- XPathNavigator.cs
- BaseTemplateBuildProvider.cs
- PropertyExpression.cs
- XmlSignificantWhitespace.cs
- DataControlFieldCell.cs
- XamlPathDataSerializer.cs
- ServiceProviders.cs
- IntSecurity.cs
- SessionStateContainer.cs
- EventManager.cs
- GridViewAutoFormat.cs
- GenericAuthenticationEventArgs.cs
- SrgsElement.cs
- SoapServerMessage.cs
- SharedPerformanceCounter.cs
- InfoCardRSAPKCS1SignatureFormatter.cs
- NotifyParentPropertyAttribute.cs
- Point3D.cs
- PagedDataSource.cs
- ProcessModelSection.cs
- LambdaCompiler.Logical.cs
- PluralizationServiceUtil.cs
- StandardBindingReliableSessionElement.cs
- DataControlPagerLinkButton.cs
- AsyncResult.cs
- Int64KeyFrameCollection.cs
- SqlDataSourceFilteringEventArgs.cs
- HttpServerVarsCollection.cs
- InputLanguageProfileNotifySink.cs
- MetaChildrenColumn.cs
- ConnectionsZone.cs
- Rule.cs
- ColumnMap.cs
- SqlTypeSystemProvider.cs
- ColorKeyFrameCollection.cs
- MetadataArtifactLoaderResource.cs
- WindowsSpinner.cs
- DecimalAnimationUsingKeyFrames.cs
- FolderBrowserDialog.cs
- ComboBoxDesigner.cs
- SqlConnectionFactory.cs
- HealthMonitoringSectionHelper.cs
- DateTimeOffsetConverter.cs