예제 #1
0
        internal static Type GetDataContractType(IDataContractSurrogate surrogate, Type type)
        {
            if (DataContract.GetBuiltInDataContract(type) != null)
            {
                return(type);
            }
            Type dcType = surrogate.GetDataContractType(type);

            if (dcType == null)
            {
                return(type);
            }
            return(dcType);
        }
예제 #2
0
        internal static object GetDeserializedObject(IDataContractSurrogate surrogate, object obj, Type objType, Type memberType)
        {
            if (obj == null)
            {
                return(null);
            }

            if (DataContract.GetBuiltInDataContract(objType) != null)
            {
                return(obj);
            }

            return(surrogate.GetDeserializedObject(obj, memberType));
        }
예제 #3
0
            internal EnumDataContractCriticalHelper(
                [DynamicallyAccessedMembers(ClassDataContract.DataContractPreserveMemberTypes)]
                Type type) : base(type)
            {
                XmlName = DataContract.GetXmlName(type, out _hasDataContract);
                Type             baseType     = Enum.GetUnderlyingType(type);
                XmlQualifiedName baseTypeName = GetBaseContractName(baseType);

                _baseContract = DataContract.GetBuiltInDataContract(baseTypeName.Name, baseTypeName.Namespace) !;
                // Setting XmlName might be redundant. But I don't want to miss an edge case.
                _baseContract.XmlName = baseTypeName;
                ImportBaseType(baseType);
                IsFlags = type.IsDefined(Globals.TypeOfFlagsAttribute, false);
                ImportDataMembers();

                XmlDictionary dictionary = new XmlDictionary(2 + Members.Count);

                Name               = dictionary.Add(XmlName.Name);
                Namespace          = dictionary.Add(XmlName.Namespace);
                _childElementNames = new XmlDictionaryString[Members.Count];
                for (int i = 0; i < Members.Count; i++)
                {
                    _childElementNames[i] = dictionary.Add(Members[i].Name);
                }
                if (TryGetDCAttribute(type, out DataContractAttribute? dataContractAttribute))
                {
                    if (dataContractAttribute.IsReference)
                    {
                        DataContract.ThrowInvalidDataContractException(
                            SR.Format(SR.EnumTypeCannotHaveIsReference,
                                      DataContract.GetClrTypeFullName(type),
                                      dataContractAttribute.IsReference,
                                      false),
                            type);
                    }
                }
            }
예제 #4
0
 internal static PrimitiveDataContract GetPrimitiveDataContract(string name, string ns)
 {
     return(DataContract.GetBuiltInDataContract(name, ns) as PrimitiveDataContract);
 }
예제 #5
0
 internal static PrimitiveDataContract GetPrimitiveDataContract(Type type)
 {
     return(DataContract.GetBuiltInDataContract(type) as PrimitiveDataContract);
 }
        private XmlElement ExportGenericInfo(Type clrType, string elementName, string elementNs)
        {
            int nestedCollectionLevel = 0;

            while (CollectionDataContract.IsCollection(clrType, out Type itemType))
            {
                if (DataContract.GetBuiltInDataContract(clrType) != null ||
                    CollectionDataContract.IsCollectionDataContract(clrType))
                {
                    break;
                }
                clrType = itemType;
                nestedCollectionLevel++;
            }

            Type[]      genericArguments      = null;
            IList <int> genericArgumentCounts = null;

            if (clrType.IsGenericType)
            {
                genericArguments = clrType.GetGenericArguments();
                string typeName;
                if (clrType.DeclaringType == null)
                {
                    typeName = clrType.Name;
                }
                else
                {
                    int nsLen = (clrType.Namespace == null) ? 0 : clrType.Namespace.Length;
                    if (nsLen > 0)
                    {
                        nsLen++; //include the . following namespace
                    }

                    typeName = DataContract.GetClrTypeFullName(clrType).Substring(nsLen).Replace('+', '.');
                }
                int iParam = typeName.IndexOf('[');
                if (iParam >= 0)
                {
                    typeName = typeName.Substring(0, iParam);
                }

                genericArgumentCounts = DataContract.GetDataContractNameForGenericName(typeName, null);
                clrType = clrType.GetGenericTypeDefinition();
            }
            XmlQualifiedName dcqname = DataContract.GetStableName(clrType);

            if (nestedCollectionLevel > 0)
            {
                string collectionName = dcqname.Name;
                for (int n = 0; n < nestedCollectionLevel; n++)
                {
                    collectionName = Globals.ArrayPrefix + collectionName;
                }

                dcqname = new XmlQualifiedName(collectionName, DataContract.GetCollectionNamespace(dcqname.Namespace));
            }
            XmlElement typeElement = XmlDoc.CreateElement(elementName, elementNs);

            XmlAttribute nameAttribute = XmlDoc.CreateAttribute(Globals.GenericNameAttribute);

            nameAttribute.Value = genericArguments != null?XmlConvert.DecodeName(dcqname.Name) : dcqname.Name;

            //nameAttribute.Value = dcqname.Name;
            typeElement.Attributes.Append(nameAttribute);

            XmlAttribute nsAttribute = XmlDoc.CreateAttribute(Globals.GenericNamespaceAttribute);

            nsAttribute.Value = dcqname.Namespace;
            typeElement.Attributes.Append(nsAttribute);

            if (genericArguments != null)
            {
                int argIndex    = 0;
                int nestedLevel = 0;
                foreach (int genericArgumentCount in genericArgumentCounts)
                {
                    for (int i = 0; i < genericArgumentCount; i++, argIndex++)
                    {
                        XmlElement argumentElement = ExportGenericInfo(genericArguments[argIndex], Globals.GenericParameterLocalName, Globals.SerializationNamespace);
                        if (nestedLevel > 0)
                        {
                            XmlAttribute nestedLevelAttribute = XmlDoc.CreateAttribute(Globals.GenericParameterNestedLevelAttribute);
                            nestedLevelAttribute.Value = nestedLevel.ToString(CultureInfo.InvariantCulture);
                            argumentElement.Attributes.Append(nestedLevelAttribute);
                        }
                        typeElement.AppendChild(argumentElement);
                    }
                    nestedLevel++;
                }
                if (genericArgumentCounts[nestedLevel - 1] == 0)
                {
                    XmlAttribute typeNestedLevelsAttribute = XmlDoc.CreateAttribute(Globals.GenericParameterNestedLevelAttribute);
                    typeNestedLevelsAttribute.Value = genericArgumentCounts.Count.ToString(CultureInfo.InvariantCulture);
                    typeElement.Attributes.Append(typeNestedLevelsAttribute);
                }
            }

            return(typeElement);
        }