Beispiel #1
0
        private static bool InvokeSchemaProviderMethod(Type clrType, XmlSchemaSet schemas, out XmlQualifiedName stableName, out XmlSchemaType xsdType, out bool hasRoot)
        {
            xsdType = null;
            hasRoot = true;
            object[] attrs = clrType.GetCustomAttributes(Globals.TypeOfXmlSchemaProviderAttribute, false).ToArray();
            if (attrs == null || attrs.Length == 0)
            {
                stableName = DataContract.GetDefaultStableName(clrType);
                return(false);
            }

            XmlSchemaProviderAttribute provider = (XmlSchemaProviderAttribute)attrs[0];

            if (provider.IsAny)
            {
                xsdType = CreateAnyElementType();
                hasRoot = false;
            }
            string methodName = provider.MethodName;

            if (methodName == null || methodName.Length == 0)
            {
                if (!provider.IsAny)
                {
                    throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidDataContractException(SR.Format(SR.InvalidGetSchemaMethod, DataContract.GetClrTypeFullName(clrType))));
                }
                stableName = DataContract.GetDefaultStableName(clrType);
            }
            else
            {
                MethodInfo getMethod = clrType.GetMethod(methodName, /*BindingFlags.DeclaredOnly |*/ BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.Public, new Type[] { typeof(XmlSchemaSet) });
                if (getMethod == null)
                {
                    throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidDataContractException(SR.Format(SR.MissingGetSchemaMethod, DataContract.GetClrTypeFullName(clrType), methodName)));
                }

                if (!(Globals.TypeOfXmlQualifiedName.IsAssignableFrom(getMethod.ReturnType)))
                {
                    throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidDataContractException(SR.Format(SR.InvalidReturnTypeOnGetSchemaMethod, DataContract.GetClrTypeFullName(clrType), methodName, DataContract.GetClrTypeFullName(getMethod.ReturnType), DataContract.GetClrTypeFullName(Globals.TypeOfXmlQualifiedName))));
                }

                object typeInfo = getMethod.Invoke(null, new object[] { schemas });

                if (provider.IsAny)
                {
                    if (typeInfo != null)
                    {
                        throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidDataContractException(SR.Format(SR.InvalidNonNullReturnValueByIsAny, DataContract.GetClrTypeFullName(clrType), methodName)));
                    }
                    stableName = DataContract.GetDefaultStableName(clrType);
                }
                else if (typeInfo == null)
                {
                    xsdType    = CreateAnyElementType();
                    hasRoot    = false;
                    stableName = DataContract.GetDefaultStableName(clrType);
                }
                else
                {
                    stableName = (XmlQualifiedName)typeInfo;
                }
            }
            return(true);
        }
Beispiel #2
0
        private static bool InvokeSchemaProviderMethod(Type clrType, XmlSchemaSet schemas, out XmlQualifiedName stableName, out XmlSchemaType xsdType, out bool hasRoot)
        {
            xsdType = null;
            hasRoot = true;
            object[] attrs = clrType.GetCustomAttributes(Globals.TypeOfXmlSchemaProviderAttribute, false);
            if (attrs == null || attrs.Length == 0)
            {
                stableName = DataContract.GetDefaultStableName(clrType);
                return(false);
            }

            XmlSchemaProviderAttribute provider = (XmlSchemaProviderAttribute)attrs[0];

            if (provider.IsAny)
            {
                xsdType = CreateAnyElementType();
                hasRoot = false;
            }
            string methodName = provider.MethodName;

            if (methodName == null || methodName.Length == 0)
            {
                if (!provider.IsAny)
                {
                    throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidDataContractException(SR.Format(SR.InvalidGetSchemaMethod, DataContract.GetClrTypeFullName(clrType))));
                }
                stableName = DataContract.GetDefaultStableName(clrType);
            }
            else
            {
                MethodInfo getMethod = clrType.GetMethod(methodName, /*BindingFlags.DeclaredOnly |*/ BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.Public, null, new Type[] { typeof(XmlSchemaSet) }, null);
                if (getMethod == null)
                {
                    throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidDataContractException(SR.Format(SR.MissingGetSchemaMethod, DataContract.GetClrTypeFullName(clrType), methodName)));
                }

                if (!(Globals.TypeOfXmlQualifiedName.IsAssignableFrom(getMethod.ReturnType)) && !(Globals.TypeOfXmlSchemaType.IsAssignableFrom(getMethod.ReturnType)))
                {
                    throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidDataContractException(SR.Format(SR.InvalidReturnTypeOnGetSchemaMethod, DataContract.GetClrTypeFullName(clrType), methodName, DataContract.GetClrTypeFullName(getMethod.ReturnType), DataContract.GetClrTypeFullName(Globals.TypeOfXmlQualifiedName), typeof(XmlSchemaType))));
                }

                object typeInfo = getMethod.Invoke(null, new object[] { schemas });

                if (provider.IsAny)
                {
                    if (typeInfo != null)
                    {
                        throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidDataContractException(SR.Format(SR.InvalidNonNullReturnValueByIsAny, DataContract.GetClrTypeFullName(clrType), methodName)));
                    }
                    stableName = DataContract.GetDefaultStableName(clrType);
                }
                else if (typeInfo == null)
                {
                    xsdType    = CreateAnyElementType();
                    hasRoot    = false;
                    stableName = DataContract.GetDefaultStableName(clrType);
                }
                else
                {
                    XmlSchemaType providerXsdType = typeInfo as XmlSchemaType;
                    if (providerXsdType != null)
                    {
                        string typeName = providerXsdType.Name;
                        string typeNs   = null;
                        if (typeName == null || typeName.Length == 0)
                        {
                            DataContract.GetDefaultStableName(DataContract.GetClrTypeFullName(clrType), out typeName, out typeNs);
                            stableName = new XmlQualifiedName(typeName, typeNs);
                            providerXsdType.Annotation = GetSchemaAnnotation(ExportActualType(stableName, new XmlDocument()));
                            xsdType = providerXsdType;
                        }
                        else
                        {
                            foreach (XmlSchema schema in schemas.Schemas())
                            {
                                foreach (XmlSchemaObject schemaItem in schema.Items)
                                {
                                    if ((object)schemaItem == (object)providerXsdType)
                                    {
                                        typeNs = schema.TargetNamespace;
                                        if (typeNs == null)
                                        {
                                            typeNs = String.Empty;
                                        }
                                        break;
                                    }
                                }
                                if (typeNs != null)
                                {
                                    break;
                                }
                            }
                            if (typeNs == null)
                            {
                                throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidDataContractException(SR.Format(SR.MissingSchemaType, typeName, DataContract.GetClrTypeFullName(clrType))));
                            }
                            stableName = new XmlQualifiedName(typeName, typeNs);
                        }
                    }
                    else
                    {
                        stableName = (XmlQualifiedName)typeInfo;
                    }
                }
            }
            return(true);
        }
Beispiel #3
0
        internal XmlSerializableMapping(string elementName, string ns, TypeData typeData, string xmlType, string xmlTypeNamespace)
            : base(elementName, ns, typeData, xmlType, xmlTypeNamespace)
        {
#if NET_2_0
            XmlSchemaProviderAttribute schemaProvider = (XmlSchemaProviderAttribute)Attribute.GetCustomAttribute(typeData.Type, typeof(XmlSchemaProviderAttribute));

            if (schemaProvider != null)
            {
                string     method = schemaProvider.MethodName;
                MethodInfo mi     = typeData.Type.GetMethod(method, BindingFlags.Static | BindingFlags.Public | BindingFlags.FlattenHierarchy);
                if (mi == null)
                {
                    throw new InvalidOperationException(String.Format("Type '{0}' must implement public static method '{1}'", typeData.Type, method));
                }
                if (!typeof(XmlQualifiedName).IsAssignableFrom(mi.ReturnType) &&
                    // LAMESPEC: it is undocumented. (We don't have to tell users about it in the error message.)
                    // Also do not add such a silly compatibility test to assert that it does not raise an error.
                    !typeof(XmlSchemaComplexType).IsAssignableFrom(mi.ReturnType))
                {
                    throw new InvalidOperationException(String.Format("Method '{0}' indicated by XmlSchemaProviderAttribute must have its return type as XmlQualifiedName", method));
                }
                XmlSchemaSet xs     = new XmlSchemaSet();
                object       retVal = mi.Invoke(null, new object [] { xs });
                _schemaTypeName = XmlQualifiedName.Empty;
                if (retVal == null)
                {
                    return;
                }

                if (retVal is XmlSchemaComplexType)
                {
                    _schemaType = (XmlSchemaComplexType)retVal;
                    if (!_schemaType.QualifiedName.IsEmpty)
                    {
                        _schemaTypeName = _schemaType.QualifiedName;
                    }
                    else
                    {
                        _schemaTypeName = new XmlQualifiedName(xmlType, xmlTypeNamespace);
                    }
                }
                else if (retVal is XmlQualifiedName)
                {
                    _schemaTypeName = (XmlQualifiedName)retVal;
                }
                else
                {
                    throw new InvalidOperationException(
                              String.Format("Method {0}.{1}() specified by XmlSchemaProviderAttribute has invalid signature: return type must be compatible with System.Xml.XmlQualifiedName.", typeData.Type.Name, method));
                }

                // defaultNamespace at XmlReflectionImporter takes precedence for Namespace, but not for XsdTypeNamespace.
                UpdateRoot(new XmlQualifiedName(_schemaTypeName.Name, Namespace ?? _schemaTypeName.Namespace));
                XmlTypeNamespace = _schemaTypeName.Namespace;
                XmlType          = _schemaTypeName.Name;

                if (!_schemaTypeName.IsEmpty && xs.Count > 0)
                {
                    XmlSchema [] schemas = new XmlSchema [xs.Count];
                    xs.CopyTo(schemas, 0);
                    _schema = schemas [0];
                }

                return;
            }
#endif
            IXmlSerializable serializable = (IXmlSerializable)Activator.CreateInstance(typeData.Type, true);
#if NET_2_0
            try {
                _schema = serializable.GetSchema();
            } catch (Exception) {
                // LAMESPEC: .NET has a bad exception catch and swallows it silently.
            }
#else
            _schema = serializable.GetSchema();
#endif
            if (_schema != null)
            {
                if (_schema.Id == null || _schema.Id.Length == 0)
                {
                    throw new InvalidOperationException("Schema Id is missing. The schema returned from " + typeData.Type.FullName + ".GetSchema() must have an Id.");
                }
            }
        }