}
}
+ // FIXME: xsd types and ms serialization types should be differentiated.
internal static Type GetPrimitiveTypeFromName (string name)
{
switch (name) {
protected override void InsertItem (int index, Type type)
{
- if (TryRegister (type))
+ if (!Contains (type) && TryRegister (type))
base.InsertItem (index, type);
}
protected override void SetItem (int index, Type type)
{
- if (index == Count)
- InsertItem (index, type);
- else {
+ if (index != Count)
RemoveItem (index);
- if (TryRegister (type))
- base.InsertItem (index - 1, type);
- }
+ if (TryRegister (type))
+ base.InsertItem (index - 1, type);
}
internal SerializationMap FindUserMap (QName qname)
{
- for (int i = 0; i < contracts.Count; i++)
- if (qname == contracts [i].XmlName)
- return contracts [i];
- return null;
+ return contracts.FirstOrDefault (c => c.XmlName == qname);
}
internal Type GetSerializedType (Type type)
internal QName GetQName (Type type)
{
- if (IsPrimitiveNotEnum (type))
- return GetPrimitiveTypeName (type);
-
SerializationMap map = FindUserMap (type);
if (map != null)
// already mapped.
return map.XmlName;
+ return GetStaticQName (type);
+ }
+
+ public static QName GetStaticQName (Type type)
+ {
+ if (IsPrimitiveNotEnum (type))
+ return GetPrimitiveTypeName (type);
if (type.IsEnum)
return GetEnumQName (type);
if (GetAttribute<SerializableAttribute> (type) != null)
return GetSerializableQName (type);
- // FIXME: it needs in-depth check.
- return QName.Empty;
+ // default type map - still uses GetContractQName().
+ return GetContractQName (type, null, null);
}
- QName GetContractQName (Type type)
+ internal static QName GetContractQName (Type type)
{
var a = GetAttribute<DataContractAttribute> (type);
return a == null ? null : GetContractQName (type, a.Name, a.Namespace);
}
- QName GetCollectionContractQName (Type type)
+ static QName GetCollectionContractQName (Type type)
{
var a = GetAttribute<CollectionDataContractAttribute> (type);
return a == null ? null : GetContractQName (type, a.Name, a.Namespace);
}
- internal static QName GetContractQName (Type type, string name, string ns)
+ static QName GetContractQName (Type type, string name, string ns)
{
- if (name == null) {
- // FIXME: there could be decent ways to get
- // the same result...
- name = type.Namespace == null || type.Namespace.Length == 0 ? type.Name : type.FullName.Substring (type.Namespace.Length + 1).Replace ('+', '.');
- if (type.IsGenericType) {
- name = name.Substring (0, name.IndexOf ('`')) + "Of";
- foreach (var t in type.GetGenericArguments ())
- name += t.Name; // FIXME: check namespaces too
- }
- }
+ if (name == null)
+ name = GetDefaultName (type);
if (ns == null)
- ns = DefaultClrNamespaceBase + type.Namespace;
+ ns = GetDefaultNamespace (type);
return new QName (name, ns);
}
- private QName GetEnumQName (Type type)
+ static QName GetEnumQName (Type type)
{
string name = null, ns = null;
}
if (ns == null)
- ns = DefaultClrNamespaceBase + type.Namespace;
+ ns = GetDefaultNamespace (type);
if (name == null)
- name = type.Namespace == null || type.Namespace.Length == 0 ? type.Name : type.FullName.Substring (type.Namespace.Length + 1).Replace ('+', '.');
+ name = type.Namespace == null ? type.Name : type.FullName.Substring (type.Namespace.Length + 1).Replace ('+', '.');
return new QName (name, ns);
}
- private QName GetCollectionQName (Type element)
+ internal static string GetDefaultName (Type type)
+ {
+ // FIXME: there could be decent ways to get
+ // the same result...
+ string name = type.Namespace == null || type.Namespace.Length == 0 ? type.Name : type.FullName.Substring (type.Namespace.Length + 1).Replace ('+', '.');
+ if (type.IsGenericType) {
+ name = name.Substring (0, name.IndexOf ('`')) + "Of";
+ foreach (var t in type.GetGenericArguments ())
+ name += t.Name; // FIXME: check namespaces too
+ }
+ return name;
+ }
+
+ internal static string GetDefaultNamespace (Type type)
+ {
+ foreach (ContractNamespaceAttribute a in type.Assembly.GetCustomAttributes (typeof (ContractNamespaceAttribute), true))
+ if (a.ClrNamespace == type.Namespace)
+ return a.ContractNamespace;
+ return DefaultClrNamespaceBase + type.Namespace;
+ }
+
+ static QName GetCollectionQName (Type element)
{
- QName eqname = GetQName (element);
+ QName eqname = GetStaticQName (element);
string ns = eqname.Namespace;
if (eqname.Namespace == MSSimpleNamespace)
ns);
}
- private QName GetSerializableQName (Type type)
- {
+ static QName GetSerializableQName (Type type)
+ {
+#if !NET_2_1
+ // First, check XmlSchemaProviderAttribute and try GetSchema() to see if it returns a schema in the expected format.
+ var xpa = type.GetCustomAttribute<XmlSchemaProviderAttribute> (true);
+ if (xpa != null) {
+ var mi = type.GetMethod (xpa.MethodName, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static);
+ if (mi != null) {
+ try {
+ var xss = new XmlSchemaSet ();
+ return (XmlQualifiedName) mi.Invoke (null, new object [] {xss});
+ } catch {
+ // ignore.
+ }
+ }
+ }
+#endif
+
string xmlName = type.Name;
if (type.IsGenericType) {
xmlName = xmlName.Substring (0, xmlName.IndexOf ('`')) + "Of";
foreach (var t in type.GetGenericArguments ())
- xmlName += GetQName (t).Name; // FIXME: check namespaces too
+ xmlName += GetStaticQName (t).Name; // FIXME: check namespaces too
}
- string xmlNamespace = DefaultClrNamespaceBase + type.Namespace;
+ string xmlNamespace = GetDefaultNamespace (type);
var x = GetAttribute<XmlRootAttribute> (type);
if (x != null) {
xmlName = x.ElementName;
return new QName (XmlConvert.EncodeLocalName (xmlName), xmlNamespace);
}
- internal bool IsPrimitiveNotEnum (Type type)
+ static bool IsPrimitiveNotEnum (Type type)
{
if (type.IsEnum)
return false;
if (RegisterEnum (type) != null)
return true;
- if (RegisterContract (type) != null)
+ if (RegisterDictionary (type) != null)
return true;
- if (RegisterIXmlSerializable (type) != null)
+ if (RegisterCollectionContract (type) != null)
return true;
- if (RegisterDictionary (type) != null)
+ if (RegisterContract (type) != null)
return true;
- if (RegisterCollectionContract (type) != null)
+ if (RegisterIXmlSerializable (type) != null)
return true;
if (RegisterCollection (type) != null)
return true;
}
- internal static Type GetCollectionElementType (Type type)
+ static Type GetCollectionElementType (Type type)
{
if (type.IsArray)
return type.GetElementType ();
return null;
}
- internal T GetAttribute<T> (MemberInfo mi) where T : Attribute
+ internal static T GetAttribute<T> (ICustomAttributeProvider ap) where T : Attribute
{
- object [] atts = mi.GetCustomAttributes (typeof (T), false);
+ object [] atts = ap.GetCustomAttributes (typeof (T), false);
return atts.Length == 0 ? null : (T) atts [0];
}
QName qname = GetCollectionQName (element);
- if (FindUserMap (qname) != null)
- throw new InvalidOperationException (String.Format ("Failed to add type {0} to known type collection. There already is a registered type for XML name {1}", type, qname));
+ var map = FindUserMap (qname);
+ if (map != null) {
+ var cmap = map as CollectionTypeMap;
+ if (cmap == null || cmap.RuntimeType != type)
+ throw new InvalidOperationException (String.Format ("Failed to add type {0} to known type collection. There already is a registered type for XML name {1}", type, qname));
+ return cmap;
+ }
CollectionTypeMap ret =
new CollectionTypeMap (type, element, qname, this);
return ret;
}
+ static bool TypeImplementsIDictionary (Type type)
+ {
+ foreach (var iface in type.GetInterfaces ())
+ if (iface == typeof (IDictionary) || (iface.IsGenericType && iface.GetGenericTypeDefinition () == typeof (IDictionary<,>)))
+ return true;
+
+ return false;
+ }
+
// it also supports contract-based dictionary.
private DictionaryTypeMap RegisterDictionary (Type type)
{
- if (!type.GetInterfaces ().Any (t => t == typeof (IDictionary) || t.FullName.StartsWith ("System.Collections.Generic.IDictionary")))
+ if (!TypeImplementsIDictionary (type))
return null;
var cdca = GetAttribute<CollectionDataContractAttribute> (type);
if (FindUserMap (qname) != null)
throw new InvalidOperationException (String.Format ("There is already a registered type for XML name {0}", qname));
- SharedTypeMap ret =
- new SharedTypeMap (type, qname, this);
+ SharedTypeMap ret = new SharedTypeMap (type, qname, this);
contracts.Add (ret);
+ ret.Initialize ();
return ret;
}
if (FindUserMap (qname) != null)
throw new InvalidOperationException (String.Format ("There is already a registered type for XML name {0}", qname));
- SharedContractMap ret =
- new SharedContractMap (type, qname, this);
+ SharedContractMap ret = new SharedContractMap (type, qname, this);
contracts.Add (ret);
+ ret.Initialize ();
+
+ if (type.BaseType != typeof (object)) {
+ TryRegister (type.BaseType);
+ if (!FindUserMap (type.BaseType).IsContractAllowedType)
+ throw new InvalidDataContractException (String.Format ("To be serializable by data contract, type '{0}' cannot inherit from non-contract and non-Serializable type '{1}'", type, type.BaseType));
+ }
object [] attrs = type.GetCustomAttributes (typeof (KnownTypeAttribute), true);
for (int i = 0; i < attrs.Length; i++) {
{
DefaultTypeMap ret = new DefaultTypeMap (type, this);
contracts.Add (ret);
+ ret.Initialize ();
return ret;
}