示例#1
0
        static bool IsTypeReferenceable(Type type)
        {
            Type itemType;

            try
            {
                return(type.IsSerializable ||
                       type.IsDefined(Globals.TypeOfDataContractAttribute, false) ||
                       (Globals.TypeOfIXmlSerializable.IsAssignableFrom(type) && !type.IsGenericTypeDefinition) ||
                       CollectionDataContract.IsCollection(type, out itemType) ||
                       ClassDataContract.IsNonAttributedTypeValidForSerialization(type));
            }
            catch (Exception ex)
            {
                // An exception can be thrown in the designer when a project has a runtime binding redirection for a referenced assembly or a reference dependent assembly.
                // Type.IsDefined is known to throw System.IO.FileLoadException.
                // ClassDataContract.IsNonAttributedTypeValidForSerialization is known to throw System.IO.FileNotFoundException.
                // We guard against all non-critical exceptions.
                if (Fx.IsFatal(ex))
                {
                    throw;
                }
            }

            return(false);
        }
示例#2
0
        static bool IsTypeReferenceable(Type type)
        {
            Type itemType;

            return(type.IsSerializable ||
                   type.IsDefined(Globals.TypeOfDataContractAttribute, false) ||
                   (Globals.TypeOfIXmlSerializable.IsAssignableFrom(type) && !type.IsGenericTypeDefinition) ||
                   CollectionDataContract.IsCollection(type, out itemType) ||
                   ClassDataContract.IsNonAttributedTypeValidForSerialization(type));
        }
        private void AddCollectionItemTypeToKnownTypes(Type knownType)
        {
            Type type;

            for (Type type2 = knownType; CollectionDataContract.IsCollection(type2, out type); type2 = type)
            {
                if (type.IsGenericType && (type.GetGenericTypeDefinition() == Globals.TypeOfKeyValue))
                {
                    type = Globals.TypeOfKeyValuePair.MakeGenericType(type.GetGenericArguments());
                }
                this.knownTypeList.Add(type);
            }
        }
        private void AddCollectionItemTypeToKnownTypes(Type knownType)
        {
            Type itemType;
            Type typeToCheck = knownType;

            while (CollectionDataContract.IsCollection(typeToCheck, out itemType))
            {
                if (itemType.GetTypeInfo().IsGenericType&& (itemType.GetGenericTypeDefinition() == Globals.TypeOfKeyValue))
                {
                    itemType = Globals.TypeOfKeyValuePair.MakeGenericType(itemType.GetGenericArguments());
                }
                this.knownTypeList.Add(itemType);
                typeToCheck = itemType;
            }
        }
示例#5
0
        static bool IsTypeReferenceable(Type type)
        {
            Type itemType;

            try
            {
                return(type.IsSerializable ||
                       type.IsDefined(Globals.TypeOfDataContractAttribute, false) ||
                       (Globals.TypeOfIXmlSerializable.IsAssignableFrom(type) && !type.IsGenericTypeDefinition) ||
                       CollectionDataContract.IsCollection(type, out itemType) ||
                       ClassDataContract.IsNonAttributedTypeValidForSerialization(type));
            }
            catch (System.IO.FileLoadException)
            { // this can happen in Type.IsDefined when trying to load a referenced library that is not available at design time.
            }

            return(false);
        }
示例#6
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);
        }
        private static bool IsTypeReferenceable(Type type)
        {
            Type type2;

            if (((!type.IsSerializable && !type.IsDefined(Globals.TypeOfDataContractAttribute, false)) && (!Globals.TypeOfIXmlSerializable.IsAssignableFrom(type) || type.IsGenericTypeDefinition)) && !CollectionDataContract.IsCollection(type, out type2))
            {
                return(ClassDataContract.IsNonAttributedTypeValidForSerialization(type));
            }
            return(true);
        }