Beispiel #1
0
        private bool InternalCanImport(XmlSchemaSet schemas, ICollection <XmlQualifiedName>?typeNames, ICollection <XmlSchemaElement>?elements)
        {
            DataContractSet?oldValue = (_dataContractSet == null) ? null : new DataContractSet(_dataContractSet);

            try
            {
                if (elements != null)
                {
                    DataContractSet.ImportSchemaSet(schemas, elements, ImportXmlDataType);
                }
                else
                {
                    DataContractSet.ImportSchemaSet(schemas, typeNames, ImportXmlDataType);
                }
                return(true);
            }
            catch (InvalidDataContractException)
            {
                _dataContractSet = oldValue;
                return(false);
            }
            catch (Exception ex)
            {
                if (Fx.IsFatal(ex))
                {
                    throw;
                }

                _dataContractSet = oldValue;
                throw;
            }
        }
Beispiel #2
0
        private IList <XmlQualifiedName>?InternalImport(XmlSchemaSet schemas, ICollection <XmlQualifiedName>?typeNames, ICollection <XmlSchemaElement>?elements)
        {
            DataContractSet?         oldValue         = (_dataContractSet == null) ? null : new DataContractSet(_dataContractSet);
            IList <XmlQualifiedName>?elementTypeNames = null;

            try
            {
                if (elements != null)
                {
                    elementTypeNames = DataContractSet.ImportSchemaSet(schemas, elements, ImportXmlDataType);
                }
                else
                {
                    DataContractSet.ImportSchemaSet(schemas, typeNames, ImportXmlDataType);
                }

                CodeExporter codeExporter = new CodeExporter(DataContractSet, Options, CodeCompileUnit);
                codeExporter.Export();

                return(elementTypeNames);
            }
            catch (Exception ex)
            {
                if (Fx.IsFatal(ex))
                {
                    throw;
                }

                _dataContractSet = oldValue;
                throw;
            }
        }
Beispiel #3
0
        public void Export(Type type)
        {
            if (type == null)
            {
                throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentNullException(nameof(type)));
            }

            TraceExportBegin();

            DataContractSet oldValue = (_dataContractSet == null) ? null : new DataContractSet(_dataContractSet);

            try
            {
                AddType(type);
                Export();
            }
            catch (Exception ex)
            {
                if (DiagnosticUtility.IsFatal(ex))
                {
                    throw;
                }
                _dataContractSet = oldValue;
                TraceExportError(ex);
                throw;
            }
            TraceExportEnd();
        }
Beispiel #4
0
        public bool CanExport(Type type)
        {
            if (type == null)
            {
                throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentNullException(nameof(type)));
            }

            DataContractSet oldValue = (_dataContractSet == null) ? null : new DataContractSet(_dataContractSet);

            try
            {
                AddType(type);
                AddKnownTypes();
                return(true);
            }
            catch (InvalidDataContractException)
            {
                _dataContractSet = oldValue;
                return(false);
            }
            catch (Exception ex)
            {
                if (DiagnosticUtility.IsFatal(ex))
                {
                    throw;
                }
                _dataContractSet = oldValue;
                TraceExportError(ex);
                throw;
            }
        }
 internal SchemaImporter(XmlSchemaSet schemas, ICollection<XmlQualifiedName> typeNames, ICollection<XmlSchemaElement> elements, XmlQualifiedName[] elementTypeNames, DataContractSet dataContractSet, bool importXmlDataType)
 {
     this.dataContractSet = dataContractSet;
     this.schemaSet = schemas;
     this.typeNames = typeNames;
     this.elements = elements;
     this.elementTypeNames = elementTypeNames;
     this.importXmlDataType = importXmlDataType;
 }
        public bool CanExport(ICollection <Assembly> assemblies)
        {
            if (assemblies == null)
            {
                throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentNullException(nameof(assemblies)));
            }

            DataContractSet oldValue = (_dataContractSet == null) ? null : new DataContractSet(_dataContractSet);

            try
            {
                foreach (Assembly assembly in assemblies)
                {
                    if (assembly == null)
                    {
                        throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentException(SR.Format(SR.CannotExportNullAssembly, nameof(assemblies))));
                    }

                    Type[] types = assembly.GetTypes();
                    for (int j = 0; j < types.Length; j++)
                    {
                        CheckAndAddType(types[j]);
                    }
                }
                AddKnownTypes();
                return(true);
            }
            catch (InvalidDataContractException)
            {
                _dataContractSet = oldValue;
                return(false);
            }
            catch (Exception ex)
            {
                if (DiagnosticUtility.IsFatal(ex))
                {
                    throw;
                }
                _dataContractSet = oldValue;
                TraceExportError(ex);
                throw;
            }
        }
Beispiel #7
0
        public void Export(ICollection <Assembly> assemblies)
        {
            if (assemblies == null)
            {
                throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentNullException("assemblies"));
            }

            TraceExportBegin();

            DataContractSet oldValue = (dataContractSet == null) ? null : new DataContractSet(dataContractSet);

            try
            {
                foreach (Assembly assembly in assemblies)
                {
                    if (assembly == null)
                    {
                        throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentException(SR.GetString(SR.CannotExportNullAssembly, "assemblies")));
                    }

                    Type[] types = assembly.GetTypes();
                    for (int j = 0; j < types.Length; j++)
                    {
                        CheckAndAddType(types[j]);
                    }
                }

                Export();
            }
            catch (Exception ex)
            {
                if (Fx.IsFatal(ex))
                {
                    throw;
                }
                dataContractSet = oldValue;
                TraceExportError(ex);
                throw;
            }
            TraceExportEnd();
        }
Beispiel #8
0
        internal DataContractSet(DataContractSet dataContractSet)
        {
            ArgumentNullException.ThrowIfNull(dataContractSet);

            //this.dataContractSurrogate = dataContractSet.dataContractSurrogate;
            _referencedTypes           = dataContractSet._referencedTypes;
            _referencedCollectionTypes = dataContractSet._referencedCollectionTypes;

            foreach (KeyValuePair <XmlQualifiedName, DataContract> pair in dataContractSet)
            {
                Add(pair.Key, pair.Value);
            }

            if (dataContractSet._processedContracts != null)
            {
                foreach (KeyValuePair <DataContract, object> pair in dataContractSet._processedContracts)
                {
                    ProcessedContracts.Add(pair.Key, pair.Value);
                }
            }
        }
 internal DataContractSet(DataContractSet dataContractSet)
 {
     if (dataContractSet == null)
     {
         throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentNullException("dataContractSet"));
     }
     this.dataContractSurrogate     = dataContractSet.dataContractSurrogate;
     this.referencedTypes           = dataContractSet.referencedTypes;
     this.referencedCollectionTypes = dataContractSet.referencedCollectionTypes;
     foreach (KeyValuePair <XmlQualifiedName, DataContract> pair in dataContractSet)
     {
         this.Add(pair.Key, pair.Value);
     }
     if (dataContractSet.processedContracts != null)
     {
         foreach (KeyValuePair <DataContract, object> pair2 in dataContractSet.processedContracts)
         {
             this.ProcessedContracts.Add(pair2.Key, pair2.Value);
         }
     }
 }
 internal DataContractSet(DataContractSet dataContractSet)
 {
     if (dataContractSet == null)
     {
         throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentNullException("dataContractSet"));
     }
     this.dataContractSurrogate = dataContractSet.dataContractSurrogate;
     this.referencedTypes = dataContractSet.referencedTypes;
     this.referencedCollectionTypes = dataContractSet.referencedCollectionTypes;
     foreach (KeyValuePair<XmlQualifiedName, DataContract> pair in dataContractSet)
     {
         this.Add(pair.Key, pair.Value);
     }
     if (dataContractSet.processedContracts != null)
     {
         foreach (KeyValuePair<DataContract, object> pair2 in dataContractSet.processedContracts)
         {
             this.ProcessedContracts.Add(pair2.Key, pair2.Value);
         }
     }
 }
 internal void Export()
 {
     try
     {
         // Remove this if we decide to publish serialization schema at well-known location
         ExportSerializationSchema();
         foreach (KeyValuePair<XmlQualifiedName, DataContract> pair in dataContractSet)
         {
             DataContract dataContract = pair.Value;
             if (!dataContractSet.IsContractProcessed(dataContract))
             {
                 ExportDataContract(dataContract);
                 dataContractSet.SetContractProcessed(dataContract);
             }
         }
     }
     finally
     {
         xmlDoc = null;
         dataContractSet = null;
     }
 }
Beispiel #12
0
 internal void Export()
 {
     try
     {
         // Remove this if we decide to publish serialization schema at well-known location
         ExportSerializationSchema();
         foreach (KeyValuePair <XmlQualifiedName, DataContract> pair in _dataContractSet)
         {
             DataContract dataContract = pair.Value;
             if (!_dataContractSet.IsContractProcessed(dataContract))
             {
                 ExportDataContract(dataContract);
                 _dataContractSet.SetContractProcessed(dataContract);
             }
         }
     }
     finally
     {
         _xmlDoc          = null;
         _dataContractSet = null;
     }
 }
Beispiel #13
0
        public bool CanExport(ICollection <Type> types)
        {
            if (types == null)
            {
                throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentNullException("types"));
            }

            DataContractSet oldValue = (dataContractSet == null) ? null : new DataContractSet(dataContractSet);

            try
            {
                foreach (Type type in types)
                {
                    if (type == null)
                    {
                        throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentException(SR.GetString(SR.CannotExportNullType, "types")));
                    }
                    AddType(type);
                }
                AddKnownTypes();
                return(true);
            }
            catch (InvalidDataContractException)
            {
                dataContractSet = oldValue;
                return(false);
            }
            catch (Exception ex)
            {
                if (Fx.IsFatal(ex))
                {
                    throw;
                }
                dataContractSet = oldValue;
                TraceExportError(ex);
                throw;
            }
        }
        private void InternalImport(XmlSchemaSet schemas, ICollection <XmlQualifiedName> typeNames, ICollection <XmlSchemaElement> elements, XmlQualifiedName[] elementTypeNames /*filled on return*/)
        {
            DataContractSet oldValue = (_dataContractSet == null) ? null : new DataContractSet(_dataContractSet);

            try
            {
                SchemaImporter schemaImporter = new SchemaImporter(schemas, typeNames, elements, elementTypeNames /*filled on return*/, DataContractSet, ImportXmlDataType);
                schemaImporter.Import();

                CodeExporter codeExporter = new CodeExporter(DataContractSet, Options, GetCodeCompileUnit());
                codeExporter.Export();
            }
            catch (Exception ex)
            {
                if (DiagnosticUtility.IsFatal(ex))
                {
                    throw;
                }
                _dataContractSet = oldValue;
                TraceImportError(ex);
                throw;
            }
        }
Beispiel #15
0
        internal DataContractSet(DataContractSet dataContractSet)
        {
            if (dataContractSet == null)
            {
                throw /*System.Runtime.Serialization.*/ DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentNullException("dataContractSet"));
            }

            _dataContractSurrogate     = dataContractSet._dataContractSurrogate;
            _referencedTypes           = dataContractSet._referencedTypes;
            _referencedCollectionTypes = dataContractSet._referencedCollectionTypes;

            foreach (KeyValuePair <XmlQualifiedName, DataContract> pair in dataContractSet)
            {
                Add(pair.Key, pair.Value);
            }

            if (dataContractSet._processedContracts != null)
            {
                foreach (KeyValuePair <DataContract, object> pair in dataContractSet._processedContracts)
                {
                    ProcessedContracts.Add(pair.Key, pair.Value);
                }
            }
        }
        public void Export(ICollection <Type> types)
        {
            if (types == null)
            {
                throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentNullException(nameof(types)));
            }

            TraceExportBegin();

            DataContractSet oldValue = (_dataContractSet == null) ? null : new DataContractSet(_dataContractSet);

            try
            {
                foreach (Type type in types)
                {
                    if (type == null)
                    {
                        throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentException(SR.Format(SR.CannotExportNullType, nameof(types))));
                    }
                    AddType(type);
                }

                Export();
            }
            catch (Exception ex)
            {
                if (DiagnosticUtility.IsFatal(ex))
                {
                    throw;
                }
                _dataContractSet = oldValue;
                TraceExportError(ex);
                throw;
            }
            TraceExportEnd();
        }
 internal void Export()
 {
     try
     {
         this.ExportSerializationSchema();
         foreach (KeyValuePair<XmlQualifiedName, DataContract> pair in this.dataContractSet)
         {
             DataContract dataContract = pair.Value;
             if (!this.dataContractSet.IsContractProcessed(dataContract))
             {
                 this.ExportDataContract(dataContract);
                 this.dataContractSet.SetContractProcessed(dataContract);
             }
         }
     }
     finally
     {
         this.xmlDoc = null;
         this.dataContractSet = null;
     }
 }
Beispiel #18
0
 internal SchemaExporter(XmlSchemaSet schemas, DataContractSet dataContractSet)
 {
     _schemas         = schemas;
     _dataContractSet = dataContractSet;
 }
 bool InternalCanImport(XmlSchemaSet schemas, ICollection<XmlQualifiedName> typeNames, ICollection<XmlSchemaElement> elements, XmlQualifiedName[] elementTypeNames)
 {
     DataContractSet oldValue = (dataContractSet == null) ? null : new DataContractSet(dataContractSet);
     try
     {
         SchemaImporter schemaImporter = new SchemaImporter(schemas, typeNames, elements, elementTypeNames, DataContractSet, ImportXmlDataType);
         schemaImporter.Import();
         return true;
     }
     catch (InvalidDataContractException)
     {
         dataContractSet = oldValue;
         return false;
     }
     catch (Exception ex)
     {
         if (Fx.IsFatal(ex))
         {
             throw;
         }
         dataContractSet = oldValue;
         TraceImportError(ex);
         throw;
     }
 }
        void InternalImport(XmlSchemaSet schemas, ICollection<XmlQualifiedName> typeNames, ICollection<XmlSchemaElement> elements, XmlQualifiedName[] elementTypeNames/*filled on return*/)
        {
            if (DiagnosticUtility.ShouldTraceInformation)
            {
                TraceUtility.Trace(TraceEventType.Information, TraceCode.XsdImportBegin, SR.GetString(SR.TraceCodeXsdImportBegin));
            }

            DataContractSet oldValue = (dataContractSet == null) ? null : new DataContractSet(dataContractSet);
            try
            {
                SchemaImporter schemaImporter = new SchemaImporter(schemas, typeNames, elements, elementTypeNames/*filled on return*/, DataContractSet, ImportXmlDataType);
                schemaImporter.Import();

                CodeExporter codeExporter = new CodeExporter(DataContractSet, Options, GetCodeCompileUnit());
                codeExporter.Export();
            }
            catch (Exception ex)
            {
                if (Fx.IsFatal(ex))
                {
                    throw;
                }
                dataContractSet = oldValue;
                TraceImportError(ex);
                throw;
            }

            if (DiagnosticUtility.ShouldTraceInformation)
            {
                TraceUtility.Trace(TraceEventType.Information, TraceCode.XsdImportEnd, SR.GetString(SR.TraceCodeXsdImportEnd));
            }
        }
Beispiel #21
0
        private void ExportCollectionDataContract(CollectionDataContract collectionDataContract, XmlSchema schema)
        {
            XmlSchemaComplexType type = new XmlSchemaComplexType();

            type.Name = collectionDataContract.StableName.Name;
            schema.Items.Add(type);
            XmlElement?genericInfoElement = null, isDictionaryElement = null;

            if (collectionDataContract.UnderlyingType.IsGenericType && CollectionDataContract.IsCollectionDataContract(collectionDataContract.UnderlyingType))
            {
                genericInfoElement = ExportGenericInfo(collectionDataContract.UnderlyingType, Globals.GenericTypeLocalName, Globals.SerializationNamespace);
            }
            if (collectionDataContract.IsDictionary)
            {
                isDictionaryElement = ExportIsDictionary();
            }
            type.Annotation = GetSchemaAnnotation(isDictionaryElement, genericInfoElement, ExportSurrogateData(collectionDataContract));

            XmlSchemaSequence rootSequence = new XmlSchemaSequence();

            XmlSchemaElement element = new XmlSchemaElement();

            element.Name            = collectionDataContract.ItemName;
            element.MinOccurs       = 0;
            element.MaxOccursString = Globals.OccursUnbounded;
            if (collectionDataContract.IsDictionary)
            {
                ClassDataContract    keyValueContract = (collectionDataContract.ItemContract as ClassDataContract) !;
                XmlSchemaComplexType keyValueType     = new XmlSchemaComplexType();
                XmlSchemaSequence    keyValueSequence = new XmlSchemaSequence();
                foreach (DataMember dataMember in keyValueContract.Members !)
                {
                    XmlSchemaElement keyValueElement = new XmlSchemaElement();
                    keyValueElement.Name = dataMember.Name;
                    SetElementType(keyValueElement, DataContractSet.GetMemberTypeDataContract(dataMember), schema);
                    SchemaHelper.AddElementForm(keyValueElement, schema);
                    if (dataMember.IsNullable)
                    {
                        keyValueElement.IsNillable = true;
                    }
                    keyValueElement.Annotation = GetSchemaAnnotation(ExportSurrogateData(dataMember));
                    keyValueSequence.Items.Add(keyValueElement);
                }
                keyValueType.Particle = keyValueSequence;
                element.SchemaType    = keyValueType;
            }
            else
            {
                if (collectionDataContract.IsItemTypeNullable)
                {
                    element.IsNillable = true;
                }
                DataContract itemContract = DataContractSet.GetItemTypeDataContract(collectionDataContract);
                SetElementType(element, itemContract, schema);
            }
            SchemaHelper.AddElementForm(element, schema);
            rootSequence.Items.Add(element);

            type.Particle = rootSequence;

            if (collectionDataContract.IsReference)
            {
                AddReferenceAttributes(type.Attributes, schema);
            }
        }
Beispiel #22
0
        private void ExportClassDataContract(ClassDataContract classDataContract, XmlSchema schema)
        {
            XmlSchemaComplexType type = new XmlSchemaComplexType();

            type.Name = classDataContract.StableName.Name;
            schema.Items.Add(type);
            XmlElement?genericInfoElement = null;

            if (classDataContract.UnderlyingType.IsGenericType)
            {
                genericInfoElement = ExportGenericInfo(classDataContract.UnderlyingType, Globals.GenericTypeLocalName, Globals.SerializationNamespace);
            }

            XmlSchemaSequence rootSequence = new XmlSchemaSequence();

            for (int i = 0; i < classDataContract.Members !.Count; i++)
            {
                DataMember dataMember = classDataContract.Members[i];

                XmlSchemaElement element = new XmlSchemaElement();
                element.Name = dataMember.Name;
                XmlElement?  actualTypeElement  = null;
                DataContract memberTypeContract = DataContractSet.GetMemberTypeDataContract(dataMember);
                if (CheckIfMemberHasConflict(dataMember))
                {
                    element.SchemaTypeName = AnytypeQualifiedName;
                    actualTypeElement      = ExportActualType(memberTypeContract.StableName);
                    SchemaHelper.AddSchemaImport(memberTypeContract.StableName.Namespace, schema);
                }
                else
                {
                    SetElementType(element, memberTypeContract, schema);
                }
                SchemaHelper.AddElementForm(element, schema);
                if (dataMember.IsNullable)
                {
                    element.IsNillable = true;
                }
                if (!dataMember.IsRequired)
                {
                    element.MinOccurs = 0;
                }

                element.Annotation = GetSchemaAnnotation(actualTypeElement, ExportSurrogateData(dataMember), ExportEmitDefaultValue(dataMember));
                rootSequence.Items.Add(element);
            }

            XmlElement?isValueTypeElement = null;

            if (classDataContract.BaseContract != null)
            {
                XmlSchemaComplexContentExtension extension = CreateTypeContent(type, classDataContract.BaseContract.StableName, schema);
                extension.Particle = rootSequence;
                if (classDataContract.IsReference && !classDataContract.BaseContract.IsReference)
                {
                    AddReferenceAttributes(extension.Attributes, schema);
                }
            }
            else
            {
                type.Particle = rootSequence;
                if (classDataContract.IsValueType)
                {
                    isValueTypeElement = GetAnnotationMarkup(IsValueTypeName, XmlConvert.ToString(classDataContract.IsValueType), schema);
                }
                if (classDataContract.IsReference)
                {
                    AddReferenceAttributes(type.Attributes, schema);
                }
            }
            type.Annotation = GetSchemaAnnotation(genericInfoElement, ExportSurrogateData(classDataContract), isValueTypeElement);
        }
        //Returns warnings
        public IList <string> GenerateCode(IList <Assembly> assemblies)
        {
            if (assemblies == null)
            {
                throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentNullException(nameof(assemblies)));
            }
            List <string> warnings = new List <string>();

            DataContractSet oldValue = (dataContractSet == null) ? null : new DataContractSet(dataContractSet);

            try
            {
                for (int i = 0; i < assemblies.Count; i++)
                {
                    Assembly assembly = assemblies[i];
                    if (assembly == null)
                    {
                        throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentException(SR.Format(SR.CannotExportNullAssembly, "assemblies")));
                    }

                    Type[] types = assembly.GetTypes();
                    for (int j = 0; j < types.Length; j++)
                    {
                        try
                        {
                            CheckAndAddType(types[j]);
                        }
                        catch (Exception ex)
                        {
                            warnings.Add("Error on exporting Type " + DataContract.GetClrTypeFullName(types[j]) + ". " + ex.Message);
                        }
                    }
                }

                foreach (KeyValuePair <XmlQualifiedName, DataContract> pair in dataContractSet)
                {
                    DataContract dataContract = pair.Value;
                    if (dataContract is ClassDataContract)
                    {
                        try
                        {
                            XmlFormatClassWriterDelegate writerMethod = ((ClassDataContract)dataContract).XmlFormatWriterDelegate;
                            XmlFormatClassReaderDelegate readerMethod = ((ClassDataContract)dataContract).XmlFormatReaderDelegate;
                        }
                        catch (Exception ex)
                        {
                            warnings.Add("Error on exporting Type " + dataContract.UnderlyingType + ". " + ex.Message);
                        }
                    }
                    else if (dataContract is CollectionDataContract)
                    {
                        try
                        {
                            XmlFormatCollectionWriterDelegate writerMethod = ((CollectionDataContract)dataContract).XmlFormatWriterDelegate;
                            XmlFormatCollectionReaderDelegate readerMethod = ((CollectionDataContract)dataContract).XmlFormatReaderDelegate;
                        }
                        catch (Exception ex)
                        {
                            warnings.Add("Error on exporting Type " + dataContract.UnderlyingType + ". " + ex.Message);
                        }
                    }
                }
                return(warnings);
            }
            catch (Exception ex)
            {
                if (DiagnosticUtility.IsFatal(ex))
                {
                    throw;
                }
                dataContractSet = oldValue;
                TraceExportError(ex);
                throw;
            }
        }
        //Returns warnings
        public IList<string> GenerateCode(IList<Assembly> assemblies) 
        {
            if (assemblies == null)
                throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentNullException("assemblies"));
            List<string> warnings = new List<string>();

            DataContractSet oldValue = (dataContractSet == null) ? null : new DataContractSet(dataContractSet);
            try
            {
                for (int i=0; i < assemblies.Count; i++)
                {
                    Assembly assembly = assemblies[i];
                    if (assembly == null)
                        throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentException(SR.GetString(SR.CannotExportNullAssembly, "assemblies")));

                    Type[] types = assembly.GetTypes();
                    for (int j=0; j < types.Length; j++)
                    {
                        try
                        {
                            CheckAndAddType(types[j]);
                        }
                        catch (Exception ex)
                        {
                            warnings.Add("Error on exporting Type " + DataContract.GetClrTypeFullName(types[j]) + ". " + ex.Message);
                        }
                        
                    }
                }

                foreach (KeyValuePair<XmlQualifiedName, DataContract> pair in dataContractSet)
                {
                    DataContract dataContract = pair.Value;
                    if (dataContract is ClassDataContract)
                    {
                        try
                        {
                            XmlFormatClassWriterDelegate writerMethod = ((ClassDataContract)dataContract).XmlFormatWriterDelegate;
                            XmlFormatClassReaderDelegate readerMethod = ((ClassDataContract)dataContract).XmlFormatReaderDelegate;
                        }
                        catch (Exception ex)
                        {
                            warnings.Add("Error on exporting Type " + dataContract.UnderlyingType + ". " + ex.Message);
                        }
                    }
                    else if (dataContract is CollectionDataContract)
                    {
                        try
                        {
                            XmlFormatCollectionWriterDelegate writerMethod = ((CollectionDataContract)dataContract).XmlFormatWriterDelegate;
                            XmlFormatCollectionReaderDelegate readerMethod = ((CollectionDataContract)dataContract).XmlFormatReaderDelegate;
                        }
                        catch (Exception ex)
                        {
                            warnings.Add("Error on exporting Type " + dataContract.UnderlyingType + ". " + ex.Message);
                        }
                    }
                }
                return warnings;
            }
            catch (Exception ex)
            {
                if (Fx.IsFatal(ex))
                {
                    throw;
                }
                dataContractSet = oldValue;
                TraceExportError(ex);
                throw;
            }
        }
        public bool CanExport(Type type)
        {
            if (type == null)
                throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentNullException("type"));

            DataContractSet oldValue = (dataContractSet == null) ? null : new DataContractSet(dataContractSet);
            try
            {
                AddType(type);
                AddKnownTypes();
                return true;
            }
            catch (InvalidDataContractException)
            {
                dataContractSet = oldValue;
                return false;
            }
            catch (Exception ex)
            {
                if (Fx.IsFatal(ex))
                {
                    throw;
                }
                dataContractSet = oldValue;
                TraceExportError(ex);
                throw;
            }
        }
        public bool CanExport(ICollection<Type> types)
        {
            if (types == null)
                throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentNullException("types"));

            DataContractSet oldValue = (dataContractSet == null) ? null : new DataContractSet(dataContractSet);
            try
            {
                foreach (Type type in types)
                {
                    if (type == null)
                        throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentException(SR.GetString(SR.CannotExportNullType, "types")));
                    AddType(type);
                }
                AddKnownTypes();
                return true;
            }
            catch (InvalidDataContractException)
            {
                dataContractSet = oldValue;
                return false;
            }
            catch (Exception ex)
            {
                if (Fx.IsFatal(ex))
                {
                    throw;
                }
                dataContractSet = oldValue;
                TraceExportError(ex);
                throw;
            }
        }
 internal SchemaExporter(XmlSchemaSet schemas, DataContractSet dataContractSet)
 {
     this.schemas = schemas;
     this.dataContractSet = dataContractSet;
 }
Beispiel #28
0
 internal DataContractSet(DataContractSet dataContractSet !!)
 {
        internal CodeExporter(DataContractSet dataContractSet, ImportOptions options, CodeCompileUnit codeCompileUnit)
        {
            this.dataContractSet = dataContractSet;
            this.codeCompileUnit = codeCompileUnit;
            AddReferencedAssembly(Assembly.GetExecutingAssembly());
            this.options = options;
            this.namespaces = new Dictionary<string, string>();
            this.clrNamespaces = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);

            // Update namespace tables for DataContract(s) that are already processed
            foreach (KeyValuePair<XmlQualifiedName, DataContract> pair in dataContractSet)
            {
                DataContract dataContract = pair.Value;
                if (!(dataContract.IsBuiltInDataContract || dataContract is CollectionDataContract))
                {
                    ContractCodeDomInfo contractCodeDomInfo = GetContractCodeDomInfo(dataContract);
                    if (contractCodeDomInfo.IsProcessed && !contractCodeDomInfo.UsesWildcardNamespace)
                    {
                        string clrNamespace = contractCodeDomInfo.ClrNamespace;
                        if (clrNamespace != null && !this.clrNamespaces.ContainsKey(clrNamespace))
                        {
                            this.clrNamespaces.Add(clrNamespace, dataContract.StableName.Namespace);
                            this.namespaces.Add(dataContract.StableName.Namespace, clrNamespace);
                        }
                    }
                }
            }

            // Copy options.Namespaces to namespace tables
            if (this.options != null)
            {
                foreach (KeyValuePair<string, string> pair in options.Namespaces)
                {
                    string dataContractNamespace = pair.Key;
                    string clrNamespace = pair.Value;
                    if (clrNamespace == null)
                        clrNamespace = String.Empty;

                    string currentDataContractNamespace;
                    if (this.clrNamespaces.TryGetValue(clrNamespace, out currentDataContractNamespace))
                    {
                        if (dataContractNamespace != currentDataContractNamespace)
                            throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.GetString(SR.CLRNamespaceMappedMultipleTimes, currentDataContractNamespace, dataContractNamespace, clrNamespace)));
                    }
                    else
                        this.clrNamespaces.Add(clrNamespace, dataContractNamespace);

                    string currentClrNamespace;
                    if (this.namespaces.TryGetValue(dataContractNamespace, out currentClrNamespace))
                    {
                        if (clrNamespace != currentClrNamespace)
                        {
                            this.namespaces.Remove(dataContractNamespace);
                            this.namespaces.Add(dataContractNamespace, clrNamespace);
                        }
                    }
                    else
                        this.namespaces.Add(dataContractNamespace, clrNamespace);
                }
            }

            // Update namespace tables for pre-existing namespaces in CodeCompileUnit
            foreach (CodeNamespace codeNS in codeCompileUnit.Namespaces)
            {
                string ns = codeNS.Name ?? string.Empty;
                if (!this.clrNamespaces.ContainsKey(ns))
                {
                    this.clrNamespaces.Add(ns, null);
                }
                if (ns.Length == 0)
                {
                    foreach (CodeTypeDeclaration codeTypeDecl in codeNS.Types)
                    {
                        AddGlobalTypeName(codeTypeDecl.Name);
                    }
                }
            }

        }
        public void Export(ICollection<Type> types)
        {
            if (types == null)
                throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentNullException(nameof(types)));

            TraceExportBegin();

            DataContractSet oldValue = (_dataContractSet == null) ? null : new DataContractSet(_dataContractSet);
            try
            {
                foreach (Type type in types)
                {
                    if (type == null)
                        throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentException(SR.Format(SR.CannotExportNullType, nameof(types))));
                    AddType(type);
                }

                Export();
            }
            catch (Exception ex)
            {
                if (DiagnosticUtility.IsFatal(ex))
                {
                    throw;
                }
                _dataContractSet = oldValue;
                TraceExportError(ex);
                throw;
            }
            TraceExportEnd();
        }
        public void Export(ICollection<Assembly> assemblies)
        {
            if (assemblies == null)
                throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentNullException("assemblies"));

            TraceExportBegin();

            DataContractSet oldValue = (dataContractSet == null) ? null : new DataContractSet(dataContractSet);
            try
            {
                foreach (Assembly assembly in assemblies)
                {
                    if (assembly == null)
                        throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentException(SR.GetString(SR.CannotExportNullAssembly, "assemblies")));

                    Type[] types = assembly.GetTypes();
                    for (int j = 0; j < types.Length; j++)
                        CheckAndAddType(types[j]);
                }

                Export();
            }
            catch (Exception ex)
            {
                if (Fx.IsFatal(ex))
                {
                    throw;
                }
                dataContractSet = oldValue;
                TraceExportError(ex);
                throw;
            }
            TraceExportEnd();
        }
        public void Export(Type type)
        {
            if (type == null)
                throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentNullException("type"));

            TraceExportBegin();

            DataContractSet oldValue = (dataContractSet == null) ? null : new DataContractSet(dataContractSet);
            try
            {
                AddType(type);
                Export();
            }
            catch (Exception ex)
            {
                if (Fx.IsFatal(ex))
                {
                    throw;
                }
                dataContractSet = oldValue;
                TraceExportError(ex);
                throw;
            }
            TraceExportEnd();
        }
        public bool CanExport(ICollection<Assembly> assemblies)
        {
            if (assemblies == null)
                throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentNullException(nameof(assemblies)));

            DataContractSet oldValue = (_dataContractSet == null) ? null : new DataContractSet(_dataContractSet);
            try
            {
                foreach (Assembly assembly in assemblies)
                {
                    if (assembly == null)
                        throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentException(SR.Format(SR.CannotExportNullAssembly, nameof(assemblies))));

                    Type[] types = assembly.GetTypes();
                    for (int j = 0; j < types.Length; j++)
                        CheckAndAddType(types[j]);
                }
                AddKnownTypes();
                return true;
            }
            catch (InvalidDataContractException)
            {
                _dataContractSet = oldValue;
                return false;
            }
            catch (Exception ex)
            {
                if (DiagnosticUtility.IsFatal(ex))
                {
                    throw;
                }
                _dataContractSet = oldValue;
                TraceExportError(ex);
                throw;
            }
        }
 internal CodeExporter(DataContractSet dataContractSet, ImportOptions options, CodeCompileUnit codeCompileUnit)
 {
     this.dataContractSet = dataContractSet;
     this.codeCompileUnit = codeCompileUnit;
     this.AddReferencedAssembly(Assembly.GetExecutingAssembly());
     this.options = options;
     this.namespaces = new Dictionary<string, string>();
     this.clrNamespaces = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
     foreach (KeyValuePair<XmlQualifiedName, DataContract> pair in dataContractSet)
     {
         DataContract dataContract = pair.Value;
         if (!dataContract.IsBuiltInDataContract && !(dataContract is CollectionDataContract))
         {
             ContractCodeDomInfo contractCodeDomInfo = this.GetContractCodeDomInfo(dataContract);
             if (contractCodeDomInfo.IsProcessed && !contractCodeDomInfo.UsesWildcardNamespace)
             {
                 string clrNamespace = contractCodeDomInfo.ClrNamespace;
                 if ((clrNamespace != null) && !this.clrNamespaces.ContainsKey(clrNamespace))
                 {
                     this.clrNamespaces.Add(clrNamespace, dataContract.StableName.Namespace);
                     this.namespaces.Add(dataContract.StableName.Namespace, clrNamespace);
                 }
             }
         }
     }
     if (this.options != null)
     {
         foreach (KeyValuePair<string, string> pair2 in options.Namespaces)
         {
             string str4;
             string str5;
             string key = pair2.Key;
             string str3 = pair2.Value;
             if (str3 == null)
             {
                 str3 = string.Empty;
             }
             if (this.clrNamespaces.TryGetValue(str3, out str4))
             {
                 if (key != str4)
                 {
                     throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(System.Runtime.Serialization.SR.GetString("CLRNamespaceMappedMultipleTimes", new object[] { str4, key, str3 })));
                 }
             }
             else
             {
                 this.clrNamespaces.Add(str3, key);
             }
             if (this.namespaces.TryGetValue(key, out str5))
             {
                 if (str3 != str5)
                 {
                     this.namespaces.Remove(key);
                     this.namespaces.Add(key, str3);
                 }
             }
             else
             {
                 this.namespaces.Add(key, str3);
             }
         }
     }
     foreach (CodeNamespace namespace2 in codeCompileUnit.Namespaces)
     {
         string str6 = namespace2.Name ?? string.Empty;
         if (!this.clrNamespaces.ContainsKey(str6))
         {
             this.clrNamespaces.Add(str6, null);
         }
         if (str6.Length == 0)
         {
             foreach (CodeTypeDeclaration declaration in namespace2.Types)
             {
                 this.AddGlobalTypeName(declaration.Name);
             }
         }
     }
 }
Beispiel #35
0
        //XmlDocument _xmlDoc;

        internal SchemaExporter(XmlSchemaSet schemas, DataContractSet dataContractSet)
        {
            _schemas = schemas;
            _dataContractSet = dataContractSet;
        }