Code:
/ 4.0 / 4.0 / untmp / DEVDIV_TFS / Dev10 / Releases / RTMRel / wpf / src / Framework / MS / Internal / TraceData.cs / 1305600 / TraceData.cs
#define TRACE //---------------------------------------------------------------------------- // //// Copyright (C) Microsoft Corporation. All rights reserved. // // // Description: Defines TraceData class, for providing debugging information // for Data Binding and Styling // //--------------------------------------------------------------------------- using System; using System.Collections; using System.ComponentModel; using System.Diagnostics; using System.Reflection; using System.Text; using System.Windows; using System.Windows.Data; using System.Windows.Markup; using MS.Internal.Data; using MS.Win32; namespace MS.Internal { // levels for the various extended traces internal enum TraceDataLevel { // Binding and friends CreateExpression = PresentationTraceLevel.High, // 10, ShowPath = PresentationTraceLevel.High, // 11, ResolveDefaults = PresentationTraceLevel.High, // 13, Attach = PresentationTraceLevel.Low, // 1, AttachToContext = PresentationTraceLevel.Low, // 2, SourceLookup = PresentationTraceLevel.Low, // 4, Activate = PresentationTraceLevel.Low, // 3, Transfer = PresentationTraceLevel.Medium, // 5, Update = PresentationTraceLevel.Medium, // 6, Validation = PresentationTraceLevel.High, // 12, Events = PresentationTraceLevel.Medium, // 7, GetValue = PresentationTraceLevel.High, // 12, ReplaceItem = PresentationTraceLevel.Medium, // 8, GetInfo = PresentationTraceLevel.Medium, // 9, // Data providers ProviderQuery = PresentationTraceLevel.Low, // 1, XmlProvider = PresentationTraceLevel.Medium, // 2, XmlBuildCollection = PresentationTraceLevel.High, // 3, } ////// Provides a central mechanism for providing debugging information /// to aid programmers in using data binding. /// Helpers are defined here. /// The rest of the class is generated; see also: AvTraceMessage.txt and genTraceStrings.pl /// internal static partial class TraceData { // ----------------------------------------------------------------- // Constructors // ----------------------------------------------------------------- static TraceData() { _avTrace.TraceExtraMessages += new AvTraceEventHandler(OnTrace); // This tells tracing that IsEnabled should be true if we're in the debugger, // even if the registry flag isn't turned on. By default, IsEnabled is only // true if the registry is set. _avTrace.EnabledByDebugger = true; // This tells the tracing code not to automatically generate the .GetType // and .HashCode in the trace strings. _avTrace.SuppressGeneratedParameters = true; } // ------------------------------------------------------------------ // Methods // ----------------------------------------------------------------- // determine whether an extended trace should be produced for the given // object static public bool IsExtendedTraceEnabled(object element, TraceDataLevel level) { if (TraceData.IsEnabled) { PresentationTraceLevel traceLevel = PresentationTraceSources.GetTraceLevel(element); return (traceLevel >= (PresentationTraceLevel)level); } else return false; } // report/describe any additional parameters passed to TraceData.Trace() static public void OnTrace( AvTraceBuilder traceBuilder, object[] parameters, int start ) { for( int i = start; i < parameters.Length; i++ ) { object o = parameters[i]; string s = o as string; traceBuilder.Append(" "); if (s != null) { traceBuilder.Append(s); } else if (o != null) { traceBuilder.Append(o.GetType().Name); traceBuilder.Append(":"); Describe(traceBuilder, o); } else { traceBuilder.Append("null"); } } } // ------------------------------------------------------------------ // Helper functions for message string construction // ------------------------------------------------------------------ ////// Construct a string that describes data and debugging information about the object. /// A title is appended in front if provided. /// If object o is not a recognized object, it will be ToString()'ed. /// /// description will be appended to this builder /// object to be described; /// currently recognized types: BindingExpression, Binding, DependencyObject, Exception ///a string that describes the object static public void Describe(AvTraceBuilder traceBuilder, object o) { if (o == null) { traceBuilder.Append("null"); } else if (o is BindingExpression) { BindingExpression bindingExpr = o as BindingExpression; Describe(traceBuilder, bindingExpr.ParentBinding); traceBuilder.Append("; DataItem="); DescribeSourceObject(traceBuilder, bindingExpr.DataItem); traceBuilder.Append("; "); DescribeTarget(traceBuilder, bindingExpr.TargetElement, bindingExpr.TargetProperty); } else if (o is Binding) { Binding binding = o as Binding; if (binding.Path != null) traceBuilder.AppendFormat("Path={0}", binding.Path.Path ); else if (binding.XPath != null) traceBuilder.AppendFormat("XPath={0}", binding.XPath ); else traceBuilder.Append("(no path)"); } else if (o is BindingExpressionBase) { BindingExpressionBase beb = o as BindingExpressionBase; DescribeTarget(traceBuilder, beb.TargetElement, beb.TargetProperty); } else if (o is DependencyObject) { DescribeSourceObject(traceBuilder, o); } else { traceBuilder.AppendFormat("'{0}'", AvTrace.ToStringHelper(o)); } } ////// Produces a string that describes a source object: /// e.g. element in a Binding Path, DataItem in BindingExpression, ContextElement /// /// description will be appended to this builder /// a source object (e.g. element in a Binding Path, DataItem in BindingExpression, ContextElement) ///a string that describes the object static public void DescribeSourceObject(AvTraceBuilder traceBuilder, object o) { if (o == null) { traceBuilder.Append("null"); } else { FrameworkElement fe = o as FrameworkElement; if (fe != null) { traceBuilder.AppendFormat("'{0}' (Name='{1}')", fe.GetType().Name, fe.Name); } else { traceBuilder.AppendFormat("'{0}' (HashCode={1})", o.GetType().Name, o.GetHashCode()); } } } ////// static public string DescribeSourceObject(object o) { AvTraceBuilder atb = new AvTraceBuilder(null); DescribeSourceObject(atb, o); return atb.ToString(); } ////// Produces a string that describes TargetElement and TargetProperty /// /// description will be appended to this builder /// TargetElement /// TargetProperty ///a string that describes TargetElement and TargetProperty static public void DescribeTarget(AvTraceBuilder traceBuilder, DependencyObject targetElement, DependencyProperty targetProperty) { if (targetElement != null) { traceBuilder.Append("target element is "); DescribeSourceObject(traceBuilder, targetElement); if (targetProperty != null) { traceBuilder.Append("; "); } } if (targetProperty != null) { traceBuilder.AppendFormat("target property is '{0}' (type '{1}')", targetProperty.Name, targetProperty.PropertyType.Name); } } ////// static public string DescribeTarget(DependencyObject targetElement, DependencyProperty targetProperty) { AvTraceBuilder atb = new AvTraceBuilder(null); DescribeTarget(atb, targetElement, targetProperty); return atb.ToString(); } static public string Identify(object o) { if (o == null) return ""; Type type = o.GetType(); if (type.IsPrimitive || type.IsEnum) return Format("'{0}'", o); string s = o as String; if (s != null) return Format("'{0}'", AvTrace.AntiFormat(s)); NamedObject n = o as NamedObject; if (n != null) return AvTrace.AntiFormat(n.ToString()); ICollection ic = o as ICollection; if (ic != null) return Format("{0} (hash={1} Count={2})", type.Name, AvTrace.GetHashCodeHelper(o), ic.Count); return Format("{0} (hash={1})", type.Name, AvTrace.GetHashCodeHelper(o)); } static public string IdentifyWeakEvent(Type type) { const string suffix = "EventManager"; string name = type.Name; if (name.EndsWith(suffix, StringComparison.Ordinal)) { name = name.Substring(0, name.Length - suffix.Length); } return name; } static public string IdentifyAccessor(object accessor) { DependencyProperty dp = accessor as DependencyProperty; if (dp != null) return Format("{0}({1})", dp.GetType().Name, dp.Name); PropertyInfo pi = accessor as PropertyInfo;; if (pi != null) return Format("{0}({1})", pi.GetType().Name, pi.Name); PropertyDescriptor pd = accessor as PropertyDescriptor;; if (pd != null) return Format("{0}({1})", pd.GetType().Name, pd.Name); return Identify(accessor); } static public string IdentifyException(Exception ex) { if (ex == null) return " "; return Format("{0} ({1})", ex.GetType().Name, AvTrace.AntiFormat(ex.Message)); } static string Format(string format, params object[] args) { return String.Format(TypeConverterHelper.InvariantEnglishUS, format, args); } } } // 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
- CompressedStack.cs
- QilPatternFactory.cs
- RelationshipNavigation.cs
- PackageProperties.cs
- MetafileHeader.cs
- SingleObjectCollection.cs
- EdmScalarPropertyAttribute.cs
- DocumentAutomationPeer.cs
- Pool.cs
- DummyDataSource.cs
- WebServiceHost.cs
- MatcherBuilder.cs
- Normalization.cs
- ExpressionBinding.cs
- PermissionListSet.cs
- CompositeScriptReference.cs
- MenuItem.cs
- RNGCryptoServiceProvider.cs
- ExitEventArgs.cs
- SafeArrayRankMismatchException.cs
- CmsInterop.cs
- MulticastOption.cs
- XmlQueryCardinality.cs
- CacheSection.cs
- __TransparentProxy.cs
- Crc32.cs
- FormViewPageEventArgs.cs
- ConfigurationManagerInternal.cs
- ResourceCategoryAttribute.cs
- DataObjectSettingDataEventArgs.cs
- DetailsViewPagerRow.cs
- ChannelServices.cs
- EventQueueState.cs
- _DigestClient.cs
- TypeUtils.cs
- ToolStripMenuItem.cs
- Converter.cs
- FunctionUpdateCommand.cs
- Ppl.cs
- DataGridViewRowStateChangedEventArgs.cs
- SecurityPolicyVersion.cs
- WmlSelectionListAdapter.cs
- IDQuery.cs
- ProcessingInstructionAction.cs
- XmlSerializationGeneratedCode.cs
- DatagridviewDisplayedBandsData.cs
- StorageScalarPropertyMapping.cs
- BinaryUtilClasses.cs
- CommandEventArgs.cs
- GradientBrush.cs
- DataFieldConverter.cs
- RegexGroup.cs
- Container.cs
- ExpressionWriter.cs
- AnnouncementClient.cs
- XmlMemberMapping.cs
- WebPartChrome.cs
- DataTemplate.cs
- GridItemProviderWrapper.cs
- DrawingGroup.cs
- StorageConditionPropertyMapping.cs
- HeaderUtility.cs
- KeyGestureValueSerializer.cs
- EditorZoneBase.cs
- MatchingStyle.cs
- VisualStyleTypesAndProperties.cs
- OdbcEnvironmentHandle.cs
- baseshape.cs
- xmlglyphRunInfo.cs
- WebPartConnectionsDisconnectVerb.cs
- ReliableOutputConnection.cs
- CommandDevice.cs
- CssTextWriter.cs
- CodeCatchClauseCollection.cs
- ParagraphResult.cs
- SectionInformation.cs
- FragmentQueryProcessor.cs
- WebPartTracker.cs
- EdmComplexPropertyAttribute.cs
- EventRecord.cs
- odbcmetadatacolumnnames.cs
- Empty.cs
- DataRowView.cs
- StylusTip.cs
- Header.cs
- BinaryKeyIdentifierClause.cs
- AsyncStreamReader.cs
- DataFormats.cs
- InputLanguageProfileNotifySink.cs
- MetadataItemCollectionFactory.cs
- WinCategoryAttribute.cs
- WindowsListBox.cs
- BamlRecordHelper.cs
- DynamicResourceExtensionConverter.cs
- RegexGroup.cs
- TypeSystem.cs
- LocalsItemDescription.cs
- HttpCapabilitiesSectionHandler.cs
- PatternMatchRules.cs
- FilterEventArgs.cs