Esempio n. 1
0
            internal XmlDataContractCriticalHelper(
                [DynamicallyAccessedMembers(ClassDataContract.DataContractPreserveMemberTypes)]
                Type type) : base(type)
            {
                if (type.IsDefined(Globals.TypeOfDataContractAttribute, false))
                {
                    throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidDataContractException(SR.Format(SR.IXmlSerializableCannotHaveDataContract, DataContract.GetClrTypeFullName(type))));
                }
                if (type.IsDefined(Globals.TypeOfCollectionDataContractAttribute, false))
                {
                    throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidDataContractException(SR.Format(SR.IXmlSerializableCannotHaveCollectionDataContract, DataContract.GetClrTypeFullName(type))));
                }
                bool             hasRoot;
                XmlSchemaType?   xsdType;
                XmlQualifiedName xmlName;

                SchemaExporter.GetXmlTypeInfo(type, out xmlName, out xsdType, out hasRoot);
                XmlName = xmlName;
                XsdType = xsdType;
                HasRoot = hasRoot;
                XmlDictionary dictionary = new XmlDictionary();

                Name      = dictionary.Add(XmlName.Name);
                Namespace = dictionary.Add(XmlName.Namespace);
                object[]? xmlRootAttributes = UnderlyingType?.GetCustomAttributes(Globals.TypeOfXmlRootAttribute, false).ToArray();
                if (xmlRootAttributes == null || xmlRootAttributes.Length == 0)
                {
                    if (hasRoot)
                    {
                        _topLevelElementName       = Name;
                        _topLevelElementNamespace  = (this.XmlName.Namespace == Globals.SchemaNamespace) ? DictionaryGlobals.EmptyString : Namespace;
                        _isTopLevelElementNullable = true;
                    }
                }
                else
                {
                    if (hasRoot)
                    {
                        XmlRootAttribute xmlRootAttribute = (XmlRootAttribute)xmlRootAttributes[0];
                        _isTopLevelElementNullable = xmlRootAttribute.IsNullable;
                        string elementName = xmlRootAttribute.ElementName;
                        _topLevelElementName = (elementName == null || elementName.Length == 0) ? Name : dictionary.Add(DataContract.EncodeLocalName(elementName));
                        string?elementNs = xmlRootAttribute.Namespace;
                        _topLevelElementNamespace = (elementNs == null || elementNs.Length == 0) ? DictionaryGlobals.EmptyString : dictionary.Add(elementNs);
                    }
                    else
                    {
                        throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidDataContractException(SR.Format(SR.IsAnyCannotHaveXmlRoot, DataContract.GetClrTypeFullName(UnderlyingType !))));
                    }
                }
            }
Esempio n. 2
0
            internal XmlDataContractCriticalHelper(Type type)
                : base(type)
            {
                if (type.IsDefined(Globals.TypeOfDataContractAttribute, false))
                {
                    throw Fx.Exception.AsError(new InvalidDataContractException(SR.IXmlSerializableCannotHaveDataContract(DataContract.GetClrTypeFullName(type))));
                }
                if (type.IsDefined(Globals.TypeOfCollectionDataContractAttribute, false))
                {
                    throw Fx.Exception.AsError(new InvalidDataContractException(SR.IXmlSerializableCannotHaveCollectionDataContract(DataContract.GetClrTypeFullName(type))));
                }
                XmlSchemaType    xsdType;
                bool             hasRoot;
                XmlQualifiedName stableName;

                SchemaExporter.GetXmlTypeInfo(type, out stableName, out xsdType, out hasRoot);
                this.StableName = stableName;
                this.XsdType    = xsdType;
                this.HasRoot    = hasRoot;
                XmlDictionary dictionary = new XmlDictionary();

                this.Name      = dictionary.Add(StableName.Name);
                this.Namespace = dictionary.Add(StableName.Namespace);
                object[] xmlRootAttributes = (UnderlyingType == null) ? null : UnderlyingType.GetCustomAttributes(Globals.TypeOfXmlRootAttribute, false);
                if (xmlRootAttributes == null || xmlRootAttributes.Length == 0)
                {
                    if (hasRoot)
                    {
                        topLevelElementName       = Name;
                        topLevelElementNamespace  = (this.StableName.Namespace == Globals.SchemaNamespace) ? DictionaryGlobals.EmptyString : Namespace;
                        isTopLevelElementNullable = true;
                    }
                }
                else
                {
                    if (hasRoot)
                    {
                        XmlRootAttribute xmlRootAttribute = (XmlRootAttribute)xmlRootAttributes[0];
                        isTopLevelElementNullable = xmlRootAttribute.IsNullable;
                        string elementName = xmlRootAttribute.ElementName;
                        topLevelElementName = (elementName == null || elementName.Length == 0) ? Name : dictionary.Add(DataContract.EncodeLocalName(elementName));
                        string elementNs = xmlRootAttribute.Namespace;
                        topLevelElementNamespace = (elementNs == null || elementNs.Length == 0) ? DictionaryGlobals.EmptyString : dictionary.Add(elementNs);
                    }
                    else
                    {
                        throw Fx.Exception.AsError(new InvalidDataContractException(SR.IsAnyCannotHaveXmlRoot(DataContract.GetClrTypeFullName(UnderlyingType))));
                    }
                }
            }
        object[] ICustomAttributeProvider.GetCustomAttributes(Type attributeType, bool inherit)
        {
            Attribute result;

            if (TryGetCustomAttribute(attributeType, out result))
            {
                if (result != null)
                {
                    return(new Attribute[] { result });
                }
                if (s_EmptyAttributes == null)
                {
                    s_EmptyAttributes = new Attribute[0];
                }
                return(s_EmptyAttributes);
            }
            return(UnderlyingType.GetCustomAttributes(attributeType, inherit));
        }
Esempio n. 4
0
 T GetCustomAttribute <T>(bool inherit = true)
 {
     return(UnderlyingType.GetCustomAttributes(typeof(T), true).OfType <T>().FirstOrDefault());
 }
 object[] ICustomAttributeProvider.GetCustomAttributes(bool inherit)
 {
     return(UnderlyingType.GetCustomAttributes(inherit));
 }