Code:
/ Dotnetfx_Win7_3.5.1 / Dotnetfx_Win7_3.5.1 / 3.5.1 / DEVDIV / depot / DevDiv / releases / Orcas / NetFXw7 / wpf / src / Framework / MS / Internal / Annotations / Serializer.cs / 1 / Serializer.cs
//------------------------------------------------------------------------------
//
//
// Copyright (C) Microsoft Corporation. All rights reserved.
//
//
// Description:
// Serializer performs the boot-strapping to call the public implementations
// of IXmlSerializable for the Annotation object model. This would normally
// be done by XmlSerializer but its slow and causes an assembly to be generated
// at runtime. API-wise third-parties can still use XmlSerializer but we
// choose not to for our purposes.
//
// History:
// 08/26/2004: rruiz: Added new serializer class.
//
//-----------------------------------------------------------------------------
using System;
using System.Diagnostics;
using System.Reflection;
using System.Windows.Annotations.Storage;
using System.Xml;
using System.Xml.Schema;
using System.Xml.Serialization;
using MS.Internal;
namespace MS.Internal.Annotations
{
///
/// Serializer class for Annotation object model. All entities
/// in the object model implement IXmlSerializable (or are
/// contained and serialized by an entity that does). This class
/// does the simple boot-strapping for serializing/deserializing
/// of the object model. This lets us get by without paying the
/// cost of XmlSerializer (which generates a run-time assembly).
///
internal class Serializer
{
///
/// Creates an instance of the serializer for the specified type.
/// We use the type to get the default constructor and the type's
/// element name and namespace. This constructor expects the
/// type to be attributed with XmlRootAttribute (as all serializable
/// classes in the object model are).
///
/// the type to be serialized by this instance
public Serializer(Type type)
{
Invariant.Assert(type != null);
// Find the XmlRootAttribute for the type
object[] attributes = type.GetCustomAttributes(false);
foreach (object obj in attributes)
{
_attribute = obj as XmlRootAttribute;
if (_attribute != null)
break;
}
Invariant.Assert(_attribute != null, "Internal Serializer used for a type with no XmlRootAttribute.");
// Get the default constructor for the type
_ctor = type.GetConstructor(new Type[0]);
}
///
/// Serializes the object to the specified XmlWriter.
///
/// writer to serialize to
/// object to serialize
public void Serialize(XmlWriter writer, object obj)
{
Invariant.Assert(writer != null && obj != null);
IXmlSerializable serializable = obj as IXmlSerializable;
Invariant.Assert(serializable != null, "Internal Serializer used for a type that isn't IXmlSerializable.");
writer.WriteStartElement(_attribute.ElementName, _attribute.Namespace);
serializable.WriteXml(writer);
writer.WriteEndElement();
}
///
/// Deserializes the next object from the reader. The
/// reader is expected to be positioned on a node that
/// can be deserialized into the type this serializer
/// was instantiated for.
///
/// reader to deserialize from
/// an instance of the type this serializer was instanted
/// for with values retrieved from the reader
public object Deserialize(XmlReader reader)
{
Invariant.Assert(reader != null);
IXmlSerializable serializable = (IXmlSerializable)_ctor.Invoke(new object[0]);
// If this is a brand-new stream we need to jump into it
if (reader.ReadState == ReadState.Initial)
{
reader.Read();
}
serializable.ReadXml(reader);
return serializable;
}
// XmlRootAttribute - specifies the ElementName and Namespace for
// the node to read/write
private XmlRootAttribute _attribute;
// Constructor used to create instances when deserializing
private ConstructorInfo _ctor;
}
}
// 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:
// Serializer performs the boot-strapping to call the public implementations
// of IXmlSerializable for the Annotation object model. This would normally
// be done by XmlSerializer but its slow and causes an assembly to be generated
// at runtime. API-wise third-parties can still use XmlSerializer but we
// choose not to for our purposes.
//
// History:
// 08/26/2004: rruiz: Added new serializer class.
//
//-----------------------------------------------------------------------------
using System;
using System.Diagnostics;
using System.Reflection;
using System.Windows.Annotations.Storage;
using System.Xml;
using System.Xml.Schema;
using System.Xml.Serialization;
using MS.Internal;
namespace MS.Internal.Annotations
{
///
/// Serializer class for Annotation object model. All entities
/// in the object model implement IXmlSerializable (or are
/// contained and serialized by an entity that does). This class
/// does the simple boot-strapping for serializing/deserializing
/// of the object model. This lets us get by without paying the
/// cost of XmlSerializer (which generates a run-time assembly).
///
internal class Serializer
{
///
/// Creates an instance of the serializer for the specified type.
/// We use the type to get the default constructor and the type's
/// element name and namespace. This constructor expects the
/// type to be attributed with XmlRootAttribute (as all serializable
/// classes in the object model are).
///
/// the type to be serialized by this instance
public Serializer(Type type)
{
Invariant.Assert(type != null);
// Find the XmlRootAttribute for the type
object[] attributes = type.GetCustomAttributes(false);
foreach (object obj in attributes)
{
_attribute = obj as XmlRootAttribute;
if (_attribute != null)
break;
}
Invariant.Assert(_attribute != null, "Internal Serializer used for a type with no XmlRootAttribute.");
// Get the default constructor for the type
_ctor = type.GetConstructor(new Type[0]);
}
///
/// Serializes the object to the specified XmlWriter.
///
/// writer to serialize to
/// object to serialize
public void Serialize(XmlWriter writer, object obj)
{
Invariant.Assert(writer != null && obj != null);
IXmlSerializable serializable = obj as IXmlSerializable;
Invariant.Assert(serializable != null, "Internal Serializer used for a type that isn't IXmlSerializable.");
writer.WriteStartElement(_attribute.ElementName, _attribute.Namespace);
serializable.WriteXml(writer);
writer.WriteEndElement();
}
///
/// Deserializes the next object from the reader. The
/// reader is expected to be positioned on a node that
/// can be deserialized into the type this serializer
/// was instantiated for.
///
/// reader to deserialize from
/// an instance of the type this serializer was instanted
/// for with values retrieved from the reader
public object Deserialize(XmlReader reader)
{
Invariant.Assert(reader != null);
IXmlSerializable serializable = (IXmlSerializable)_ctor.Invoke(new object[0]);
// If this is a brand-new stream we need to jump into it
if (reader.ReadState == ReadState.Initial)
{
reader.Read();
}
serializable.ReadXml(reader);
return serializable;
}
// XmlRootAttribute - specifies the ElementName and Namespace for
// the node to read/write
private XmlRootAttribute _attribute;
// Constructor used to create instances when deserializing
private ConstructorInfo _ctor;
}
}
// 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
- RSAOAEPKeyExchangeFormatter.cs
- ExpressionBindingCollection.cs
- ResourceReferenceKeyNotFoundException.cs
- XNodeSchemaApplier.cs
- ISCIIEncoding.cs
- updateconfighost.cs
- HtmlContainerControl.cs
- MimeFormImporter.cs
- PropVariant.cs
- DefaultTextStoreTextComposition.cs
- OleDbConnectionPoolGroupProviderInfo.cs
- SystemWebCachingSectionGroup.cs
- XmlCharCheckingReader.cs
- PageTextBox.cs
- ToolStripItemClickedEventArgs.cs
- KeyGestureValueSerializer.cs
- ReadOnlyDictionary.cs
- StaticDataManager.cs
- MultipartContentParser.cs
- WorkflowPageSetupDialog.cs
- TypeUsageBuilder.cs
- HeaderPanel.cs
- Grant.cs
- RelatedView.cs
- XmlQueryRuntime.cs
- Matrix3DConverter.cs
- TiffBitmapDecoder.cs
- DataGridRowHeaderAutomationPeer.cs
- AdornerHitTestResult.cs
- TypeUsage.cs
- DictionaryEditChange.cs
- WebPartChrome.cs
- PropertyDescriptorComparer.cs
- HostingEnvironment.cs
- ThumbButtonInfoCollection.cs
- PreservationFileReader.cs
- NavigatingCancelEventArgs.cs
- SourceExpressionException.cs
- RadialGradientBrush.cs
- ObsoleteAttribute.cs
- RadioButton.cs
- ConstNode.cs
- CompilerInfo.cs
- EventLogException.cs
- ManagementObject.cs
- WorkflowDurableInstance.cs
- GroupBox.cs
- CompositionTarget.cs
- FillErrorEventArgs.cs
- CellTreeNodeVisitors.cs
- StringUtil.cs
- FileIOPermission.cs
- XmlnsCompatibleWithAttribute.cs
- IMembershipProvider.cs
- BaseAddressElementCollection.cs
- TimeSpanMinutesConverter.cs
- MouseWheelEventArgs.cs
- ProxyElement.cs
- Compiler.cs
- ActivityInfo.cs
- DataBoundControlActionList.cs
- RowToFieldTransformer.cs
- View.cs
- StrokeCollectionDefaultValueFactory.cs
- CodeIdentifiers.cs
- WbmpConverter.cs
- AddressUtility.cs
- DataSourceCacheDurationConverter.cs
- DataGridViewCell.cs
- GenericEnumerator.cs
- WebPartManagerInternals.cs
- MsmqTransportBindingElement.cs
- VectorCollection.cs
- ViewValidator.cs
- ResourceProviderFactory.cs
- _AcceptOverlappedAsyncResult.cs
- HttpListenerResponse.cs
- PermissionSet.cs
- OleDbCommandBuilder.cs
- Boolean.cs
- EntityStoreSchemaFilterEntry.cs
- CultureInfo.cs
- OraclePermission.cs
- AttachedPropertyBrowsableForChildrenAttribute.cs
- ToolStripGripRenderEventArgs.cs
- EntityCommand.cs
- OrderPreservingPipeliningSpoolingTask.cs
- TrustLevelCollection.cs
- FlowLayoutPanel.cs
- SecurityPermission.cs
- SqlBuilder.cs
- RestHandlerFactory.cs
- SerializationAttributes.cs
- DataTableReader.cs
- CustomSignedXml.cs
- ImageButton.cs
- HtmlTableCell.cs
- OSEnvironmentHelper.cs
- Rectangle.cs
- WebHttpBehavior.cs