Code:
/ DotNET / DotNET / 8.0 / untmp / WIN_WINDOWS / lh_tools_devdiv_wpf / Windows / wcp / Print / Reach / Serialization / manager / MetroSerializationManager.cs / 1 / MetroSerializationManager.cs
/*++ Copyright (C) 2004- 2005 Microsoft Corporation All rights reserved. Module Name: MetroSerializationManager.cs Abstract: This file contains the definition of a Base class that controls the serialization processo of METRO into S0. It also includes a cache manager which helps in caching data about types while the tree of objects is being traversed to optimize the permormance of the serialization process Author: [....] ([....]) 1-December-2004 Revision History: --*/ using System; using System.Collections; using System.Collections.Specialized; using System.ComponentModel; using System.Diagnostics; using System.Reflection; using System.Xml; using System.IO; using System.Security; using System.Security.Permissions; using System.ComponentModel.Design.Serialization; using System.Windows.Xps.Packaging; using System.Windows.Documents; using System.Windows.Media; using System.Windows.Markup; using System.Windows.Xps.Serialization; using System.Windows.Xps; using System.Printing; namespace System.Windows.Xps.Serialization { ////// This class defines all necessary interfaces (with a stub /// implementation to some of them) that are necessary to provide /// serialization services for persisting an AVALON root object /// into a package. It glues together all necessary serializers and /// type converters for different type of objects to produce the correct /// serialized content in the package. /// public abstract class PackageSerializationManager : IDisposable { #region Constructors ////// Constructor to create and initialize the base /// PackageSerializationManager class. /// ////// Critical - creates a ContextStack which is link critical /// /// TreatAsSafe - All access to the context stack is marked Security Critical /// [SecurityCritical, SecurityTreatAsSafe] protected PackageSerializationManager( ) { _serializersCacheManager = new SerializersCacheManager(this); _graphContextStack = new ContextStack(); this._rootSerializableObjectContext = null; } #endregion Constructors #region Public Methods ////// Saves the given object instance to the underlying packaging /// as S0 representation /// /// /// object instance to serialize. /// public abstract void SaveAsXaml( Object serializedObject ); #endregion Public Methods #region IDisposable implementation void IDisposable.Dispose() { } #endregion IDisposable implementation #region Internal Methods ////// Returns the namespace for the object instance /// being serialized /// /// /// The Type of the object instance being serialized. /// internal abstract String GetXmlNSForType( Type objectType ); ////// Retrieves the serializer for a given object instance /// /// /// The object instance being serialized. /// internal virtual ReachSerializer GetSerializer( Object serializedObject ) { ReachSerializer reachSerializer = null; reachSerializer = _serializersCacheManager.GetSerializer(serializedObject); return reachSerializer; } ////// Retrieves the type of the serializer for a given object instance type. /// /// /// The type of the object instance being serialized. /// internal virtual Type GetSerializerType( Type objectType ) { Type serializerType = null; return serializerType; } ////// Retrieves the TypeConverter for a given object instance. /// /// /// The object instance being serialized. /// internal virtual TypeConverter GetTypeConverter ( Object serializedObject ) { if (serializedObject == null) { throw new ArgumentNullException("serializedObject"); } return TypeDescriptor.GetConverter(serializedObject.GetType()); } ////// Retrieves the TypeConverter for a given object instance type. /// /// /// The type of the object instance being serialized. /// internal virtual TypeConverter GetTypeConverter ( Type serializedObjectType ) { return TypeDescriptor.GetConverter(serializedObjectType); } ////// Retrieves the XmlWriter for a given type. /// /// /// The type of object that consequently dictates the type of /// writer. /// internal abstract XmlWriter AcquireXmlWriter( Type writerType ); ////// Releases the XmlWriter already retreived for a given type. /// /// /// The type of object that consequently dictates the type of /// writer. /// internal abstract void ReleaseXmlWriter( Type writerType ); ////// Retrieves the stream for a given resource type. This stream /// would be filled in by the resource data during the serialization /// process /// /// /// The type of resource that consequently dictates the type of /// stream. /// internal abstract XpsResourceStream AcquireResourceStream( Type resourceType ); ////// Retrieves the stream for a given resource type. This stream /// would be filled in by the resource data during the serialization /// process /// /// /// The type of resource that consequently dictates the type of /// stream. /// /// /// The ID of resource and that is used internally for caching /// and sharing resources across multiple parts. /// internal abstract XpsResourceStream AcquireResourceStream( Type resourceType, String resourceID ); ////// Releases the stream for a given resource type. Releasing a stream /// means that the stream would be flushed and committed to the underlying /// packaging infrasturcutre /// /// /// The type of resource that consequently dictates the type of /// part being flushed. /// internal abstract void ReleaseResourceStream( Type resourceType ); ////// Releases the stream for a given resource type. Releasing a stream /// means that the stream would be flushed and committed to the underlying /// packaging infrasturcutre /// /// /// The type of resource that consequently dictates the type of /// part being flushed. /// /// /// The ID of resource and that is used internally for caching /// and sharing resources across multiple parts. /// internal abstract void ReleaseResourceStream( Type resourceType, String resourceID ); internal abstract void AddRelationshipToCurrentPage( Uri targetUri, string relationshipName ); internal virtual bool CanSerializeDependencyProperty( Object serializableObject, TypeDependencyPropertyCache dependencyProperty ) { return true; } internal virtual bool CanSerializeClrProperty( Object serializableObject, TypePropertyCache property ) { return true; } #endregion Internal Methods #region Internal Properties internal abstract BasePackagingPolicy PackagingPolicy { get; } internal abstract XpsResourcePolicy ResourcePolicy { get; } ////// Queries or Sets the StackContext that hosts all /// the nodes within the graph of the serialized object /// ////// Critical - Returns a ContextStack which is link critical /// internal ContextStack GraphContextStack { [SecurityCritical] get { return _graphContextStack; } [SecurityCritical] set { _graphContextStack = value; } } ////// Queries the cache manager /// internal SerializersCacheManager CacheManager { get { return _serializersCacheManager; } } ////// Queries root object of the current serialization run. /// internal SerializableObjectContext RootSerializableObjectContext { get { return _rootSerializableObjectContext; } set { _rootSerializableObjectContext = value; } } ////// Queries the cache manager /// internal SerializersCacheManager SerializersCacheManager { get { return _serializersCacheManager; } } internal XmlLanguage Language { get { return _language; } set { _language = value; } } ////// Critical - job identifier is something we should be carefull giving out in PT /// TreatAsSafe - demands DefaultPrinting, not enabled in Partial Trust /// internal int JobIdentifier { [System.Drawing.Printing.PrintingPermission( System.Security.Permissions.SecurityAction.Demand, Level = System.Drawing.Printing.PrintingPermissionLevel.DefaultPrinting)] [SecurityCritical, SecurityTreatAsSafe] set { _jobIdentifier = value; } [System.Drawing.Printing.PrintingPermission( System.Security.Permissions.SecurityAction.Demand, Level = System.Drawing.Printing.PrintingPermissionLevel.DefaultPrinting)] [SecurityCritical, SecurityTreatAsSafe] get { return _jobIdentifier; } } #endregion Internal Properties #region Private Data Members private SerializersCacheManager _serializersCacheManager; ////// Critical - ContextStack is link critical /// This is private no one should be accessing this directly /// [SecurityCritical] private ContextStack _graphContextStack; private SerializableObjectContext _rootSerializableObjectContext; private XmlLanguage _language; ////// Critical - job identifier is something we should be carefull giving out in PT /// [SecurityCritical] private int _jobIdentifier; #endregion Private Data Members }; ////// /// public delegate void XpsSerializationPrintTicketRequiredEventHandler( object sender, XpsSerializationPrintTicketRequiredEventArgs e ); ////// This class is a cache repository for different items /// that couold be repeatedly used in the serialization /// process.This actually control the caching for the /// following : /// - Types /// - Serializers on Types /// - Type Converters on Types /// internal class SerializersCacheManager { #region Constructors ////// Constructor to create and initialize the base /// SerializersCacheManager class. /// /// /// The instance of the serilization manager for this current /// run that information is being cached for /// public SerializersCacheManager( PackageSerializationManager serializationManager ) { this._serializationManager = serializationManager; // // Allocate all necessary hashtables for storing // the cache information // _typesCacheTable = new Hashtable(20); _serializersTable = new Hashtable(20); _typesDependencyPropertiesCacheTable = new Hashtable(20); } #endregion Constructors #region Public Methods ////// Retrieves the serializer for a given object instance /// /// /// The object instance being serialized. /// public ReachSerializer GetSerializer( Object serializedObject ) { if (serializedObject == null) { throw new ArgumentNullException("serializedObject"); } ReachSerializer reachSerializer = null; TypeCacheItem cacheItem = GetTypeCacheItem(serializedObject); if(cacheItem != null) { Type serializerType = cacheItem.SerializerType; // // Instantiate the metro serializer based on this type // if(serializerType!=null) { reachSerializer = (ReachSerializer)_serializersTable[serializerType]; if (reachSerializer == null) { object[] args; args = new object[1]; args[0] = _serializationManager; reachSerializer = Activator. CreateInstance(serializerType, args) as ReachSerializer; if (reachSerializer == null) { throw new XpsSerializationException(ReachSR.Get(ReachSRID.ReachSerialization_UnableToInstantiateSerializer)); } _serializersTable[serializerType] = reachSerializer; } } } return reachSerializer; } #endregion Public Methods #region Internal Methods ////// Retrieves the serializable clr properties for a given object instance /// /// /// The object instance being serialized. /// internal TypePropertyCache[] GetClrSerializableProperties( Object serializableObject ) { TypeCacheItem item = GetTypeCacheItem(serializableObject); TypePropertyCache[] clrProperties = item.GetClrSerializableProperties(this); int[] serializableIndeces = new int[clrProperties.Length]; int serializablePropertiesIndex = 0; // // Not everything we get can be serializable, so we have to figure out which // ones are but checking if we can also serialize the value of the property // values would be added to the cache as well. // for(int indexInClrProperties = 0; indexInClrProperties < clrProperties.Length; indexInClrProperties++) { if(CanSerializeValue(serializableObject, clrProperties[indexInClrProperties]) && _serializationManager.CanSerializeClrProperty(serializableObject, clrProperties[indexInClrProperties])) { serializableIndeces[serializablePropertiesIndex++] = indexInClrProperties; } } TypePropertyCache[] clrSerializableProperties = new TypePropertyCache[serializablePropertiesIndex]; for(int indexInClrSerializableProperties = 0; indexInClrSerializableProperties < serializablePropertiesIndex; indexInClrSerializableProperties++) { TypePropertyCache propertyCache = clrProperties[serializableIndeces[indexInClrSerializableProperties]]; TypePropertyCache serializablePropertyCache = new TypePropertyCache(propertyCache.PropertyInfo, propertyCache.Visibility, propertyCache.SerializerTypeForProperty, propertyCache.TypeConverterForProperty, propertyCache.DefaultValueAttr, propertyCache.DesignerSerializationOptionsAttr); serializablePropertyCache.PropertyValue = propertyCache.PropertyValue; clrSerializableProperties[indexInClrSerializableProperties] = serializablePropertyCache; } // // Clear all set values // for(int indexInClrProperties = 0; indexInClrProperties < clrProperties.Length; indexInClrProperties++) { clrProperties[indexInClrProperties].PropertyValue = null; } return clrSerializableProperties; } ////// Retrieves the serializable dependency properties for a /// given object instance /// /// /// The object instance being serialized. /// internal TypeDependencyPropertyCache[] GetSerializableDependencyProperties( Object serializableObject ) { TypeDependencyPropertyCache[] serializableDependencyProperties = null; TypeDependencyPropertiesCacheItem item = GetTypeDependencyPropertiesCacheItem(serializableObject); if(item != null) { TypeDependencyPropertyCache[] dependencyProperties = item.GetSerializableDependencyProperties(); int[] serializableIndeces = new int[dependencyProperties.Length]; int serializablePropertiesIndex = 0; // // Not everything we get can be serializable, so we have to figure out which // ones are by checking if we can also serialize the value of the property // values would be added to the cache as well. // for(int indexInDependencyProperties = 0; indexInDependencyProperties < dependencyProperties.Length; indexInDependencyProperties++) { if(TypeDependencyPropertyCache. CanSerializeValue(serializableObject, dependencyProperties[indexInDependencyProperties]) && _serializationManager.CanSerializeDependencyProperty(serializableObject, dependencyProperties[indexInDependencyProperties])) { serializableIndeces[serializablePropertiesIndex++] = indexInDependencyProperties; } } serializableDependencyProperties = new TypeDependencyPropertyCache[serializablePropertiesIndex]; for(int indexInSerializableDependencyProperties = 0; indexInSerializableDependencyProperties < serializablePropertiesIndex; indexInSerializableDependencyProperties++) { TypeDependencyPropertyCache propertyCache = dependencyProperties[serializableIndeces[indexInSerializableDependencyProperties]]; TypeDependencyPropertyCache serializablePropertyCache = new TypeDependencyPropertyCache(propertyCache.MemberInfo, propertyCache.DependencyProperty, propertyCache.Visibility, propertyCache.SerializerTypeForProperty, propertyCache.TypeConverterForProperty, propertyCache.DefaultValueAttr, propertyCache.DesignerSerializationOptionsAttr); serializablePropertyCache.PropertyValue = propertyCache.PropertyValue; serializableDependencyProperties[indexInSerializableDependencyProperties] = serializablePropertyCache; } // // Clear all set values // for(int indexInDependencyProperties = 0; indexInDependencyProperties < dependencyProperties.Length; indexInDependencyProperties++) { dependencyProperties[indexInDependencyProperties].PropertyValue = null; } } return serializableDependencyProperties; } ////// Retrieves the Cache for a certain type. The idea here /// is that most of the information for a given type are /// cached up the first time the type is encountered and then /// reused in consequence type serializaiton. /// /// /// The object instance being serialized. /// internal TypeCacheItem GetTypeCacheItem( Object serializableObject ) { if(serializableObject == null) { throw new ArgumentNullException("serializableObject"); } Type type = serializableObject.GetType(); TypeCacheItem typeCacheItem = (TypeCacheItem)_typesCacheTable[type]; if(typeCacheItem == null) { // // This means that the type was not seen before // We have to create a new entry to that type // Type serializerType = _serializationManager.GetSerializerType(type); if(serializerType!=null) { typeCacheItem = new TypeCacheItem(type, serializerType); } else { // // if the Type does not have a type serializer, then // we should try getting the type converter for that // type // TypeConverter typeConverter = _serializationManager.GetTypeConverter(serializableObject); if(typeConverter != null) { typeCacheItem = new TypeCacheItem(type, typeConverter); } else { typeCacheItem = new TypeCacheItem(type); } } _typesCacheTable[type] = typeCacheItem; } return typeCacheItem; } ////// Retrieves the cached dependency properties for a given object instance. /// The dependency properties for any type are discovered only once and then /// they are reused from the cache later on. /// /// /// The object instance being serialized. /// internal TypeDependencyPropertiesCacheItem GetTypeDependencyPropertiesCacheItem( Object serializableObject ) { if(serializableObject == null) { throw new ArgumentNullException("serializableObject"); } Type type = serializableObject.GetType(); TypeDependencyPropertiesCacheItem cachedItem = (TypeDependencyPropertiesCacheItem)_typesDependencyPropertiesCacheTable[type]; if(cachedItem == null) { // // This means that the type was not seen before // We have to create a new entry to that type // DependencyObject objectAsDependencyObject = serializableObject as DependencyObject; if (objectAsDependencyObject != null) { // // First we have to figure out if this dependency // object has any dependency properties that can be // serializable and this has to happen before creating // any cache // DependencyPropertyList list = new DependencyPropertyList(1); for(LocalValueEnumerator localValues = objectAsDependencyObject.GetLocalValueEnumerator(); localValues.MoveNext();) { DependencyProperty dependencyProperty = localValues.Current.Property; list.Add(dependencyProperty); } if(list.Count > 0) { int numOfSerializableDependencyProperties = 0; TypeDependencyPropertyCache[] dependencyPropertiesCache = new TypeDependencyPropertyCache[list.Count]; for (int indexInDependencyPropertyList=0; indexInDependencyPropertyList0) { TypeDependencyPropertyCache[] serializableDependencyPropertiesCache = new TypeDependencyPropertyCache[numOfSerializableDependencyProperties]; for(int indexInSerializableProperties = 0; indexInSerializableProperties < numOfSerializableDependencyProperties; indexInSerializableProperties++) { serializableDependencyPropertiesCache[indexInSerializableProperties] = dependencyPropertiesCache[indexInSerializableProperties]; } cachedItem = new TypeDependencyPropertiesCacheItem(type, serializableDependencyPropertiesCache); _typesDependencyPropertiesCacheTable[type] = cachedItem; } } } } return cachedItem; } /// /// Retrieves the Cache for a certain type. The idea here /// is that most of the information for a given type are /// cached up the first time the type is encountered and then /// reused in consequence type serializaiton. /// /// /// The type of the object instance being serialized. /// internal TypeCacheItem GetTypeCacheItem( Type serializableObjectType ) { if(serializableObjectType == null) { throw new ArgumentNullException("serializableObjectType"); } TypeCacheItem typeCacheItem = (TypeCacheItem)_typesCacheTable[serializableObjectType]; if(typeCacheItem == null) { // // This means that the type was not seen before // We have to create a new entry to that type // Type serializerType = _serializationManager.GetSerializerType(serializableObjectType); if(serializerType!=null) { typeCacheItem = new TypeCacheItem(serializableObjectType, serializerType); } else { // // if the Type does not have a type serializer, then // we should try getting the type converter for that // type // TypeConverter typeConverter = _serializationManager.GetTypeConverter(serializableObjectType); if(typeConverter != null) { typeCacheItem = new TypeCacheItem(serializableObjectType, typeConverter); } else { typeCacheItem = new TypeCacheItem(serializableObjectType); } } _typesCacheTable[serializableObjectType] = typeCacheItem; } return typeCacheItem; } #endregion Internal Methods #region Internal Properties ////// Queries the SerializationManager for which this /// cache is being used /// internal PackageSerializationManager SerializationManger { get { return _serializationManager; } } #endregion Internal Properties #region Private Methods ////// This function makes the following checks /// 1. If the property is readonly it will not be serialized /// unless ShouldSerialize{PropertyName} or /// DesignerSerializationVisibility.Content override this behavior /// 2. If there is no DefaultValue attribute it will always be serialized /// unless ShouldSerialize{PropertyName} overrides this behavior /// 3. If there is a DefaultValue attribute it will be serialized if the /// current property value does not equal the specified default. /// private bool CanSerializeValue( object serializableObject, TypePropertyCache propertyCache ) { bool canSerializeValue = false; // // For readonly properties check for DesignerSerializationVisibility.Content // bool isReadOnly = !propertyCache.PropertyInfo.CanWrite; if ((isReadOnly && propertyCache.Visibility == DesignerSerializationVisibility.Content) || propertyCache.DefaultValueAttr == null) { // // Populate the property value in this data structure // propertyCache.PropertyValue = propertyCache.PropertyInfo.GetValue(serializableObject, null); canSerializeValue = true; } else { // // Populate the property value in this data structure // as it is required to evaluate the default value // propertyCache.PropertyValue = propertyCache.PropertyInfo.GetValue(serializableObject, null); // For Clr properties with a DefaultValueAttribute // check if the current value equals the default canSerializeValue = !object.Equals(propertyCache.DefaultValueAttr.Value, propertyCache.PropertyValue); if(!canSerializeValue) { propertyCache.PropertyValue = null; } } return canSerializeValue; } #endregion Private Methods #region Private Data Members private PackageSerializationManager _serializationManager; // // The following is being cached for future reference and to // optimize performance // 1. The Types and their clr properties // 2. The Types and their corresponding dependency properties. Since, // not all types have dependency properties, that is why I am // caching this in a separate table // 3. All Serializers that have been used so far. This makes instantiating // a new instance of a required serializer much faster // private IDictionary _typesCacheTable; private IDictionary _serializersTable; private IDictionary _typesDependencyPropertiesCacheTable; #endregion Private Data Members }; ////// /// public enum SerializationState { ////// /// Normal, ////// /// Stop }; } // 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
- ObjectStateManagerMetadata.cs
- DataGridViewBindingCompleteEventArgs.cs
- BaseAppDomainProtocolHandler.cs
- SafeBitVector32.cs
- PlatformCulture.cs
- MenuEventArgs.cs
- DataControlImageButton.cs
- ActiveXSite.cs
- ButtonColumn.cs
- AuthenticationException.cs
- XPathNodeList.cs
- BuildProvidersCompiler.cs
- ToolStripAdornerWindowService.cs
- MouseOverProperty.cs
- ObjectListShowCommandsEventArgs.cs
- Profiler.cs
- DataProtection.cs
- HandleRef.cs
- BuildProvider.cs
- Columns.cs
- XmlUtilWriter.cs
- RegexCode.cs
- XmlDataProvider.cs
- DesignerAutoFormatCollection.cs
- XhtmlBasicListAdapter.cs
- JsonReaderDelegator.cs
- PropertyPath.cs
- DataIdProcessor.cs
- Opcode.cs
- UxThemeWrapper.cs
- ScriptDescriptor.cs
- DesignerActionVerbItem.cs
- ConfigXmlWhitespace.cs
- CodeEventReferenceExpression.cs
- SplineKeyFrames.cs
- PointAnimationUsingKeyFrames.cs
- XmlSchemaObject.cs
- ThicknessAnimation.cs
- MediaContext.cs
- XmlSchemaComplexContentExtension.cs
- AuthenticationService.cs
- ScrollContentPresenter.cs
- SourceCollection.cs
- DataTableCollection.cs
- Parameter.cs
- WindowsListViewItemCheckBox.cs
- ToolboxComponentsCreatedEventArgs.cs
- UserControlParser.cs
- BaseCollection.cs
- FtpCachePolicyElement.cs
- control.ime.cs
- EntityDataSourceContainerNameConverter.cs
- ModifierKeysConverter.cs
- GroupLabel.cs
- DomNameTable.cs
- RuntimeIdentifierPropertyAttribute.cs
- Metafile.cs
- MULTI_QI.cs
- AppDomain.cs
- SqlDataSourceStatusEventArgs.cs
- sqlser.cs
- SchemaAttDef.cs
- ThreadAttributes.cs
- ImpersonateTokenRef.cs
- Formatter.cs
- BindingCompleteEventArgs.cs
- HttpSessionStateWrapper.cs
- FullTextBreakpoint.cs
- ToolStripItemTextRenderEventArgs.cs
- TargetParameterCountException.cs
- SplitterPanel.cs
- Emitter.cs
- TextDpi.cs
- LogWriteRestartAreaAsyncResult.cs
- XmlElementAttributes.cs
- SectionXmlInfo.cs
- ILGenerator.cs
- ResourceCategoryAttribute.cs
- NameValueConfigurationCollection.cs
- PriorityChain.cs
- ScriptReferenceBase.cs
- Timer.cs
- ScriptResourceHandler.cs
- Codec.cs
- RootProfilePropertySettingsCollection.cs
- WebBrowserBase.cs
- PipeStream.cs
- TextParagraph.cs
- DocumentViewerConstants.cs
- MD5CryptoServiceProvider.cs
- DataServiceEntityAttribute.cs
- RegistryDataKey.cs
- AttributeSetAction.cs
- ConnectionPoint.cs
- DataBindingExpressionBuilder.cs
- LinearGradientBrush.cs
- DeflateStream.cs
- LocationUpdates.cs
- NamedPipeChannelListener.cs
- ToolStripManager.cs