private void ClearDocument()
 {
     if (this._documentType != null)
     {
         base.LoaderHost.RemoveService(typeof(CodeTypeDeclaration));
         this._documentType = null;
         this._documentNamespace = null;
         this._documentCompileUnit = null;
         this._rootSerializer = null;
         this._typeSerializer = null;
     }
 }
 private void EnsureDocument(IDesignerSerializationManager manager)
 {
     if (this._documentCompileUnit == null)
     {
         this._documentCompileUnit = this.Parse();
         if (this._documentCompileUnit == null)
         {
             Exception exception = new NotSupportedException(System.Design.SR.GetString("CodeDomDesignerLoaderNoLanguageSupport")) {
                 HelpLink = "CodeDomDesignerLoaderNoLanguageSupport"
             };
             throw exception;
         }
     }
     if (this._documentType == null)
     {
         ArrayList list = null;
         bool flag = true;
         if (this._documentCompileUnit.UserData[typeof(InvalidOperationException)] != null)
         {
             InvalidOperationException exception2 = this._documentCompileUnit.UserData[typeof(InvalidOperationException)] as InvalidOperationException;
             if (exception2 != null)
             {
                 this._documentCompileUnit = null;
                 throw exception2;
             }
         }
         foreach (CodeNamespace namespace2 in this._documentCompileUnit.Namespaces)
         {
             foreach (CodeTypeDeclaration declaration in namespace2.Types)
             {
                 Type componentType = null;
                 foreach (CodeTypeReference reference in declaration.BaseTypes)
                 {
                     Type type = base.LoaderHost.GetType(CodeDomSerializerBase.GetTypeNameFromCodeTypeReference(manager, reference));
                     if ((type != null) && !type.IsInterface)
                     {
                         componentType = type;
                         break;
                     }
                     if (type == null)
                     {
                         if (list == null)
                         {
                             list = new ArrayList();
                         }
                         list.Add(System.Design.SR.GetString("CodeDomDesignerLoaderDocumentFailureTypeNotFound", new object[] { declaration.Name, reference.BaseType }));
                     }
                 }
                 if (componentType != null)
                 {
                     bool flag2 = false;
                     foreach (Attribute attribute in TypeDescriptor.GetAttributes(componentType))
                     {
                         if (attribute is RootDesignerSerializerAttribute)
                         {
                             RootDesignerSerializerAttribute attribute2 = (RootDesignerSerializerAttribute) attribute;
                             string serializerBaseTypeName = attribute2.SerializerBaseTypeName;
                             if ((serializerBaseTypeName != null) && (base.LoaderHost.GetType(serializerBaseTypeName) == typeof(CodeDomSerializer)))
                             {
                                 Type type3 = base.LoaderHost.GetType(attribute2.SerializerTypeName);
                                 if ((type3 != null) && (type3 != typeof(RootCodeDomSerializer)))
                                 {
                                     flag2 = true;
                                     if (!flag)
                                     {
                                         throw new InvalidOperationException(System.Design.SR.GetString("CodeDomDesignerLoaderSerializerTypeNotFirstType", new object[] { declaration.Name }));
                                     }
                                     this._rootSerializer = (CodeDomSerializer) Activator.CreateInstance(type3, BindingFlags.CreateInstance | BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance, null, null, null);
                                     break;
                                 }
                             }
                         }
                     }
                     if ((this._rootSerializer == null) && this.HasRootDesignerAttribute(componentType))
                     {
                         this._typeSerializer = manager.GetSerializer(componentType, typeof(TypeCodeDomSerializer)) as TypeCodeDomSerializer;
                         if (!flag && (this._typeSerializer != null))
                         {
                             this._typeSerializer = null;
                             this._documentCompileUnit = null;
                             throw new InvalidOperationException(System.Design.SR.GetString("CodeDomDesignerLoaderSerializerTypeNotFirstType", new object[] { declaration.Name }));
                         }
                     }
                     if ((this._rootSerializer == null) && (this._typeSerializer == null))
                     {
                         if (list == null)
                         {
                             list = new ArrayList();
                         }
                         if (flag2)
                         {
                             list.Add(System.Design.SR.GetString("CodeDomDesignerLoaderDocumentFailureTypeDesignerNotInstalled", new object[] { declaration.Name, componentType.FullName }));
                         }
                         else
                         {
                             list.Add(System.Design.SR.GetString("CodeDomDesignerLoaderDocumentFailureTypeNotDesignable", new object[] { declaration.Name, componentType.FullName }));
                         }
                     }
                 }
                 if ((this._rootSerializer != null) || (this._typeSerializer != null))
                 {
                     this._documentNamespace = namespace2;
                     this._documentType = declaration;
                     break;
                 }
                 flag = false;
             }
             if (this._documentType != null)
             {
                 break;
             }
         }
         if (this._documentType == null)
         {
             Exception exception3;
             this._documentCompileUnit = null;
             if (list != null)
             {
                 StringBuilder builder = new StringBuilder();
                 foreach (string str2 in list)
                 {
                     builder.Append("\r\n");
                     builder.Append(str2);
                 }
                 exception3 = new InvalidOperationException(System.Design.SR.GetString("CodeDomDesignerLoaderNoRootSerializerWithFailures", new object[] { builder.ToString() })) {
                     HelpLink = "CodeDomDesignerLoaderNoRootSerializer"
                 };
             }
             else
             {
                 exception3 = new InvalidOperationException(System.Design.SR.GetString("CodeDomDesignerLoaderNoRootSerializer")) {
                     HelpLink = "CodeDomDesignerLoaderNoRootSerializer"
                 };
             }
             throw exception3;
         }
         base.LoaderHost.AddService(typeof(CodeTypeDeclaration), this._documentType);
     }
     codemarkers.CodeMarker(CodeMarkerEvent.perfFXGetDocumentType);
 }