private void Initialize(XamlSchemaContext schemaContext, XamlSavedContext savedContext, XamlObjectWriterSettings settings)
 {
     this._inDispose = false;
     if (schemaContext == null)
     {
         throw new ArgumentNullException("schemaContext");
     }
     if ((savedContext != null) && (schemaContext != savedContext.SchemaContext))
     {
         throw new ArgumentException(System.Xaml.SR.Get("SavedContextSchemaContextMismatch"), "schemaContext");
     }
     if (settings != null)
     {
         this._afterBeginInitHandler = settings.AfterBeginInitHandler;
         this._beforePropertiesHandler = settings.BeforePropertiesHandler;
         this._afterPropertiesHandler = settings.AfterPropertiesHandler;
         this._afterEndInitHandler = settings.AfterEndInitHandler;
         this._xamlSetValueHandler = settings.XamlSetValueHandler;
         this._rootObjectInstance = settings.RootObjectInstance;
         this._skipDuplicatePropertyCheck = settings.SkipDuplicatePropertyCheck;
         this._skipProvideValueOnRoot = settings.SkipProvideValueOnRoot;
         this._preferUnconvertedDictionaryKeys = settings.PreferUnconvertedDictionaryKeys;
     }
     INameScope rootNameScope = (settings != null) ? settings.ExternalNameScope : null;
     XamlRuntime runtime = this.CreateRuntime(settings, schemaContext);
     if (savedContext != null)
     {
         this._context = new ObjectWriterContext(savedContext, settings, rootNameScope, runtime);
     }
     else
     {
         if (schemaContext == null)
         {
             throw this.WithLineInfo(new XamlInternalException());
         }
         this._context = new ObjectWriterContext(schemaContext, settings, rootNameScope, runtime);
         this._context.AddNamespacePrefix("xml", "http://www.w3.org/XML/1998/namespace");
     }
     this._context.IsInitializedCallback = this;
     this._deferringWriter = new DeferringWriter(this._context);
     this._rootNamescope = null;
 }
 protected override void Dispose(bool disposing)
 {
     try
     {
         this._inDispose = true;
         if (disposing && !base.IsDisposed)
         {
             if ((this._context.LiveDepth > 1) || (this._context.CurrentType != null))
             {
                 while (this._context.LiveDepth > 0)
                 {
                     if (this._context.CurrentProperty != null)
                     {
                         this.WriteEndMember();
                     }
                     this.WriteEndObject();
                 }
             }
             this._deferringWriter.Close();
             this._deferringWriter = null;
             this._context = null;
             this._afterBeginInitHandler = null;
             this._beforePropertiesHandler = null;
             this._afterPropertiesHandler = null;
             this._afterEndInitHandler = null;
         }
     }
     finally
     {
         base.Dispose(disposing);
         this._inDispose = false;
     }
 }