Fixed bug #670539.
var arr = type.GetCustomAttributes (typeof (T), inherit);
return arr != null && arr.Length == 1 ? (T) arr [0] : default (T);
}
+
+ public static IEnumerable<Type> GetInterfacesOrSelfInterface (this Type type)
+ {
+ if (type.IsInterface)
+ yield return type;
+ foreach (var t in type.GetInterfaces ())
+ yield return t;
+ }
}
internal sealed class KnownTypeCollection : Collection<Type>
internal Type GetSerializedType (Type type)
{
+ if (IsPrimitiveNotEnum (type))
+ return type;
Type element = GetCollectionElementType (type);
if (element == null)
return type;
{
if (type.IsArray)
return type.GetElementType ();
-
- Type [] ifaces = type.GetInterfaces ();
+ var ifaces = type.GetInterfacesOrSelfInterface ();
foreach (Type i in ifaces)
- if (i.IsGenericType && i.GetGenericTypeDefinition ().Equals (typeof (ICollection<>)))
+ if (i.IsGenericType && i.GetGenericTypeDefinition ().Equals (typeof (IEnumerable<>)))
return i.GetGenericArguments () [0];
foreach (Type i in ifaces)
if (i == typeof (IList))
static bool TypeImplementsIDictionary (Type type)
{
- foreach (var iface in type.GetInterfaces ())
+ foreach (var iface in type.GetInterfacesOrSelfInterface ())
if (iface == typeof (IDictionary) || (iface.IsGenericType && iface.GetGenericTypeDefinition () == typeof (IDictionary<,>)))
return true;
{
public override void ExportSchemaType (XsdDataContractExporter exporter)
{
- exporter.ExportStandardComplexType (RuntimeType.GetCustomAttribute<DataContractAttribute> (false), RuntimeType, Members);
+ exporter.ExportStandardComplexType (RuntimeType.GetCustomAttribute<DataContractAttribute> (false), this, Members);
}
}
{
public override void ExportSchemaType (XsdDataContractExporter exporter)
{
- exporter.ExportStandardComplexType (null, RuntimeType, Members);
+ exporter.ExportStandardComplexType (null, this, Members);
}
}
{
public override void ExportSchemaType (XsdDataContractExporter exporter)
{
- exporter.ExportListContractType (a, RuntimeType);
+ exporter.ExportListContractType (a, this);
}
}
{
public override void ExportSchemaType (XsdDataContractExporter exporter)
{
- exporter.ExportListContractType (null, RuntimeType);
+ exporter.ExportListContractType (null, this);
}
}
{
public override void ExportSchemaType (XsdDataContractExporter exporter)
{
- exporter.ExportDictionaryContractType (a, RuntimeType, GetGenericDictionaryInterface (RuntimeType));
+ exporter.ExportDictionaryContractType (a, this, GetGenericDictionaryInterface (RuntimeType));
}
}
{
public override void ExportSchemaType (XsdDataContractExporter exporter)
{
- exporter.ExportStandardComplexType (null, RuntimeType, Members);
+ exporter.ExportStandardComplexType (null, this, Members);
}
}
{
public override void ExportSchemaType (XsdDataContractExporter exporter)
{
- exporter.ExportEnumContractType (RuntimeType.GetCustomAttribute<DataContractAttribute> (false), RuntimeType);
+ exporter.ExportEnumContractType (RuntimeType.GetCustomAttribute<DataContractAttribute> (false), this);
}
}
}
static Type GetGenericCollectionInterface (Type type)
{
- foreach (var iface in type.GetInterfaces ())
- if (iface.IsGenericType && iface.GetGenericTypeDefinition () == typeof (ICollection<>))
+ foreach (var iface in type.GetInterfacesOrSelfInterface ())
+ if (iface.IsGenericType && iface.GetGenericTypeDefinition () == typeof (IEnumerable<>))
return iface;
return null;
static Type GetGenericDictionaryInterface (Type type)
{
- foreach (var iface in type.GetInterfaces ())
+ foreach (var iface in type.GetInterfacesOrSelfInterface ())
if (iface.IsGenericType && iface.GetGenericTypeDefinition () == typeof (IDictionary<,>))
return iface;
return true;
}
- internal void ExportDictionaryContractType (CollectionDataContractAttribute attr, Type type, Type dicType)
+ internal void ExportDictionaryContractType (CollectionDataContractAttribute attr, SerializationMap map, Type dicType)
{
- var qname = GetSchemaTypeName (type);
+ var type = map.RuntimeType;
+ var qname = map.XmlName;
var typeArgs = dicType.IsGenericType ? dicType.GetGenericArguments () : null;
var keyType = typeArgs != null ? typeArgs [0] : typeof (object);
dictSeq.Items.Add (new XmlSchemaElement () { Name = valueName, SchemaTypeName = GetSchemaTypeName (valueType), IsNillable = true });
}
- internal void ExportListContractType (CollectionDataContractAttribute attr, Type type)
+ internal void ExportListContractType (CollectionDataContractAttribute attr, CollectionTypeMap map)
{
- var qname = attr != null && attr.Name != null ? new QName (attr.Name, attr.Namespace ?? GetXmlNamespace (type)) : GetSchemaTypeName (type);
+ Type type = map.RuntimeType;
+ var qname = map.XmlName;
var typeArgs = type.IsGenericType ? type.GetGenericArguments () : null;
if (typeArgs != null && typeArgs.Length != 1)
*/
}
- internal void ExportEnumContractType (DataContractAttribute attr, Type type)
+ internal void ExportEnumContractType (DataContractAttribute attr, SerializationMap map)
{
- var qname = attr != null && attr.Name != null ? new QName (attr.Name, attr.Namespace ?? GetXmlNamespace (type)) : GetSchemaTypeName (type);
+ Type type = map.RuntimeType;
+ var qname = map.XmlName;
var st = CreateSimpleType (qname, type);
if (type.GetCustomAttribute<FlagsAttribute> (false) != null) {
var list = new XmlSchemaSimpleTypeList ();
return r;
}
- internal void ExportStandardComplexType (DataContractAttribute attr, Type type, List<DataMemberInfo> members)
+ internal void ExportStandardComplexType (DataContractAttribute attr, SerializationMap map, List<DataMemberInfo> members)
{
- var qname = attr != null && attr.Name != null ? new QName (attr.Name, attr.Namespace ?? GetXmlNamespace (type)) : GetSchemaTypeName (type);
+ Type type = map.RuntimeType;
+ var qname = map.XmlName;
var ct = CreateComplexType (qname, type);
if (type.BaseType != null && type.BaseType != typeof (object)) {
string GetXmlTypeName (Type type)
{
var qname = KnownTypeCollection.GetPrimitiveTypeName (type);
- return qname.Equals (QName.Empty) ? type.Name : qname.Name;
+ if (!qname.Equals (QName.Empty))
+ return qname.Name;
+ var ret = type.Name;
+ int idx = ret.IndexOf ('`');
+ return idx < 0 ? ret : ret.Substring (0, idx);
}
string GetXmlNamespace (Type type)
if (info != null && info.SchemaTypeName != null)
return info.SchemaTypeName;
+ known_types.Add (type);
+ var map = known_types.FindUserMap (type);
+ if (map != null)
+ return map.XmlName;
+
+ // The following lines would be mostly redundant (legacy code now that it widely uses SerializationMap.XmlName for consistency...)
+
var cdca = type.GetCustomAttribute<CollectionDataContractAttribute> (false);
if (cdca != null)
return new QName (cdca.Name ?? GetXmlTypeName (type), cdca.Namespace ?? GetXmlNamespace (type));
[TestFixture]
public class XsdDataContractExporterTest
{
+ internal const string MSSimpleNamespace =
+ "http://schemas.microsoft.com/2003/10/Serialization/";
+ internal const string MSArraysNamespace =
+ "http://schemas.microsoft.com/2003/10/Serialization/Arrays";
+ internal const string DefaultClrNamespaceBase =
+ "http://schemas.datacontract.org/2004/07/";
+
[Test]
public void Ctor1 ()
{
CheckDcFull (xdce.Schemas);
}
+ [Test]
+ public void GetSchemaTypeName ()
+ {
+ var xdce = new XsdDataContractExporter ();
+ // bug #670539
+ Assert.AreEqual (new XmlQualifiedName ("ArrayOfstring", MSArraysNamespace), xdce.GetSchemaTypeName (typeof (IEnumerable<string>)), "#1");
+ }
+
//Helper methods
XmlSchemas GetSchemas (XmlSchemaSet set)