private string GetClrNamespace(DataContract dataContract, ContractCodeDomInfo contractCodeDomInfo)
 {
     string clrNamespace = contractCodeDomInfo.ClrNamespace;
     bool flag = false;
     if (clrNamespace == null)
     {
         if (!this.Namespaces.TryGetValue(dataContract.StableName.Namespace, out clrNamespace))
         {
             if (this.Namespaces.TryGetValue(wildcardNamespaceMapping, out clrNamespace))
             {
                 flag = true;
             }
             else
             {
                 clrNamespace = GetClrNamespace(dataContract.StableName.Namespace);
                 if (this.ClrNamespaces.ContainsKey(clrNamespace))
                 {
                     string key = null;
                     int num = 1;
                     while (true)
                     {
                         key = ((clrNamespace.Length == 0) ? "GeneratedNamespace" : clrNamespace) + num.ToString(NumberFormatInfo.InvariantInfo);
                         if (!this.ClrNamespaces.ContainsKey(key))
                         {
                             clrNamespace = key;
                             break;
                         }
                         num++;
                     }
                 }
                 this.AddNamespacePair(dataContract.StableName.Namespace, clrNamespace);
             }
         }
         contractCodeDomInfo.ClrNamespace = clrNamespace;
         contractCodeDomInfo.UsesWildcardNamespace = flag;
     }
     return clrNamespace;
 }
 internal void SetContractCodeDomInfo(DataContract dataContract, ContractCodeDomInfo info)
 {
     ProcessedContracts.Add(dataContract, info);
 }
 private void ExportISerializableDataContract(ClassDataContract dataContract, ContractCodeDomInfo contractCodeDomInfo)
 {
     this.GenerateType(dataContract, contractCodeDomInfo);
     if (!contractCodeDomInfo.ReferencedTypeExists)
     {
         if (DataContract.GetDefaultStableNamespace(contractCodeDomInfo.ClrNamespace) != dataContract.StableName.Namespace)
         {
             throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidDataContractException(System.Runtime.Serialization.SR.GetString("InvalidClrNamespaceGeneratedForISerializable", new object[] { dataContract.StableName.Name, dataContract.StableName.Namespace, DataContract.GetDataContractNamespaceFromUri(dataContract.StableName.Namespace), contractCodeDomInfo.ClrNamespace })));
         }
         string nameForAttribute = this.GetNameForAttribute(dataContract.StableName.Name);
         int num = nameForAttribute.LastIndexOf('.');
         string str2 = ((num <= 0) || (num == (nameForAttribute.Length - 1))) ? nameForAttribute : nameForAttribute.Substring(num + 1);
         if (contractCodeDomInfo.TypeDeclaration.Name != str2)
         {
             throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidDataContractException(System.Runtime.Serialization.SR.GetString("InvalidClrNameGeneratedForISerializable", new object[] { dataContract.StableName.Name, dataContract.StableName.Namespace, contractCodeDomInfo.TypeDeclaration.Name })));
         }
         CodeTypeDeclaration typeDeclaration = contractCodeDomInfo.TypeDeclaration;
         if (this.SupportsPartialTypes)
         {
             typeDeclaration.IsPartial = true;
         }
         if (dataContract.IsValueType && this.SupportsDeclareValueTypes)
         {
             typeDeclaration.IsStruct = true;
         }
         else
         {
             typeDeclaration.IsClass = true;
         }
         this.AddSerializableAttribute(true, typeDeclaration, contractCodeDomInfo);
         this.AddKnownTypes(dataContract, contractCodeDomInfo);
         if (dataContract.BaseContract == null)
         {
             if (!typeDeclaration.IsStruct)
             {
                 typeDeclaration.BaseTypes.Add(Globals.TypeOfObject);
             }
             typeDeclaration.BaseTypes.Add(DataContract.GetClrTypeFullName(Globals.TypeOfISerializable));
             typeDeclaration.Members.Add(this.ISerializableBaseConstructor);
             typeDeclaration.Members.Add(this.SerializationInfoField);
             typeDeclaration.Members.Add(this.SerializationInfoProperty);
             typeDeclaration.Members.Add(this.GetObjectDataMethod);
             this.AddPropertyChangedNotifier(contractCodeDomInfo, typeDeclaration.IsStruct);
         }
         else
         {
             ContractCodeDomInfo info = this.GetContractCodeDomInfo(dataContract.BaseContract);
             this.GenerateType(dataContract.BaseContract, info);
             typeDeclaration.BaseTypes.Add(info.TypeReference);
             if (info.ReferencedTypeExists)
             {
                 Type baseType = (Type) info.TypeReference.UserData[codeUserDataActualTypeKey];
                 this.ThrowIfReferencedBaseTypeSealed(baseType, dataContract);
             }
             typeDeclaration.Members.Add(this.ISerializableDerivedConstructor);
         }
     }
 }
 private void GenerateKeyValueType(ClassDataContract keyValueContract)
 {
     if (((keyValueContract != null) && (this.dataContractSet[keyValueContract.StableName] == null)) && (this.dataContractSet.GetContractCodeDomInfo(keyValueContract) == null))
     {
         ContractCodeDomInfo info = new ContractCodeDomInfo();
         this.dataContractSet.SetContractCodeDomInfo(keyValueContract, info);
         this.ExportClassDataContract(keyValueContract, info);
         info.IsProcessed = true;
     }
 }
 private void ExportClassDataContract(ClassDataContract classDataContract, ContractCodeDomInfo contractCodeDomInfo)
 {
     this.GenerateType(classDataContract, contractCodeDomInfo);
     if (!contractCodeDomInfo.ReferencedTypeExists)
     {
         CodeTypeDeclaration typeDeclaration = contractCodeDomInfo.TypeDeclaration;
         if (this.SupportsPartialTypes)
         {
             typeDeclaration.IsPartial = true;
         }
         if (classDataContract.IsValueType && this.SupportsDeclareValueTypes)
         {
             typeDeclaration.IsStruct = true;
         }
         else
         {
             typeDeclaration.IsClass = true;
         }
         string nameForAttribute = this.GetNameForAttribute(classDataContract.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(classDataContract.StableName.Namespace)));
         if (classDataContract.IsReference)
         {
             declaration2.Arguments.Add(new CodeAttributeArgument("IsReference", new CodePrimitiveExpression(classDataContract.IsReference)));
         }
         typeDeclaration.CustomAttributes.Add(declaration2);
         this.AddImportStatement(Globals.TypeOfDataContractAttribute.Namespace, contractCodeDomInfo.CodeNamespace);
         this.AddSerializableAttribute(this.GenerateSerializableTypes, typeDeclaration, contractCodeDomInfo);
         this.AddKnownTypes(classDataContract, contractCodeDomInfo);
         if (classDataContract.BaseContract == null)
         {
             if (!typeDeclaration.IsStruct)
             {
                 typeDeclaration.BaseTypes.Add(Globals.TypeOfObject);
             }
             this.AddExtensionData(contractCodeDomInfo);
             this.AddPropertyChangedNotifier(contractCodeDomInfo, typeDeclaration.IsStruct);
         }
         else
         {
             ContractCodeDomInfo baseContractCodeDomInfo = this.GetContractCodeDomInfo(classDataContract.BaseContract);
             typeDeclaration.BaseTypes.Add(baseContractCodeDomInfo.TypeReference);
             this.AddBaseMemberNames(baseContractCodeDomInfo, contractCodeDomInfo);
             if (baseContractCodeDomInfo.ReferencedTypeExists)
             {
                 Type baseType = (Type) baseContractCodeDomInfo.TypeReference.UserData[codeUserDataActualTypeKey];
                 this.ThrowIfReferencedBaseTypeSealed(baseType, classDataContract);
                 if (!Globals.TypeOfIExtensibleDataObject.IsAssignableFrom(baseType))
                 {
                     this.AddExtensionData(contractCodeDomInfo);
                 }
                 this.AddPropertyChangedNotifier(contractCodeDomInfo, typeDeclaration.IsStruct);
             }
         }
         if (classDataContract.Members != null)
         {
             for (int i = 0; i < classDataContract.Members.Count; i++)
             {
                 DataMember key = classDataContract.Members[i];
                 CodeTypeReference elementTypeReference = this.GetElementTypeReference(key.MemberTypeContract, key.IsNullable && key.MemberTypeContract.IsValueType);
                 string memberName = this.GetNameForAttribute(key.Name);
                 string identifier = this.GetMemberName(memberName, contractCodeDomInfo);
                 string fieldName = this.GetMemberName(AppendToValidClrIdentifier(identifier, "Field"), contractCodeDomInfo);
                 CodeMemberField field = new CodeMemberField {
                     Type = elementTypeReference,
                     Name = fieldName,
                     Attributes = MemberAttributes.Private
                 };
                 CodeMemberProperty property = this.CreateProperty(elementTypeReference, identifier, fieldName, key.MemberTypeContract.IsValueType && this.SupportsDeclareValueTypes);
                 if (this.dataContractSet.DataContractSurrogate != null)
                 {
                     property.UserData.Add(surrogateDataKey, this.dataContractSet.GetSurrogateData(key));
                 }
                 CodeAttributeDeclaration declaration3 = new CodeAttributeDeclaration(DataContract.GetClrTypeFullName(Globals.TypeOfDataMemberAttribute));
                 if (memberName != property.Name)
                 {
                     declaration3.Arguments.Add(new CodeAttributeArgument("Name", new CodePrimitiveExpression(memberName)));
                 }
                 if (key.IsRequired)
                 {
                     declaration3.Arguments.Add(new CodeAttributeArgument("IsRequired", new CodePrimitiveExpression(key.IsRequired)));
                 }
                 if (!key.EmitDefaultValue)
                 {
                     declaration3.Arguments.Add(new CodeAttributeArgument("EmitDefaultValue", new CodePrimitiveExpression(key.EmitDefaultValue)));
                 }
                 if (key.Order != 0)
                 {
                     declaration3.Arguments.Add(new CodeAttributeArgument("Order", new CodePrimitiveExpression(key.Order)));
                 }
                 property.CustomAttributes.Add(declaration3);
                 if (this.GenerateSerializableTypes && !key.IsRequired)
                 {
                     CodeAttributeDeclaration declaration4 = new CodeAttributeDeclaration(DataContract.GetClrTypeFullName(Globals.TypeOfOptionalFieldAttribute));
                     field.CustomAttributes.Add(declaration4);
                 }
                 typeDeclaration.Members.Add(field);
                 typeDeclaration.Members.Add(property);
             }
         }
     }
 }
 private void ExportCollectionDataContract(CollectionDataContract collectionContract, ContractCodeDomInfo contractCodeDomInfo)
 {
     this.GenerateType(collectionContract, contractCodeDomInfo);
     if (!contractCodeDomInfo.ReferencedTypeExists)
     {
         CodeTypeReference codeTypeReference;
         string nameForAttribute = this.GetNameForAttribute(collectionContract.StableName.Name);
         if (!this.SupportsGenericTypeReference)
         {
             throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(System.Runtime.Serialization.SR.GetString("CannotUseGenericTypeAsBase", new object[] { nameForAttribute, collectionContract.StableName.Namespace })));
         }
         DataContract itemContract = collectionContract.ItemContract;
         bool isItemTypeNullable = collectionContract.IsItemTypeNullable;
         bool flag2 = this.TryGetReferencedDictionaryType(collectionContract, out codeTypeReference);
         if (!flag2)
         {
             if (collectionContract.IsDictionary)
             {
                 this.GenerateKeyValueType(collectionContract.ItemContract as ClassDataContract);
             }
             if (!this.TryGetReferencedListType(itemContract, isItemTypeNullable, out codeTypeReference))
             {
                 if (!this.SupportsGenericTypeReference)
                 {
                     string str2 = "ArrayOf" + itemContract.StableName.Name;
                     string collectionNamespace = DataContract.GetCollectionNamespace(itemContract.StableName.Namespace);
                     throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(System.Runtime.Serialization.SR.GetString("ReferencedBaseTypeDoesNotExist", new object[] { nameForAttribute, collectionContract.StableName.Namespace, str2, collectionNamespace, DataContract.GetClrTypeFullName(Globals.TypeOfIListGeneric), DataContract.GetClrTypeFullName(Globals.TypeOfICollectionGeneric) })));
                 }
                 codeTypeReference = this.GetCodeTypeReference(Globals.TypeOfListGeneric);
                 codeTypeReference.TypeArguments.Add(this.GetElementTypeReference(itemContract, isItemTypeNullable));
             }
         }
         CodeTypeDeclaration typeDeclaration = contractCodeDomInfo.TypeDeclaration;
         typeDeclaration.BaseTypes.Add(codeTypeReference);
         CodeAttributeDeclaration declaration2 = new CodeAttributeDeclaration(DataContract.GetClrTypeFullName(Globals.TypeOfCollectionDataContractAttribute));
         declaration2.Arguments.Add(new CodeAttributeArgument("Name", new CodePrimitiveExpression(nameForAttribute)));
         declaration2.Arguments.Add(new CodeAttributeArgument("Namespace", new CodePrimitiveExpression(collectionContract.StableName.Namespace)));
         if (collectionContract.IsReference)
         {
             declaration2.Arguments.Add(new CodeAttributeArgument("IsReference", new CodePrimitiveExpression(collectionContract.IsReference)));
         }
         declaration2.Arguments.Add(new CodeAttributeArgument("ItemName", new CodePrimitiveExpression(this.GetNameForAttribute(collectionContract.ItemName))));
         if (flag2)
         {
             declaration2.Arguments.Add(new CodeAttributeArgument("KeyName", new CodePrimitiveExpression(this.GetNameForAttribute(collectionContract.KeyName))));
             declaration2.Arguments.Add(new CodeAttributeArgument("ValueName", new CodePrimitiveExpression(this.GetNameForAttribute(collectionContract.ValueName))));
         }
         typeDeclaration.CustomAttributes.Add(declaration2);
         this.AddImportStatement(Globals.TypeOfCollectionDataContractAttribute.Namespace, contractCodeDomInfo.CodeNamespace);
         this.AddSerializableAttribute(this.GenerateSerializableTypes, typeDeclaration, contractCodeDomInfo);
     }
 }
예제 #7
0
        void GenerateType(DataContract dataContract, ContractCodeDomInfo contractCodeDomInfo)
        {
            if (!contractCodeDomInfo.IsProcessed)
            {
                CodeTypeReference referencedType = GetReferencedType(dataContract);
                if (referencedType != null)
                {
                    contractCodeDomInfo.TypeReference = referencedType;
                    contractCodeDomInfo.ReferencedTypeExists = true;
                }
                else
                {
                    CodeTypeDeclaration type = contractCodeDomInfo.TypeDeclaration;
                    if (type == null)
                    {
                        string clrNamespace = GetClrNamespace(dataContract, contractCodeDomInfo);
                        CodeNamespace ns = GetCodeNamespace(clrNamespace, dataContract.StableName.Namespace, contractCodeDomInfo);
                        type = GetNestedType(dataContract, contractCodeDomInfo);
                        if (type == null)
                        {
                            string typeName = XmlConvert.DecodeName(dataContract.StableName.Name);
                            typeName = GetClrIdentifier(typeName, Globals.DefaultTypeName);
                            if (NamespaceContainsType(ns, typeName) || GlobalTypeNameConflicts(clrNamespace, typeName))
                            {
                                for (int i = 1;; i++)
                                {
                                    string uniqueName = AppendToValidClrIdentifier(typeName, i.ToString(NumberFormatInfo.InvariantInfo));
                                    if (!NamespaceContainsType(ns, uniqueName) && !GlobalTypeNameConflicts(clrNamespace, uniqueName))
                                    {
                                        typeName = uniqueName;
                                        break;
                                    }
                                    if (i == Int32.MaxValue)
                                        throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidDataContractException(SR.GetString(SR.CannotComputeUniqueName, typeName)));
                                }
                            }

                            type = CreateTypeDeclaration(typeName, dataContract);
                            ns.Types.Add(type);
                            if (string.IsNullOrEmpty(clrNamespace))
                            {
                                AddGlobalTypeName(typeName);
                            }
                            contractCodeDomInfo.TypeReference = new CodeTypeReference((clrNamespace == null || clrNamespace.Length == 0) ? typeName : clrNamespace + "." + typeName);

                            if (GenerateInternalTypes)
                                type.TypeAttributes = TypeAttributes.NotPublic;
                            else
                                type.TypeAttributes = TypeAttributes.Public;
                        }
                        if (dataContractSet.DataContractSurrogate != null)
                            type.UserData.Add(surrogateDataKey, dataContractSet.GetSurrogateData(dataContract));

                        contractCodeDomInfo.TypeDeclaration = type;
                    }
                }
            }
        }
 private void AddSerializableAttribute(bool generateSerializable, CodeTypeDeclaration type, ContractCodeDomInfo contractCodeDomInfo)
 {
     if (generateSerializable)
     {
         type.CustomAttributes.Add(this.SerializableAttribute);
         this.AddImportStatement(Globals.TypeOfSerializableAttribute.Namespace, contractCodeDomInfo.CodeNamespace);
     }
 }
예제 #9
0
 string GetClrNamespace(DataContract dataContract, ContractCodeDomInfo contractCodeDomInfo)
 {
     string clrNamespace = contractCodeDomInfo.ClrNamespace;
     bool usesWildcardNamespace = false;
     if (clrNamespace == null)
     {
         if (!Namespaces.TryGetValue(dataContract.StableName.Namespace, out clrNamespace))
         {
             if (Namespaces.TryGetValue(wildcardNamespaceMapping, out clrNamespace))
             {
                 usesWildcardNamespace = true;
             }
             else
             {
                 clrNamespace = GetClrNamespace(dataContract.StableName.Namespace);
                 if (ClrNamespaces.ContainsKey(clrNamespace))
                 {
                     string uniqueNamespace = null;
                     for (int i = 1;; i++)
                     {
                         uniqueNamespace = ((clrNamespace.Length == 0) ? Globals.DefaultClrNamespace : clrNamespace) + i.ToString(NumberFormatInfo.InvariantInfo);
                         if (!ClrNamespaces.ContainsKey(uniqueNamespace))
                         {
                             clrNamespace = uniqueNamespace;
                             break;
                         }
                     }
                 }
                 AddNamespacePair(dataContract.StableName.Namespace, clrNamespace);
             }
         }
         contractCodeDomInfo.ClrNamespace = clrNamespace;
         contractCodeDomInfo.UsesWildcardNamespace = usesWildcardNamespace;
     }
     return clrNamespace;
 }
예제 #10
0
        void ExportClassDataContractHierarchy(XmlQualifiedName typeName, ClassDataContract classContract, ContractCodeDomInfo contractCodeDomInfo, Dictionary<XmlQualifiedName, object> contractNamesInHierarchy)
        {
            if (contractNamesInHierarchy.ContainsKey(classContract.StableName))
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidDataContractException(SR.GetString(SR.TypeCannotBeImported, typeName.Name, typeName.Namespace, SR.GetString(SR.CircularTypeReference, classContract.StableName.Name, classContract.StableName.Namespace))));
            contractNamesInHierarchy.Add(classContract.StableName, null);

            ClassDataContract baseContract = classContract.BaseContract;
            if (baseContract != null)
            {
                ContractCodeDomInfo baseContractCodeDomInfo = GetContractCodeDomInfo(baseContract);
                if (!baseContractCodeDomInfo.IsProcessed)
                {
                    ExportClassDataContractHierarchy(typeName, baseContract, baseContractCodeDomInfo, contractNamesInHierarchy);
                    baseContractCodeDomInfo.IsProcessed = true;
                }
            }
            ExportClassDataContract(classContract, contractCodeDomInfo);
        }
예제 #11
0
        string GetMemberName(string memberName, ContractCodeDomInfo contractCodeDomInfo)
        {
            memberName = GetClrIdentifier(memberName, Globals.DefaultGeneratedMember);

            if (memberName == contractCodeDomInfo.TypeDeclaration.Name)
                memberName = AppendToValidClrIdentifier(memberName, Globals.DefaultMemberSuffix);

            if (contractCodeDomInfo.GetMemberNames().ContainsKey(memberName))
            {
                string uniqueMemberName = null;
                for (int i = 1;; i++)
                {
                    uniqueMemberName = AppendToValidClrIdentifier(memberName, i.ToString(NumberFormatInfo.InvariantInfo));
                    if (!contractCodeDomInfo.GetMemberNames().ContainsKey(uniqueMemberName))
                    {
                        memberName = uniqueMemberName;
                        break;
                    }
                }
            }

            contractCodeDomInfo.GetMemberNames().Add(memberName, null);
            return memberName;
        }
예제 #12
0
        CodeNamespace GetCodeNamespace(string clrNamespace, string dataContractNamespace, ContractCodeDomInfo contractCodeDomInfo)
        {
            if (contractCodeDomInfo.CodeNamespace != null)
                return contractCodeDomInfo.CodeNamespace;

            CodeNamespaceCollection codeNamespaceCollection = codeCompileUnit.Namespaces;
            foreach (CodeNamespace ns in codeNamespaceCollection)
            {
                if (ns.Name == clrNamespace)
                {
                    contractCodeDomInfo.CodeNamespace = ns;
                    return ns;
                }
            }

            CodeNamespace codeNamespace = new CodeNamespace(clrNamespace);
            codeNamespaceCollection.Add(codeNamespace);

            if (CanDeclareAssemblyAttribute(contractCodeDomInfo)
                && NeedsExplicitNamespace(dataContractNamespace, clrNamespace))
            {
                CodeAttributeDeclaration namespaceAttribute = new CodeAttributeDeclaration(DataContract.GetClrTypeFullName(Globals.TypeOfContractNamespaceAttribute));
                namespaceAttribute.Arguments.Add(new CodeAttributeArgument(new CodePrimitiveExpression(dataContractNamespace)));
                namespaceAttribute.Arguments.Add(new CodeAttributeArgument(Globals.ClrNamespaceProperty, new CodePrimitiveExpression(clrNamespace)));
                codeCompileUnit.AssemblyCustomAttributes.Add(namespaceAttribute);
            }
            contractCodeDomInfo.CodeNamespace = codeNamespace;
            return codeNamespace;
        }
예제 #13
0
        private void ExportXmlDataContract(XmlDataContract xmlDataContract, ContractCodeDomInfo contractCodeDomInfo)
        {
            GenerateType(xmlDataContract, contractCodeDomInfo);
            if (contractCodeDomInfo.ReferencedTypeExists)
                return;

            CodeTypeDeclaration type = contractCodeDomInfo.TypeDeclaration;
            if (SupportsPartialTypes)
                type.IsPartial = true;
            if (xmlDataContract.IsValueType)
                type.IsStruct = true;
            else
            {
                type.IsClass = true;
                type.BaseTypes.Add(Globals.TypeOfObject);
            }
            AddSerializableAttribute(GenerateSerializableTypes, type, contractCodeDomInfo);

            type.BaseTypes.Add(DataContract.GetClrTypeFullName(Globals.TypeOfIXmlSerializable));

            type.Members.Add(NodeArrayField);
            type.Members.Add(NodeArrayProperty);
            type.Members.Add(ReadXmlMethod);
            type.Members.Add(WriteXmlMethod);
            type.Members.Add(GetSchemaMethod);
            if (xmlDataContract.IsAnonymous && !xmlDataContract.HasRoot)
            {
                type.CustomAttributes.Add(new CodeAttributeDeclaration(
                    DataContract.GetClrTypeFullName(Globals.TypeOfXmlSchemaProviderAttribute),
                    new CodeAttributeArgument(NullReference),
                    new CodeAttributeArgument(Globals.IsAnyProperty, new CodePrimitiveExpression(true)))
                );
            }
            else
            {
                type.CustomAttributes.Add(new CodeAttributeDeclaration(
                    DataContract.GetClrTypeFullName(Globals.TypeOfXmlSchemaProviderAttribute),
                    new CodeAttributeArgument(new CodePrimitiveExpression(Globals.ExportSchemaMethod)))
                );

                CodeMemberField typeNameField = new CodeMemberField(Globals.TypeOfXmlQualifiedName, typeNameFieldName);
                typeNameField.Attributes |= MemberAttributes.Static | MemberAttributes.Private;
                XmlQualifiedName typeName = xmlDataContract.IsAnonymous
                    ? SchemaImporter.ImportActualType(xmlDataContract.XsdType.Annotation, xmlDataContract.StableName, xmlDataContract.StableName)
                    : xmlDataContract.StableName;
                typeNameField.InitExpression = new CodeObjectCreateExpression(Globals.TypeOfXmlQualifiedName, new CodePrimitiveExpression(typeName.Name), new CodePrimitiveExpression(typeName.Namespace));
                type.Members.Add(typeNameField);

                type.Members.Add(GetSchemaStaticMethod);

                bool isElementNameDifferent =
                    (xmlDataContract.TopLevelElementName != null && xmlDataContract.TopLevelElementName.Value != xmlDataContract.StableName.Name) ||
                    (xmlDataContract.TopLevelElementNamespace != null && xmlDataContract.TopLevelElementNamespace.Value != xmlDataContract.StableName.Namespace);
                if (isElementNameDifferent || xmlDataContract.IsTopLevelElementNullable == false)
                {
                    CodeAttributeDeclaration xmlRootAttribute = new CodeAttributeDeclaration(DataContract.GetClrTypeFullName(Globals.TypeOfXmlRootAttribute));
                    if (isElementNameDifferent)
                    {
                        if (xmlDataContract.TopLevelElementName != null)
                        {
                            xmlRootAttribute.Arguments.Add(new CodeAttributeArgument("ElementName", new CodePrimitiveExpression(xmlDataContract.TopLevelElementName.Value)));
                        }
                        if (xmlDataContract.TopLevelElementNamespace != null)
                        {
                            xmlRootAttribute.Arguments.Add(new CodeAttributeArgument("Namespace", new CodePrimitiveExpression(xmlDataContract.TopLevelElementNamespace.Value)));
                        }
                    }
                    if (xmlDataContract.IsTopLevelElementNullable == false)
                        xmlRootAttribute.Arguments.Add(new CodeAttributeArgument("IsNullable", new CodePrimitiveExpression(false)));
                    type.CustomAttributes.Add(xmlRootAttribute);
                }
            }
            AddPropertyChangedNotifier(contractCodeDomInfo, type.IsStruct);
        }
예제 #14
0
        void ExportCollectionDataContract(CollectionDataContract collectionContract, ContractCodeDomInfo contractCodeDomInfo)
        {
            GenerateType(collectionContract, contractCodeDomInfo);
            if (contractCodeDomInfo.ReferencedTypeExists)
                return;

            string dataContractName = GetNameForAttribute(collectionContract.StableName.Name);

            // If type name is not expected, generate collection type that derives from referenced list type and uses [CollectionDataContract] 
            if (!SupportsGenericTypeReference)
                throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(
                    SR.GetString(SR.CannotUseGenericTypeAsBase, dataContractName,
                    collectionContract.StableName.Namespace)));

            DataContract itemContract = collectionContract.ItemContract;
            bool isItemTypeNullable = collectionContract.IsItemTypeNullable;

            CodeTypeReference baseTypeReference;
            bool foundDictionaryBase = TryGetReferencedDictionaryType(collectionContract, out baseTypeReference);
            if (!foundDictionaryBase)
            {
                if (collectionContract.IsDictionary)
                {
                    GenerateKeyValueType(collectionContract.ItemContract as ClassDataContract);
                }
                if (!TryGetReferencedListType(itemContract, isItemTypeNullable, out baseTypeReference))
                {
                    if (SupportsGenericTypeReference)
                    {
                        baseTypeReference = GetCodeTypeReference(Globals.TypeOfListGeneric);
                        baseTypeReference.TypeArguments.Add(GetElementTypeReference(itemContract, isItemTypeNullable));
                    }
                    else
                    {
                        string expectedTypeName = Globals.ArrayPrefix + itemContract.StableName.Name;
                        string expectedTypeNs = DataContract.GetCollectionNamespace(itemContract.StableName.Namespace);
                        throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.GetString(SR.ReferencedBaseTypeDoesNotExist,
                            dataContractName, collectionContract.StableName.Namespace,
                            expectedTypeName, expectedTypeNs, DataContract.GetClrTypeFullName(Globals.TypeOfIListGeneric), DataContract.GetClrTypeFullName(Globals.TypeOfICollectionGeneric))));
                    }
                }
            }

            CodeTypeDeclaration generatedType = contractCodeDomInfo.TypeDeclaration;
            generatedType.BaseTypes.Add(baseTypeReference);
            CodeAttributeDeclaration collectionContractAttribute = new CodeAttributeDeclaration(DataContract.GetClrTypeFullName(Globals.TypeOfCollectionDataContractAttribute));
            collectionContractAttribute.Arguments.Add(new CodeAttributeArgument(Globals.NameProperty, new CodePrimitiveExpression(dataContractName)));
            collectionContractAttribute.Arguments.Add(new CodeAttributeArgument(Globals.NamespaceProperty, new CodePrimitiveExpression(collectionContract.StableName.Namespace)));
            if (collectionContract.IsReference != Globals.DefaultIsReference)
                collectionContractAttribute.Arguments.Add(new CodeAttributeArgument(Globals.IsReferenceProperty, new CodePrimitiveExpression(collectionContract.IsReference)));
            collectionContractAttribute.Arguments.Add(new CodeAttributeArgument(Globals.ItemNameProperty, new CodePrimitiveExpression(GetNameForAttribute(collectionContract.ItemName))));
            if (foundDictionaryBase)
            {
                collectionContractAttribute.Arguments.Add(new CodeAttributeArgument(Globals.KeyNameProperty, new CodePrimitiveExpression(GetNameForAttribute(collectionContract.KeyName))));
                collectionContractAttribute.Arguments.Add(new CodeAttributeArgument(Globals.ValueNameProperty, new CodePrimitiveExpression(GetNameForAttribute(collectionContract.ValueName))));
            }
            generatedType.CustomAttributes.Add(collectionContractAttribute);
            AddImportStatement(Globals.TypeOfCollectionDataContractAttribute.Namespace, contractCodeDomInfo.CodeNamespace);
            AddSerializableAttribute(GenerateSerializableTypes, generatedType, contractCodeDomInfo);
        }
예제 #15
0
        CodeTypeDeclaration GetNestedType(DataContract dataContract, ContractCodeDomInfo contractCodeDomInfo)
        {
            if (!SupportsNestedTypes)
                return null;
            string originalName = dataContract.StableName.Name;
            int nestedTypeIndex = originalName.LastIndexOf('.');
            if (nestedTypeIndex <= 0)
                return null;
            string containingTypeName = originalName.Substring(0, nestedTypeIndex);
            DataContract containingDataContract = dataContractSet[new XmlQualifiedName(containingTypeName, dataContract.StableName.Namespace)];
            if (containingDataContract == null)
                return null;
            string nestedTypeName = XmlConvert.DecodeName(originalName.Substring(nestedTypeIndex + 1));
            nestedTypeName = GetClrIdentifier(nestedTypeName, Globals.DefaultTypeName);

            ContractCodeDomInfo containingContractCodeDomInfo = GetContractCodeDomInfo(containingDataContract);
            GenerateType(containingDataContract, containingContractCodeDomInfo);
            if (containingContractCodeDomInfo.ReferencedTypeExists)
                return null;

            CodeTypeDeclaration containingType = containingContractCodeDomInfo.TypeDeclaration;
            if (TypeContainsNestedType(containingType, nestedTypeName))
            {
                for (int i = 1;; i++)
                {
                    string uniqueName = AppendToValidClrIdentifier(nestedTypeName, i.ToString(NumberFormatInfo.InvariantInfo));
                    if (!TypeContainsNestedType(containingType, uniqueName))
                    {
                        nestedTypeName = uniqueName;
                        break;
                    }
                }
            }

            CodeTypeDeclaration type = CreateTypeDeclaration(nestedTypeName, dataContract);
            containingType.Members.Add(type);
            contractCodeDomInfo.TypeReference = new CodeTypeReference(containingContractCodeDomInfo.TypeReference.BaseType + "+" + nestedTypeName);

            if (GenerateInternalTypes)
                type.TypeAttributes = TypeAttributes.NestedAssembly;
            else
                type.TypeAttributes = TypeAttributes.NestedPublic;
            return type;
        }
 private void AddPropertyChangedNotifier(ContractCodeDomInfo contractCodeDomInfo, bool isValueType)
 {
     if ((this.EnableDataBinding && this.SupportsDeclareEvents) && ((contractCodeDomInfo != null) && (contractCodeDomInfo.TypeDeclaration != null)))
     {
         CodeTypeDeclaration typeDeclaration = contractCodeDomInfo.TypeDeclaration;
         typeDeclaration.BaseTypes.Add(this.CodeTypeIPropertyChange);
         CodeMemberEvent propertyChangedEvent = this.PropertyChangedEvent;
         typeDeclaration.Members.Add(propertyChangedEvent);
         CodeMemberMethod raisePropertyChangedEventMethod = this.RaisePropertyChangedEventMethod;
         if (!isValueType)
         {
             raisePropertyChangedEventMethod.Attributes |= MemberAttributes.Family;
         }
         typeDeclaration.Members.Add(raisePropertyChangedEventMethod);
         contractCodeDomInfo.GetMemberNames().Add(propertyChangedEvent.Name, null);
         contractCodeDomInfo.GetMemberNames().Add(raisePropertyChangedEventMethod.Name, null);
     }
 }
예제 #17
0
        void ExportClassDataContract(ClassDataContract classDataContract, ContractCodeDomInfo contractCodeDomInfo)
        {
            GenerateType(classDataContract, contractCodeDomInfo);
            if (contractCodeDomInfo.ReferencedTypeExists)
                return;

            CodeTypeDeclaration type = contractCodeDomInfo.TypeDeclaration;
            if (SupportsPartialTypes)
                type.IsPartial = true;
            if (classDataContract.IsValueType && SupportsDeclareValueTypes)
                type.IsStruct = true;
            else
                type.IsClass = true;

            string dataContractName = GetNameForAttribute(classDataContract.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(classDataContract.StableName.Namespace)));
            if (classDataContract.IsReference != Globals.DefaultIsReference)
                dataContractAttribute.Arguments.Add(new CodeAttributeArgument(Globals.IsReferenceProperty, new CodePrimitiveExpression(classDataContract.IsReference)));
            type.CustomAttributes.Add(dataContractAttribute);
            AddImportStatement(Globals.TypeOfDataContractAttribute.Namespace, contractCodeDomInfo.CodeNamespace);

            AddSerializableAttribute(GenerateSerializableTypes, type, contractCodeDomInfo);

            AddKnownTypes(classDataContract, contractCodeDomInfo);

            bool raisePropertyChanged = EnableDataBinding && SupportsDeclareEvents;
            if (classDataContract.BaseContract == null)
            {
                if (!type.IsStruct)
                    type.BaseTypes.Add(Globals.TypeOfObject);
                AddExtensionData(contractCodeDomInfo);
                AddPropertyChangedNotifier(contractCodeDomInfo, type.IsStruct);
            }
            else
            {
                ContractCodeDomInfo baseContractCodeDomInfo = GetContractCodeDomInfo(classDataContract.BaseContract);
                Fx.Assert(baseContractCodeDomInfo.IsProcessed, "Cannot generate code for type if code for base type has not been generated");
                type.BaseTypes.Add(baseContractCodeDomInfo.TypeReference);
                AddBaseMemberNames(baseContractCodeDomInfo, contractCodeDomInfo);
                if (baseContractCodeDomInfo.ReferencedTypeExists)
                {
                    Type actualType = (Type)baseContractCodeDomInfo.TypeReference.UserData[codeUserDataActualTypeKey];
                    ThrowIfReferencedBaseTypeSealed(actualType, classDataContract);
                    if (!Globals.TypeOfIExtensibleDataObject.IsAssignableFrom(actualType))
                        AddExtensionData(contractCodeDomInfo);
                    if (!Globals.TypeOfIPropertyChange.IsAssignableFrom(actualType))
                    {
                        AddPropertyChangedNotifier(contractCodeDomInfo, type.IsStruct);
                    }
                    else
                    {
                        raisePropertyChanged = false;
                    }
                }
            }

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

                    CodeTypeReference memberType = GetElementTypeReference(dataMember.MemberTypeContract,
                        (dataMember.IsNullable && dataMember.MemberTypeContract.IsValueType));

                    string dataMemberName = GetNameForAttribute(dataMember.Name);
                    string propertyName = GetMemberName(dataMemberName, contractCodeDomInfo);
                    string fieldName = GetMemberName(AppendToValidClrIdentifier(propertyName, Globals.DefaultFieldSuffix), contractCodeDomInfo);

                    CodeMemberField field = new CodeMemberField();
                    field.Type = memberType;
                    field.Name = fieldName;
                    field.Attributes = MemberAttributes.Private;

                    CodeMemberProperty property = CreateProperty(memberType, propertyName, fieldName, dataMember.MemberTypeContract.IsValueType && SupportsDeclareValueTypes, raisePropertyChanged);
                    if (dataContractSet.DataContractSurrogate != null)
                        property.UserData.Add(surrogateDataKey, dataContractSet.GetSurrogateData(dataMember));

                    CodeAttributeDeclaration dataMemberAttribute = new CodeAttributeDeclaration(DataContract.GetClrTypeFullName(Globals.TypeOfDataMemberAttribute));
                    if (dataMemberName != property.Name)
                        dataMemberAttribute.Arguments.Add(new CodeAttributeArgument(Globals.NameProperty, new CodePrimitiveExpression(dataMemberName)));
                    if (dataMember.IsRequired != Globals.DefaultIsRequired)
                        dataMemberAttribute.Arguments.Add(new CodeAttributeArgument(Globals.IsRequiredProperty, new CodePrimitiveExpression(dataMember.IsRequired)));
                    if (dataMember.EmitDefaultValue != Globals.DefaultEmitDefaultValue)
                        dataMemberAttribute.Arguments.Add(new CodeAttributeArgument(Globals.EmitDefaultValueProperty, new CodePrimitiveExpression(dataMember.EmitDefaultValue)));
                    if (dataMember.Order != Globals.DefaultOrder)
                        dataMemberAttribute.Arguments.Add(new CodeAttributeArgument(Globals.OrderProperty, new CodePrimitiveExpression(dataMember.Order)));
                    property.CustomAttributes.Add(dataMemberAttribute);

                    if (GenerateSerializableTypes && !dataMember.IsRequired)
                    {
                        CodeAttributeDeclaration optionalFieldAttribute = new CodeAttributeDeclaration(DataContract.GetClrTypeFullName(Globals.TypeOfOptionalFieldAttribute));
                        field.CustomAttributes.Add(optionalFieldAttribute);
                    }

                    type.Members.Add(field);
                    type.Members.Add(property);
                }
            }
        }
 private bool CanDeclareAssemblyAttribute(ContractCodeDomInfo contractCodeDomInfo)
 {
     return (this.SupportsAssemblyAttributes && !contractCodeDomInfo.UsesWildcardNamespace);
 }
 private ContractCodeDomInfo GetContractCodeDomInfo(DataContract dataContract)
 {
     ContractCodeDomInfo contractCodeDomInfo = this.dataContractSet.GetContractCodeDomInfo(dataContract);
     if (contractCodeDomInfo == null)
     {
         contractCodeDomInfo = new ContractCodeDomInfo();
         this.dataContractSet.SetContractCodeDomInfo(dataContract, contractCodeDomInfo);
     }
     return contractCodeDomInfo;
 }
 private void ExportClassDataContractHierarchy(XmlQualifiedName typeName, ClassDataContract classContract, ContractCodeDomInfo contractCodeDomInfo, Dictionary<XmlQualifiedName, object> contractNamesInHierarchy)
 {
     if (contractNamesInHierarchy.ContainsKey(classContract.StableName))
     {
         throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidDataContractException(System.Runtime.Serialization.SR.GetString("TypeCannotBeImported", new object[] { typeName.Name, typeName.Namespace, System.Runtime.Serialization.SR.GetString("CircularTypeReference", new object[] { classContract.StableName.Name, classContract.StableName.Namespace }) })));
     }
     contractNamesInHierarchy.Add(classContract.StableName, null);
     ClassDataContract baseContract = classContract.BaseContract;
     if (baseContract != null)
     {
         ContractCodeDomInfo info = this.GetContractCodeDomInfo(baseContract);
         if (!info.IsProcessed)
         {
             this.ExportClassDataContractHierarchy(typeName, baseContract, info, contractNamesInHierarchy);
             info.IsProcessed = true;
         }
     }
     this.ExportClassDataContract(classContract, contractCodeDomInfo);
 }
 private void AddBaseMemberNames(ContractCodeDomInfo baseContractCodeDomInfo, ContractCodeDomInfo contractCodeDomInfo)
 {
     if (!baseContractCodeDomInfo.ReferencedTypeExists)
     {
         Dictionary<string, object> memberNames = baseContractCodeDomInfo.GetMemberNames();
         Dictionary<string, object> dictionary2 = contractCodeDomInfo.GetMemberNames();
         foreach (KeyValuePair<string, object> pair in memberNames)
         {
             dictionary2.Add(pair.Key, pair.Value);
         }
     }
 }
 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);
             }
         }
     }
 }
 private string GetMemberName(string memberName, ContractCodeDomInfo contractCodeDomInfo)
 {
     memberName = GetClrIdentifier(memberName, "GeneratedMember");
     if (memberName == contractCodeDomInfo.TypeDeclaration.Name)
     {
         memberName = AppendToValidClrIdentifier(memberName, "Member");
     }
     if (contractCodeDomInfo.GetMemberNames().ContainsKey(memberName))
     {
         string key = null;
         int num = 1;
         while (true)
         {
             key = AppendToValidClrIdentifier(memberName, num.ToString(NumberFormatInfo.InvariantInfo));
             if (!contractCodeDomInfo.GetMemberNames().ContainsKey(key))
             {
                 memberName = key;
                 break;
             }
             num++;
         }
     }
     contractCodeDomInfo.GetMemberNames().Add(memberName, null);
     return memberName;
 }
 private void ExportXmlDataContract(XmlDataContract xmlDataContract, ContractCodeDomInfo contractCodeDomInfo)
 {
     this.GenerateType(xmlDataContract, contractCodeDomInfo);
     if (!contractCodeDomInfo.ReferencedTypeExists)
     {
         CodeTypeDeclaration typeDeclaration = contractCodeDomInfo.TypeDeclaration;
         if (this.SupportsPartialTypes)
         {
             typeDeclaration.IsPartial = true;
         }
         if (xmlDataContract.IsValueType)
         {
             typeDeclaration.IsStruct = true;
         }
         else
         {
             typeDeclaration.IsClass = true;
             typeDeclaration.BaseTypes.Add(Globals.TypeOfObject);
         }
         this.AddSerializableAttribute(this.GenerateSerializableTypes, typeDeclaration, contractCodeDomInfo);
         typeDeclaration.BaseTypes.Add(DataContract.GetClrTypeFullName(Globals.TypeOfIXmlSerializable));
         typeDeclaration.Members.Add(this.NodeArrayField);
         typeDeclaration.Members.Add(this.NodeArrayProperty);
         typeDeclaration.Members.Add(this.ReadXmlMethod);
         typeDeclaration.Members.Add(this.WriteXmlMethod);
         typeDeclaration.Members.Add(this.GetSchemaMethod);
         if (xmlDataContract.IsAnonymous && !xmlDataContract.HasRoot)
         {
             typeDeclaration.CustomAttributes.Add(new CodeAttributeDeclaration(DataContract.GetClrTypeFullName(Globals.TypeOfXmlSchemaProviderAttribute), new CodeAttributeArgument[] { new CodeAttributeArgument(this.NullReference), new CodeAttributeArgument("IsAny", new CodePrimitiveExpression(true)) }));
         }
         else
         {
             CodeMemberField field;
             typeDeclaration.CustomAttributes.Add(new CodeAttributeDeclaration(DataContract.GetClrTypeFullName(Globals.TypeOfXmlSchemaProviderAttribute), new CodeAttributeArgument[] { new CodeAttributeArgument(new CodePrimitiveExpression("ExportSchema")) }));
             field = new CodeMemberField(Globals.TypeOfXmlQualifiedName, typeNameFieldName) {
                 Attributes = field.Attributes | (MemberAttributes.Private | MemberAttributes.Static)
             };
             XmlQualifiedName name = xmlDataContract.IsAnonymous ? SchemaImporter.ImportActualType(xmlDataContract.XsdType.Annotation, xmlDataContract.StableName, xmlDataContract.StableName) : xmlDataContract.StableName;
             field.InitExpression = new CodeObjectCreateExpression(Globals.TypeOfXmlQualifiedName, new CodeExpression[] { new CodePrimitiveExpression(name.Name), new CodePrimitiveExpression(name.Namespace) });
             typeDeclaration.Members.Add(field);
             typeDeclaration.Members.Add(this.GetSchemaStaticMethod);
             bool flag = ((xmlDataContract.TopLevelElementName != null) && (xmlDataContract.TopLevelElementName.Value != xmlDataContract.StableName.Name)) || ((xmlDataContract.TopLevelElementNamespace != null) && (xmlDataContract.TopLevelElementNamespace.Value != xmlDataContract.StableName.Namespace));
             if (flag || !xmlDataContract.IsTopLevelElementNullable)
             {
                 CodeAttributeDeclaration declaration2 = new CodeAttributeDeclaration(DataContract.GetClrTypeFullName(Globals.TypeOfXmlRootAttribute));
                 if (flag)
                 {
                     if (xmlDataContract.TopLevelElementName != null)
                     {
                         declaration2.Arguments.Add(new CodeAttributeArgument("ElementName", new CodePrimitiveExpression(xmlDataContract.TopLevelElementName.Value)));
                     }
                     if (xmlDataContract.TopLevelElementNamespace != null)
                     {
                         declaration2.Arguments.Add(new CodeAttributeArgument("Namespace", new CodePrimitiveExpression(xmlDataContract.TopLevelElementNamespace.Value)));
                     }
                 }
                 if (!xmlDataContract.IsTopLevelElementNullable)
                 {
                     declaration2.Arguments.Add(new CodeAttributeArgument("IsNullable", new CodePrimitiveExpression(false)));
                 }
                 typeDeclaration.CustomAttributes.Add(declaration2);
             }
         }
         this.AddPropertyChangedNotifier(contractCodeDomInfo, typeDeclaration.IsStruct);
     }
 }
 private CodeTypeDeclaration GetNestedType(DataContract dataContract, ContractCodeDomInfo contractCodeDomInfo)
 {
     if (!this.SupportsNestedTypes)
     {
         return null;
     }
     string name = dataContract.StableName.Name;
     int length = name.LastIndexOf('.');
     if (length <= 0)
     {
         return null;
     }
     string str2 = name.Substring(0, length);
     DataContract contract = this.dataContractSet[new XmlQualifiedName(str2, dataContract.StableName.Namespace)];
     if (contract == null)
     {
         return null;
     }
     string clrIdentifier = GetClrIdentifier(XmlConvert.DecodeName(name.Substring(length + 1)), "GeneratedType");
     ContractCodeDomInfo info = this.GetContractCodeDomInfo(contract);
     this.GenerateType(contract, info);
     if (info.ReferencedTypeExists)
     {
         return null;
     }
     CodeTypeDeclaration typeDeclaration = info.TypeDeclaration;
     if (this.TypeContainsNestedType(typeDeclaration, clrIdentifier))
     {
         int num2 = 1;
         while (true)
         {
             string typeName = AppendToValidClrIdentifier(clrIdentifier, num2.ToString(NumberFormatInfo.InvariantInfo));
             if (!this.TypeContainsNestedType(typeDeclaration, typeName))
             {
                 clrIdentifier = typeName;
                 break;
             }
             num2++;
         }
     }
     CodeTypeDeclaration declaration2 = CreateTypeDeclaration(clrIdentifier, dataContract);
     typeDeclaration.Members.Add(declaration2);
     contractCodeDomInfo.TypeReference = new CodeTypeReference(info.TypeReference.BaseType + "+" + clrIdentifier);
     if (this.GenerateInternalTypes)
     {
         declaration2.TypeAttributes = TypeAttributes.NestedAssembly;
         return declaration2;
     }
     declaration2.TypeAttributes = TypeAttributes.NestedPublic;
     return declaration2;
 }
 private void GenerateType(DataContract dataContract, ContractCodeDomInfo contractCodeDomInfo)
 {
     if (!contractCodeDomInfo.IsProcessed)
     {
         CodeTypeReference referencedType = this.GetReferencedType(dataContract);
         if (referencedType != null)
         {
             contractCodeDomInfo.TypeReference = referencedType;
             contractCodeDomInfo.ReferencedTypeExists = true;
         }
         else if (contractCodeDomInfo.TypeDeclaration == null)
         {
             string clrNamespace = this.GetClrNamespace(dataContract, contractCodeDomInfo);
             CodeNamespace ns = this.GetCodeNamespace(clrNamespace, dataContract.StableName.Namespace, contractCodeDomInfo);
             CodeTypeDeclaration nestedType = this.GetNestedType(dataContract, contractCodeDomInfo);
             if (nestedType == null)
             {
                 string clrIdentifier = GetClrIdentifier(XmlConvert.DecodeName(dataContract.StableName.Name), "GeneratedType");
                 if (this.NamespaceContainsType(ns, clrIdentifier) || this.GlobalTypeNameConflicts(clrNamespace, clrIdentifier))
                 {
                     int num = 1;
                     while (true)
                     {
                         string typeName = AppendToValidClrIdentifier(clrIdentifier, num.ToString(NumberFormatInfo.InvariantInfo));
                         if (!this.NamespaceContainsType(ns, typeName) && !this.GlobalTypeNameConflicts(clrNamespace, typeName))
                         {
                             clrIdentifier = typeName;
                             break;
                         }
                         if (num == 0x7fffffff)
                         {
                             throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidDataContractException(System.Runtime.Serialization.SR.GetString("CannotComputeUniqueName", new object[] { clrIdentifier })));
                         }
                         num++;
                     }
                 }
                 nestedType = CreateTypeDeclaration(clrIdentifier, dataContract);
                 ns.Types.Add(nestedType);
                 if (string.IsNullOrEmpty(clrNamespace))
                 {
                     this.AddGlobalTypeName(clrIdentifier);
                 }
                 contractCodeDomInfo.TypeReference = new CodeTypeReference(((clrNamespace == null) || (clrNamespace.Length == 0)) ? clrIdentifier : (clrNamespace + "." + clrIdentifier));
                 if (this.GenerateInternalTypes)
                 {
                     nestedType.TypeAttributes = TypeAttributes.AnsiClass;
                 }
                 else
                 {
                     nestedType.TypeAttributes = TypeAttributes.Public;
                 }
             }
             if (this.dataContractSet.DataContractSurrogate != null)
             {
                 nestedType.UserData.Add(surrogateDataKey, this.dataContractSet.GetSurrogateData(dataContract));
             }
             contractCodeDomInfo.TypeDeclaration = nestedType;
         }
     }
 }
 private void AddExtensionData(ContractCodeDomInfo contractCodeDomInfo)
 {
     if ((contractCodeDomInfo != null) && (contractCodeDomInfo.TypeDeclaration != null))
     {
         CodeTypeDeclaration typeDeclaration = contractCodeDomInfo.TypeDeclaration;
         typeDeclaration.BaseTypes.Add(DataContract.GetClrTypeFullName(Globals.TypeOfIExtensibleDataObject));
         CodeMemberField extensionDataObjectField = this.ExtensionDataObjectField;
         if (this.GenerateSerializableTypes)
         {
             CodeAttributeDeclaration declaration2 = new CodeAttributeDeclaration(DataContract.GetClrTypeFullName(Globals.TypeOfNonSerializedAttribute));
             extensionDataObjectField.CustomAttributes.Add(declaration2);
         }
         typeDeclaration.Members.Add(extensionDataObjectField);
         contractCodeDomInfo.GetMemberNames().Add(extensionDataObjectField.Name, null);
         CodeMemberProperty extensionDataObjectProperty = this.ExtensionDataObjectProperty;
         typeDeclaration.Members.Add(extensionDataObjectProperty);
         contractCodeDomInfo.GetMemberNames().Add(extensionDataObjectProperty.Name, null);
     }
 }
 private CodeNamespace GetCodeNamespace(string clrNamespace, string dataContractNamespace, ContractCodeDomInfo contractCodeDomInfo)
 {
     if (contractCodeDomInfo.CodeNamespace != null)
     {
         return contractCodeDomInfo.CodeNamespace;
     }
     CodeNamespaceCollection namespaces = this.codeCompileUnit.Namespaces;
     foreach (CodeNamespace namespace2 in namespaces)
     {
         if (namespace2.Name == clrNamespace)
         {
             contractCodeDomInfo.CodeNamespace = namespace2;
             return namespace2;
         }
     }
     CodeNamespace namespace3 = new CodeNamespace(clrNamespace);
     namespaces.Add(namespace3);
     if (this.CanDeclareAssemblyAttribute(contractCodeDomInfo) && this.NeedsExplicitNamespace(dataContractNamespace, clrNamespace))
     {
         CodeAttributeDeclaration declaration = new CodeAttributeDeclaration(DataContract.GetClrTypeFullName(Globals.TypeOfContractNamespaceAttribute));
         declaration.Arguments.Add(new CodeAttributeArgument(new CodePrimitiveExpression(dataContractNamespace)));
         declaration.Arguments.Add(new CodeAttributeArgument("ClrNamespace", new CodePrimitiveExpression(clrNamespace)));
         this.codeCompileUnit.AssemblyCustomAttributes.Add(declaration);
     }
     contractCodeDomInfo.CodeNamespace = namespace3;
     return namespace3;
 }
 private void AddKnownTypes(ClassDataContract dataContract, ContractCodeDomInfo contractCodeDomInfo)
 {
     Dictionary<XmlQualifiedName, DataContract> knownTypeContracts = this.GetKnownTypeContracts(dataContract, new Dictionary<DataContract, object>());
     if ((knownTypeContracts != null) && (knownTypeContracts.Count != 0))
     {
         foreach (DataContract contract in knownTypeContracts.Values)
         {
             CodeAttributeDeclaration declaration = new CodeAttributeDeclaration(DataContract.GetClrTypeFullName(Globals.TypeOfKnownTypeAttribute));
             declaration.Arguments.Add(new CodeAttributeArgument(new CodeTypeOfExpression(this.GetCodeTypeReference(contract))));
             contractCodeDomInfo.TypeDeclaration.CustomAttributes.Add(declaration);
         }
         this.AddImportStatement(Globals.TypeOfKnownTypeAttribute.Namespace, contractCodeDomInfo.CodeNamespace);
     }
 }
예제 #30
0
 internal void SetContractCodeDomInfo(DataContract dataContract, ContractCodeDomInfo info)
 {
     ProcessedContracts.Add(dataContract, info);
 }
예제 #31
0
 void GenerateKeyValueType(ClassDataContract keyValueContract)
 {
     // Add code for KeyValue item type in the case where its usage is limited to dictionary 
     // and dictionary is not found in referenced types
     if (keyValueContract != null && dataContractSet[keyValueContract.StableName] == null)
     {
         ContractCodeDomInfo contractCodeDomInfo = dataContractSet.GetContractCodeDomInfo(keyValueContract);
         if (contractCodeDomInfo == null)
         {
             contractCodeDomInfo = new ContractCodeDomInfo();
             dataContractSet.SetContractCodeDomInfo(keyValueContract, contractCodeDomInfo);
             ExportClassDataContract(keyValueContract, contractCodeDomInfo);
             contractCodeDomInfo.IsProcessed = true;
         }
     }
 }