internal XmlSerializableMapping(XmlRootAttribute root, 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(root != null ? root.ElementName : _schemaTypeName.Name, root != null ? root.Namespace : 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."); } } }
internal XmlSerializableMapping(string elementName, string ns, TypeData typeData, string xmlType, string xmlTypeNamespace) : base(elementName, ns, typeData, xmlType, xmlTypeNamespace) { XmlSchemaProviderAttribute xmlSchemaProviderAttribute = (XmlSchemaProviderAttribute)Attribute.GetCustomAttribute(typeData.Type, typeof(XmlSchemaProviderAttribute)); if (xmlSchemaProviderAttribute != null) { string methodName = xmlSchemaProviderAttribute.MethodName; MethodInfo method = typeData.Type.GetMethod(methodName, BindingFlags.Static | BindingFlags.Public | BindingFlags.FlattenHierarchy); if (method == null) { throw new InvalidOperationException(string.Format("Type '{0}' must implement public static method '{1}'", typeData.Type, methodName)); } if (!typeof(XmlQualifiedName).IsAssignableFrom(method.ReturnType) && !typeof(XmlSchemaComplexType).IsAssignableFrom(method.ReturnType)) { throw new InvalidOperationException(string.Format("Method '{0}' indicated by XmlSchemaProviderAttribute must have its return type as XmlQualifiedName", methodName)); } XmlSchemaSet xmlSchemaSet = new XmlSchemaSet(); object obj = method.Invoke(null, new object[] { xmlSchemaSet }); this._schemaTypeName = XmlQualifiedName.Empty; if (obj == null) { return; } if (obj is XmlSchemaComplexType) { this._schemaType = (XmlSchemaComplexType)obj; if (!this._schemaType.QualifiedName.IsEmpty) { this._schemaTypeName = this._schemaType.QualifiedName; } else { this._schemaTypeName = new XmlQualifiedName(xmlType, xmlTypeNamespace); } } else { if (!(obj is XmlQualifiedName)) { 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, methodName)); } this._schemaTypeName = (XmlQualifiedName)obj; } base.UpdateRoot(new XmlQualifiedName(this._schemaTypeName.Name, base.Namespace ?? this._schemaTypeName.Namespace)); base.XmlTypeNamespace = this._schemaTypeName.Namespace; base.XmlType = this._schemaTypeName.Name; if (!this._schemaTypeName.IsEmpty && xmlSchemaSet.Count > 0) { XmlSchema[] array = new XmlSchema[xmlSchemaSet.Count]; xmlSchemaSet.CopyTo(array, 0); this._schema = array[0]; } return; } else { IXmlSerializable xmlSerializable = (IXmlSerializable)Activator.CreateInstance(typeData.Type, true); try { this._schema = xmlSerializable.GetSchema(); } catch (Exception) { } if (this._schema != null && (this._schema.Id == null || this._schema.Id.Length == 0)) { throw new InvalidOperationException("Schema Id is missing. The schema returned from " + typeData.Type.FullName + ".GetSchema() must have an Id."); } return; } }
internal static MethodInfo GetMethodFromSchemaProvider(XmlSchemaProviderAttribute provider, Type type) { if (provider.IsAny) { // do not validate the schema provider method for wildcard types. return null; } else if (provider.MethodName == null) { throw new ArgumentNullException("MethodName"); } if (!CodeGenerator.IsValidLanguageIndependentIdentifier(provider.MethodName)) throw new ArgumentException(Res.GetString(Res.XmlGetSchemaMethodName, provider.MethodName), "MethodName"); MethodInfo getMethod = getMethod = type.GetMethod(provider.MethodName, /* BindingFlags.DeclaredOnly | */ BindingFlags.Static | BindingFlags.Public, null, new Type[] {typeof(XmlSchemaSet)}, null); if (getMethod == null) throw new InvalidOperationException(Res.GetString(Res.XmlGetSchemaMethodMissing, provider.MethodName, typeof(XmlSchemaSet).Name, type.FullName)); if (!(typeof(XmlQualifiedName).IsAssignableFrom(getMethod.ReturnType)) && !(typeof(XmlSchemaType).IsAssignableFrom(getMethod.ReturnType))) throw new InvalidOperationException(Res.GetString(Res.XmlGetSchemaMethodReturnType, type.Name, provider.MethodName, typeof(XmlSchemaProviderAttribute).Name, typeof(XmlQualifiedName).FullName, typeof(XmlSchemaType).FullName)); return getMethod; }
internal static MethodInfo GetMethodFromSchemaProvider(XmlSchemaProviderAttribute provider, Type type) { MethodInfo info; if (provider.IsAny) { return null; } if (provider.MethodName == null) { throw new ArgumentNullException("MethodName"); } if (!CodeGenerator.IsValidLanguageIndependentIdentifier(provider.MethodName)) { throw new ArgumentException(Res.GetString("XmlGetSchemaMethodName", new object[] { provider.MethodName }), "MethodName"); } info = info = type.GetMethod(provider.MethodName, BindingFlags.Public | BindingFlags.Static, null, new Type[] { typeof(XmlSchemaSet) }, null); if (info == null) { throw new InvalidOperationException(Res.GetString("XmlGetSchemaMethodMissing", new object[] { provider.MethodName, typeof(XmlSchemaSet).Name, type.FullName })); } if (!typeof(XmlQualifiedName).IsAssignableFrom(info.ReturnType) && !typeof(XmlSchemaType).IsAssignableFrom(info.ReturnType)) { throw new InvalidOperationException(Res.GetString("XmlGetSchemaMethodReturnType", new object[] { type.Name, provider.MethodName, typeof(XmlSchemaProviderAttribute).Name, typeof(XmlQualifiedName).FullName, typeof(XmlSchemaType).FullName })); } return info; }