internal static object?GetDeserializedObject(ISerializationSurrogateProvider surrogateProvider, object?obj, Type objType, Type memberType)
 {
     if (obj == null)
     {
         return(null);
     }
     if (DataContract.GetBuiltInDataContract(objType) != null)
     {
         return(obj);
     }
     return(surrogateProvider.GetDeserializedObject(obj, memberType));
 }
Example #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));
 }
Example #3
0
 public DataContract this[XmlQualifiedName key]
 {
     get
     {
         DataContract dataContract = DataContract.GetBuiltInDataContract(key.Name, key.Namespace);
         if (dataContract == null)
         {
             Contracts.TryGetValue(key, out dataContract);
         }
         return(dataContract);
     }
 }
        internal static DataContract GetDataContractFromSurrogateSelector(ISurrogateSelector surrogateSelector, StreamingContext context, RuntimeTypeHandle typeHandle, Type type, ref Hashtable surrogateDataContracts)
        {
            if (surrogateSelector == null)
            {
                return(null);
            }
            if (type == null)
            {
                type = Type.GetTypeFromHandle(typeHandle);
            }
            DataContract builtInDataContract = DataContract.GetBuiltInDataContract(type);

            if (builtInDataContract != null)
            {
                return(builtInDataContract);
            }
            if (surrogateDataContracts != null)
            {
                DataContract contract2 = (DataContract)surrogateDataContracts[type];
                if (contract2 != null)
                {
                    return(contract2);
                }
            }
            DataContract            contract3 = null;
            ISerializationSurrogate serializationSurrogate = GetSurrogate(type, surrogateSelector, context);

            if (serializationSurrogate != null)
            {
                contract3 = new SurrogateDataContract(type, serializationSurrogate);
            }
            else if (type.IsArray)
            {
                Type         elementType  = type.GetElementType();
                DataContract itemContract = GetDataContractFromSurrogateSelector(surrogateSelector, context, elementType.TypeHandle, elementType, ref surrogateDataContracts);
                if (itemContract == null)
                {
                    itemContract = DataContract.GetDataContract(elementType.TypeHandle, elementType, SerializationMode.SharedType);
                }
                contract3 = new CollectionDataContract(type, itemContract);
            }
            if (contract3 == null)
            {
                return(null);
            }
            if (surrogateDataContracts == null)
            {
                surrogateDataContracts = new Hashtable();
            }
            surrogateDataContracts.Add(type, contract3);
            return(contract3);
        }
Example #5
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);
        }
        internal DataContract FindDataContract(XmlQualifiedName typeName)
        {
            if (typeName == null)
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentNullException("typeName"));
            }
            DataContract builtInDataContract = DataContract.GetBuiltInDataContract(typeName.Name, typeName.Namespace);

            if (builtInDataContract == null)
            {
                builtInDataContract = this.DataContractSet[typeName];
                if (builtInDataContract == null)
                {
                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(System.Runtime.Serialization.SR.GetString("TypeHasNotBeenImported", new object[] { typeName.Name, typeName.Namespace })));
                }
            }
            return(builtInDataContract);
        }
        internal DataContract FindDataContract(XmlQualifiedName typeName)
        {
            if (typeName == null)
            {
                throw /*System.Runtime.Serialization.*/ DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentNullException("typeName"));
            }

            DataContract dataContract = DataContract.GetBuiltInDataContract(typeName.Name, typeName.Namespace);

            if (dataContract == null)
            {
                dataContract = DataContractSet[typeName];
                if (dataContract == null)
                {
                    throw /*System.Runtime.Serialization.*/ DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(string.Format(SRSerialization.TypeHasNotBeenImported, typeName.Name, typeName.Namespace)));
                }
            }
            return(dataContract);
        }
Example #8
0
        internal DataContract FindDataContract(XmlQualifiedName typeName)
        {
            if (typeName == null)
            {
                throw ExceptionUtil.ThrowHelperError(new ArgumentNullException(nameof(typeName)));
            }

            DataContract?dataContract = DataContract.GetBuiltInDataContract(typeName.Name, typeName.Namespace);

            if (dataContract == null)
            {
                dataContract = DataContractSet.GetDataContract(typeName);
                if (dataContract == null)
                {
                    throw ExceptionUtil.ThrowHelperError(new InvalidOperationException(SR.Format(SR.TypeHasNotBeenImported, typeName.Name, typeName.Namespace)));
                }
            }
            return(dataContract);
        }
        public ICollection <CodeTypeReference> GetKnownTypeReferences(XmlQualifiedName typeName)
        {
            if (typeName == null)
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentNullException("typeName"));
            }
            DataContract builtInDataContract = DataContract.GetBuiltInDataContract(typeName.Name, typeName.Namespace);

            if (builtInDataContract == null)
            {
                builtInDataContract = this.DataContractSet[typeName];
                if (builtInDataContract == null)
                {
                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(System.Runtime.Serialization.SR.GetString("TypeHasNotBeenImported", new object[] { typeName.Name, typeName.Namespace })));
                }
            }
            CodeExporter exporter = new CodeExporter(this.DataContractSet, this.Options, this.GetCodeCompileUnit());

            return(exporter.GetKnownTypeReferences(builtInDataContract));
        }
Example #10
0
        public ICollection <CodeTypeReference> GetKnownTypeReferences(XmlQualifiedName typeName)
        {
            if (typeName == null)
            {
                throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentNullException("typeName"));
            }

            DataContract dataContract = DataContract.GetBuiltInDataContract(typeName.Name, typeName.Namespace);

            if (dataContract == null)
            {
                dataContract = DataContractSet[typeName];
                if (dataContract == null)
                {
                    throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.GetString(SR.TypeHasNotBeenImported, typeName.Name, typeName.Namespace)));
                }
            }

            CodeExporter codeExporter = new CodeExporter(DataContractSet, Options, GetCodeCompileUnit());

            return(codeExporter.GetKnownTypeReferences(dataContract));
        }
Example #11
0
        public ICollection <CodeTypeReference>?GetKnownTypeReferences(XmlQualifiedName typeName)
        {
            if (typeName == null)
            {
                throw ExceptionUtil.ThrowHelperError(new ArgumentNullException(nameof(typeName)));
            }

            DataContract?dataContract = DataContract.GetBuiltInDataContract(typeName.Name, typeName.Namespace);

            if (dataContract == null)
            {
                dataContract = DataContractSet.GetDataContract(typeName);
                if (dataContract == null)
                {
                    throw ExceptionUtil.ThrowHelperError(new InvalidOperationException(SR.Format(SR.TypeHasNotBeenImported, typeName.Name, typeName.Namespace)));
                }
            }

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

            return(codeExporter.GetKnownTypeReferences(dataContract));
        }
        internal DataContract GetDataContract(Type clrType)
        {
            if (this.dataContractSurrogate == null)
            {
                return(DataContract.GetDataContract(clrType));
            }
            DataContract builtInDataContract = DataContract.GetBuiltInDataContract(clrType);

            if (builtInDataContract == null)
            {
                Type dataContractType = DataContractSurrogateCaller.GetDataContractType(this.dataContractSurrogate, clrType);
                builtInDataContract = DataContract.GetDataContract(dataContractType);
                if (!this.SurrogateDataTable.Contains(builtInDataContract))
                {
                    object obj2 = DataContractSurrogateCaller.GetCustomDataToExport(this.dataContractSurrogate, clrType, dataContractType);
                    if (obj2 != null)
                    {
                        this.SurrogateDataTable.Add(builtInDataContract, obj2);
                    }
                }
            }
            return(builtInDataContract);
        }
Example #13
0
        internal DataContract GetDataContract(Type clrType)
        {
#if SUPPORT_SURROGATE
            if (_dataContractSurrogate == null)
            {
                return(DataContract.GetDataContract(clrType));
            }
#endif
            DataContract?dataContract = DataContract.GetBuiltInDataContract(clrType);
            if (dataContract != null)
            {
                return(dataContract);
            }

#if SUPPORT_SURROGATE
            Type dcType = DataContractSurrogateCaller.GetDataContractType(_dataContractSurrogate, clrType);
            if (clrType.IsValueType != dcType.IsValueType)
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidDataContractException(SR.GetString(SR.ValueTypeMismatchInSurrogatedType, dcType, clrType)));
            }
#endif
            Type dcType = clrType;
            dataContract = DataContract.GetDataContract(dcType);
#if SUPPORT_SURROGATE
            if (!SurrogateDataTable.Contains(dataContract))
            {
                object customData = DataContractSurrogateCaller.GetCustomDataToExport(
                    _dataContractSurrogate, clrType, dcType);
                if (customData != null)
                {
                    SurrogateDataTable.Add(dataContract, customData);
                }
            }
#endif
            return(dataContract);
        }
Example #14
0
        private XmlElement ExportGenericInfo(Type clrType, string elementName, string elementNs)
        {
            Type itemType;
            int  nestedCollectionLevel = 0;

            while (CollectionDataContract.IsCollection(clrType, out 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);
        }
Example #15
0
 internal static PrimitiveDataContract?GetPrimitiveDataContract(string name, string ns)
 {
     return(DataContract.GetBuiltInDataContract(name, ns) as PrimitiveDataContract);
 }
Example #16
0
 internal static PrimitiveDataContract?GetPrimitiveDataContract(Type type)
 {
     return(DataContract.GetBuiltInDataContract(type) as PrimitiveDataContract);
 }