Exemplo n.º 1
0
        private void ExportEnumDataContract(EnumDataContract enumDataContract, XmlSchema schema)
        {
            XmlSchemaSimpleType type = new XmlSchemaSimpleType();

            type.Name = enumDataContract.StableName.Name;
            // https://github.com/dotnet/runtime/issues/41448 - enumDataContract.BaseContractName is always null, but this method is not reachable
            Debug.Assert(enumDataContract.BaseContractName != null, "BaseContractName is always null, but this method is not reachable. Suppressing compiler error.");
            XmlElement?actualTypeElement = (enumDataContract.BaseContractName == DefaultEnumBaseTypeName) ? null : ExportActualType(enumDataContract.BaseContractName);

            type.Annotation = GetSchemaAnnotation(actualTypeElement, ExportSurrogateData(enumDataContract));
            schema.Items.Add(type);

            XmlSchemaSimpleTypeRestriction restriction = new XmlSchemaSimpleTypeRestriction();

            restriction.BaseTypeName = StringQualifiedName;
            SchemaHelper.AddSchemaImport(enumDataContract.BaseContractName.Namespace, schema);
            if (enumDataContract.Values != null)
            {
                for (int i = 0; i < enumDataContract.Values.Count; i++)
                {
                    XmlSchemaEnumerationFacet facet = new XmlSchemaEnumerationFacet();
                    facet.Value = enumDataContract.Members[i].Name;
                    if (enumDataContract.Values[i] != GetDefaultEnumValue(enumDataContract.IsFlags, i))
                    {
                        facet.Annotation = GetSchemaAnnotation(EnumerationValueAnnotationName, enumDataContract.GetStringFromEnumValue(enumDataContract.Values[i]), schema);
                    }
                    restriction.Facets.Add(facet);
                }
            }
            if (enumDataContract.IsFlags)
            {
                XmlSchemaSimpleTypeList list          = new XmlSchemaSimpleTypeList();
                XmlSchemaSimpleType     anonymousType = new XmlSchemaSimpleType();
                anonymousType.Content = restriction;
                list.ItemType         = anonymousType;
                type.Content          = list;
            }
            else
            {
                type.Content = restriction;
            }
        }
Exemplo n.º 2
0
        private void ExportEnumDataContract(EnumDataContract enumDataContract, XmlSchema schema)
        {
            XmlSchemaSimpleType type = new XmlSchemaSimpleType();

            type.Name = enumDataContract.StableName.Name;
            XmlElement actualTypeElement = (enumDataContract.BaseContractName == DefaultEnumBaseTypeName) ? null : ExportActualType(enumDataContract.BaseContractName);

            type.Annotation = GetSchemaAnnotation(actualTypeElement, ExportSurrogateData(enumDataContract));
            schema.Items.Add(type);

            XmlSchemaSimpleTypeRestriction restriction = new XmlSchemaSimpleTypeRestriction();

            restriction.BaseTypeName = StringQualifiedName;
            SchemaHelper.AddSchemaImport(enumDataContract.BaseContractName.Namespace, schema);
            if (enumDataContract.Values != null)
            {
                for (int i = 0; i < enumDataContract.Values.Count; i++)
                {
                    XmlSchemaEnumerationFacet facet = new XmlSchemaEnumerationFacet();
                    facet.Value = enumDataContract.Members[i].Name;
                    if (enumDataContract.Values[i] != GetDefaultEnumValue(enumDataContract.IsFlags, i))
                    {
                        facet.Annotation = GetSchemaAnnotation(EnumerationValueAnnotationName, enumDataContract.GetStringFromEnumValue(enumDataContract.Values[i]), schema);
                    }
                    restriction.Facets.Add(facet);
                }
            }
            if (enumDataContract.IsFlags)
            {
                XmlSchemaSimpleTypeList list          = new XmlSchemaSimpleTypeList();
                XmlSchemaSimpleType     anonymousType = new XmlSchemaSimpleType();
                anonymousType.Content = restriction;
                list.ItemType         = anonymousType;
                type.Content          = list;
            }
            else
            {
                type.Content = restriction;
            }
        }
Exemplo n.º 3
0
        internal override bool Equals(object other, Dictionary <DataContractPairKey, object> checkedContracts)
        {
            if (IsEqualOrChecked(other, checkedContracts))
            {
                return(true);
            }

            if (base.Equals(other, null))
            {
                EnumDataContract dataContract = other as EnumDataContract;
                if (dataContract != null)
                {
                    if (Members.Count != dataContract.Members.Count || Values.Count != dataContract.Values.Count)
                    {
                        return(false);
                    }
                    string[] memberNames1 = new string[Members.Count], memberNames2 = new string[Members.Count];
                    for (int i = 0; i < Members.Count; i++)
                    {
                        memberNames1[i] = Members[i].Name;
                        memberNames2[i] = dataContract.Members[i].Name;
                    }
                    Array.Sort(memberNames1);
                    Array.Sort(memberNames2);
                    for (int i = 0; i < Members.Count; i++)
                    {
                        if (memberNames1[i] != memberNames2[i])
                        {
                            return(false);
                        }
                    }

                    return(IsFlags == dataContract.IsFlags);
                }
            }
            return(false);
        }
Exemplo n.º 4
0
 internal override bool Equals(object other, Dictionary <DataContractPairKey, object> checkedContracts)
 {
     if (base.IsEqualOrChecked(other, checkedContracts))
     {
         return(true);
     }
     if (base.Equals(other, null))
     {
         EnumDataContract contract = other as EnumDataContract;
         if (contract != null)
         {
             if ((this.Members.Count != contract.Members.Count) || (this.Values.Count != contract.Values.Count))
             {
                 return(false);
             }
             string[] array     = new string[this.Members.Count];
             string[] strArray2 = new string[this.Members.Count];
             for (int i = 0; i < this.Members.Count; i++)
             {
                 array[i]     = this.Members[i].Name;
                 strArray2[i] = contract.Members[i].Name;
             }
             Array.Sort <string>(array);
             Array.Sort <string>(strArray2);
             for (int j = 0; j < this.Members.Count; j++)
             {
                 if (array[j] != strArray2[j])
                 {
                     return(false);
                 }
             }
             return(this.IsFlags == contract.IsFlags);
         }
     }
     return(false);
 }
 private static DataContract CreateDataContract(int id, RuntimeTypeHandle typeHandle, Type type)
 {
     lock (createDataContractLock)
     {
         DataContract dataContract = dataContractCache[id];
         if (dataContract == null)
         {
             if (type == null)
             {
                 type = Type.GetTypeFromHandle(typeHandle);
             }
             type = DataContract.UnwrapNullableType(type);
             type = GetDataContractAdapterType(type);
             dataContract = GetBuiltInDataContract(type);
             if (dataContract == null)
             {
                 if (type.IsArray)
                 {
                     dataContract = new CollectionDataContract(type);
                 }
                 else if (type.IsEnum)
                 {
                     dataContract = new EnumDataContract(type);
                 }
                 else if (type.IsGenericParameter)
                 {
                     dataContract = new GenericParameterDataContract(type);
                 }
                 else if (Globals.TypeOfIXmlSerializable.IsAssignableFrom(type))
                 {
                     dataContract = new XmlDataContract(type);
                 }
                 else
                 {
                     if (type.IsPointer)
                     {
                         type = Globals.TypeOfReflectionPointer;
                     }
                     if (!CollectionDataContract.TryCreate(type, out dataContract))
                     {
                         if ((!type.IsSerializable && !type.IsDefined(Globals.TypeOfDataContractAttribute, false)) && !ClassDataContract.IsNonAttributedTypeValidForSerialization(type))
                         {
                             ThrowInvalidDataContractException(System.Runtime.Serialization.SR.GetString("TypeNotSerializable", new object[] { type }), type);
                         }
                         dataContract = new ClassDataContract(type);
                     }
                 }
             }
         }
         dataContractCache[id] = dataContract;
         return dataContract;
     }
 }
 private void ExportEnumDataContract(EnumDataContract enumDataContract, ContractCodeDomInfo contractCodeDomInfo)
 {
     this.GenerateType(enumDataContract, contractCodeDomInfo);
     if (!contractCodeDomInfo.ReferencedTypeExists)
     {
         CodeTypeDeclaration typeDeclaration = contractCodeDomInfo.TypeDeclaration;
         typeDeclaration.IsEnum = true;
         typeDeclaration.BaseTypes.Add(EnumDataContract.GetBaseType(enumDataContract.BaseContractName));
         if (enumDataContract.IsFlags)
         {
             typeDeclaration.CustomAttributes.Add(new CodeAttributeDeclaration(DataContract.GetClrTypeFullName(Globals.TypeOfFlagsAttribute)));
             this.AddImportStatement(Globals.TypeOfFlagsAttribute.Namespace, contractCodeDomInfo.CodeNamespace);
         }
         string nameForAttribute = this.GetNameForAttribute(enumDataContract.StableName.Name);
         CodeAttributeDeclaration declaration2 = new CodeAttributeDeclaration(DataContract.GetClrTypeFullName(Globals.TypeOfDataContractAttribute));
         declaration2.Arguments.Add(new CodeAttributeArgument("Name", new CodePrimitiveExpression(nameForAttribute)));
         declaration2.Arguments.Add(new CodeAttributeArgument("Namespace", new CodePrimitiveExpression(enumDataContract.StableName.Namespace)));
         typeDeclaration.CustomAttributes.Add(declaration2);
         this.AddImportStatement(Globals.TypeOfDataContractAttribute.Namespace, contractCodeDomInfo.CodeNamespace);
         if (enumDataContract.Members != null)
         {
             for (int i = 0; i < enumDataContract.Members.Count; i++)
             {
                 string name = enumDataContract.Members[i].Name;
                 long num2 = enumDataContract.Values[i];
                 CodeMemberField field = new CodeMemberField();
                 if (enumDataContract.IsULong)
                 {
                     field.InitExpression = new CodeSnippetExpression(enumDataContract.GetStringFromEnumValue(num2));
                 }
                 else
                 {
                     field.InitExpression = new CodePrimitiveExpression(num2);
                 }
                 field.Name = this.GetMemberName(name, contractCodeDomInfo);
                 CodeAttributeDeclaration declaration3 = new CodeAttributeDeclaration(DataContract.GetClrTypeFullName(Globals.TypeOfEnumMemberAttribute));
                 if (field.Name != name)
                 {
                     declaration3.Arguments.Add(new CodeAttributeArgument("Value", new CodePrimitiveExpression(name)));
                 }
                 field.CustomAttributes.Add(declaration3);
                 typeDeclaration.Members.Add(field);
             }
         }
     }
 }
Exemplo n.º 7
0
        void ExportEnumDataContract(EnumDataContract enumDataContract, ContractCodeDomInfo contractCodeDomInfo)
        {
            GenerateType(enumDataContract, contractCodeDomInfo);
            if (contractCodeDomInfo.ReferencedTypeExists)
                return;

            CodeTypeDeclaration type = contractCodeDomInfo.TypeDeclaration;
            type.IsEnum = true;
            type.BaseTypes.Add(EnumDataContract.GetBaseType(enumDataContract.BaseContractName));
            if (enumDataContract.IsFlags)
            {
                type.CustomAttributes.Add(new CodeAttributeDeclaration(DataContract.GetClrTypeFullName(Globals.TypeOfFlagsAttribute)));
                AddImportStatement(Globals.TypeOfFlagsAttribute.Namespace, contractCodeDomInfo.CodeNamespace);
            }

            string dataContractName = GetNameForAttribute(enumDataContract.StableName.Name);
            CodeAttributeDeclaration dataContractAttribute = new CodeAttributeDeclaration(DataContract.GetClrTypeFullName(Globals.TypeOfDataContractAttribute));
            dataContractAttribute.Arguments.Add(new CodeAttributeArgument(Globals.NameProperty, new CodePrimitiveExpression(dataContractName)));
            dataContractAttribute.Arguments.Add(new CodeAttributeArgument(Globals.NamespaceProperty, new CodePrimitiveExpression(enumDataContract.StableName.Namespace)));
            type.CustomAttributes.Add(dataContractAttribute);
            AddImportStatement(Globals.TypeOfDataContractAttribute.Namespace, contractCodeDomInfo.CodeNamespace);

            if (enumDataContract.Members != null)
            {
                for (int i = 0; i < enumDataContract.Members.Count; i++)
                {
                    string stringValue = enumDataContract.Members[i].Name;
                    long longValue = enumDataContract.Values[i];

                    CodeMemberField enumMember = new CodeMemberField();
                    if (enumDataContract.IsULong)
                        enumMember.InitExpression = new CodeSnippetExpression(enumDataContract.GetStringFromEnumValue(longValue));
                    else
                        enumMember.InitExpression = new CodePrimitiveExpression(longValue);
                    enumMember.Name = GetMemberName(stringValue, contractCodeDomInfo);
                    CodeAttributeDeclaration enumMemberAttribute = new CodeAttributeDeclaration(DataContract.GetClrTypeFullName(Globals.TypeOfEnumMemberAttribute));
                    if (enumMember.Name != stringValue)
                        enumMemberAttribute.Arguments.Add(new CodeAttributeArgument(Globals.ValueProperty, new CodePrimitiveExpression(stringValue)));
                    enumMember.CustomAttributes.Add(enumMemberAttribute);
                    type.Members.Add(enumMember);
                }
            }
        }
Exemplo n.º 8
0
        void ExportEnumDataContract(EnumDataContract enumDataContract, XmlSchema schema)
        {
            XmlSchemaSimpleType type = new XmlSchemaSimpleType();
            type.Name = enumDataContract.StableName.Name;
            XmlElement actualTypeElement = (enumDataContract.BaseContractName == DefaultEnumBaseTypeName) ? null : ExportActualType(enumDataContract.BaseContractName);
            type.Annotation = GetSchemaAnnotation(actualTypeElement, ExportSurrogateData(enumDataContract));
            schema.Items.Add(type);

            XmlSchemaSimpleTypeRestriction restriction = new XmlSchemaSimpleTypeRestriction();
            restriction.BaseTypeName = StringQualifiedName;
            SchemaHelper.AddSchemaImport(enumDataContract.BaseContractName.Namespace, schema);
            if (enumDataContract.Values != null)
            {
                for (int i = 0; i < enumDataContract.Values.Count; i++)
                {
                    XmlSchemaEnumerationFacet facet = new XmlSchemaEnumerationFacet();
                    facet.Value = enumDataContract.Members[i].Name;
                    if (enumDataContract.Values[i] != GetDefaultEnumValue(enumDataContract.IsFlags, i))
                        facet.Annotation = GetSchemaAnnotation(EnumerationValueAnnotationName, enumDataContract.GetStringFromEnumValue(enumDataContract.Values[i]), schema);
                    restriction.Facets.Add(facet);
                }
            }
            if (enumDataContract.IsFlags)
            {
                XmlSchemaSimpleTypeList list = new XmlSchemaSimpleTypeList();
                XmlSchemaSimpleType anonymousType = new XmlSchemaSimpleType();
                anonymousType.Content = restriction;
                list.ItemType = anonymousType;
                type.Content = list;
            }
            else
                type.Content = restriction;
        }
Exemplo n.º 9
0
        EnumDataContract ImportEnum(XmlQualifiedName typeName, XmlSchemaSimpleTypeRestriction restriction, bool isFlags, XmlSchemaAnnotation annotation)
        {
            EnumDataContract dataContract = new EnumDataContract();
            dataContract.StableName = typeName;
            dataContract.BaseContractName = ImportActualType(annotation, SchemaExporter.DefaultEnumBaseTypeName, typeName);
            dataContract.IsFlags = isFlags;
            AddDataContract(dataContract);

            // CheckIfEnum has already checked if baseType of restriction is string 
            dataContract.Values = new List<long>();
            dataContract.Members = new List<DataMember>();
            foreach (XmlSchemaFacet facet in restriction.Facets)
            {
                XmlSchemaEnumerationFacet enumFacet = facet as XmlSchemaEnumerationFacet;
                if (enumFacet == null)
                    ThrowEnumTypeCannotBeImportedException(typeName.Name, typeName.Namespace, SR.GetString(SR.EnumOnlyEnumerationFacetsSupported));
                if (enumFacet.Value == null)
                    ThrowEnumTypeCannotBeImportedException(typeName.Name, typeName.Namespace, SR.GetString(SR.EnumEnumerationFacetsMustHaveValue));

                string valueInnerText = GetInnerText(typeName, ImportAnnotation(enumFacet.Annotation, SchemaExporter.EnumerationValueAnnotationName));
                if (valueInnerText == null)
                    dataContract.Values.Add(SchemaExporter.GetDefaultEnumValue(isFlags, dataContract.Members.Count));
                else
                    dataContract.Values.Add(dataContract.GetEnumValueFromString(valueInnerText));
                DataMember dataMember = new DataMember(enumFacet.Value);
                dataContract.Members.Add(dataMember);
            }
            return dataContract;
        }
 private void ExportEnumDataContract(EnumDataContract enumDataContract, XmlSchema schema)
 {
     XmlSchemaSimpleType item = new XmlSchemaSimpleType {
         Name = enumDataContract.StableName.Name
     };
     XmlElement element = (enumDataContract.BaseContractName == DefaultEnumBaseTypeName) ? null : this.ExportActualType(enumDataContract.BaseContractName);
     item.Annotation = GetSchemaAnnotation(new System.Xml.XmlNode[] { element, this.ExportSurrogateData(enumDataContract) });
     schema.Items.Add(item);
     XmlSchemaSimpleTypeRestriction restriction = new XmlSchemaSimpleTypeRestriction {
         BaseTypeName = StringQualifiedName
     };
     SchemaHelper.AddSchemaImport(enumDataContract.BaseContractName.Namespace, schema);
     if (enumDataContract.Values != null)
     {
         for (int i = 0; i < enumDataContract.Values.Count; i++)
         {
             XmlSchemaEnumerationFacet facet = new XmlSchemaEnumerationFacet {
                 Value = enumDataContract.Members[i].Name
             };
             if (enumDataContract.Values[i] != GetDefaultEnumValue(enumDataContract.IsFlags, i))
             {
                 facet.Annotation = this.GetSchemaAnnotation(EnumerationValueAnnotationName, enumDataContract.GetStringFromEnumValue(enumDataContract.Values[i]), schema);
             }
             restriction.Facets.Add(facet);
         }
     }
     if (enumDataContract.IsFlags)
     {
         XmlSchemaSimpleTypeList list = new XmlSchemaSimpleTypeList();
         XmlSchemaSimpleType type2 = new XmlSchemaSimpleType {
             Content = restriction
         };
         list.ItemType = type2;
         item.Content = list;
     }
     else
     {
         item.Content = restriction;
     }
 }
 private EnumDataContract ImportEnum(XmlQualifiedName typeName, XmlSchemaSimpleTypeRestriction restriction, bool isFlags, XmlSchemaAnnotation annotation)
 {
     EnumDataContract dataContract = new EnumDataContract {
         StableName = typeName,
         BaseContractName = ImportActualType(annotation, SchemaExporter.DefaultEnumBaseTypeName, typeName),
         IsFlags = isFlags
     };
     this.AddDataContract(dataContract);
     dataContract.Values = new List<long>();
     dataContract.Members = new List<DataMember>();
     foreach (XmlSchemaFacet facet in restriction.Facets)
     {
         XmlSchemaEnumerationFacet facet2 = facet as XmlSchemaEnumerationFacet;
         if (facet2 == null)
         {
             ThrowEnumTypeCannotBeImportedException(typeName.Name, typeName.Namespace, System.Runtime.Serialization.SR.GetString("EnumOnlyEnumerationFacetsSupported"));
         }
         if (facet2.Value == null)
         {
             ThrowEnumTypeCannotBeImportedException(typeName.Name, typeName.Namespace, System.Runtime.Serialization.SR.GetString("EnumEnumerationFacetsMustHaveValue"));
         }
         string innerText = this.GetInnerText(typeName, ImportAnnotation(facet2.Annotation, SchemaExporter.EnumerationValueAnnotationName));
         if (innerText == null)
         {
             dataContract.Values.Add(SchemaExporter.GetDefaultEnumValue(isFlags, dataContract.Members.Count));
         }
         else
         {
             dataContract.Values.Add(dataContract.GetEnumValueFromString(innerText));
         }
         DataMember item = new DataMember(facet2.Value);
         dataContract.Members.Add(item);
     }
     return dataContract;
 }