Author: atsushi
Date: 2006-11-22 06:37:28 -0500 (Wed, 22 Nov 2006)
New Revision: 68341
Modified:
trunk/mcs/class/System.XML/System.Xml.Serialization/ChangeLog
trunk/mcs/class/System.XML/System.Xml.Serialization/TypeData.cs
trunk/mcs/class/System.XML/System.Xml.Serialization/TypeTranslator.cs
trunk/mcs/class/System.XML/System.Xml.Serialization/XmlReflectionImporter.cs
trunk/mcs/class/System.XML/Test/System.Xml.Serialization/ChangeLog
trunk/mcs/class/System.XML/Test/System.Xml.Serialization/XmlReflectionImporterTests.cs
Log:
2006-11-22 Atsushi Enomoto <[EMAIL PROTECTED]>
Fixed bug #78611.
* TypeData.cs, TypeTranslator.cs: improved TypeData to actually store
"nullable" information. Introduced new primitiveNullableTypes pool
whose TypeData has 'true' for new IsNullable property. This
property is also used for non-primitive types.
* XmlReflectionImporter.cs : there, use TypeData.IsNullable instead
of IsValueType, to handle nullable types.
* XmlReflectionImporterTests.cs : added test for class that contains
nullable value type field whose XmlElementAttribute IsNullable.
Modified: trunk/mcs/class/System.XML/System.Xml.Serialization/ChangeLog
===================================================================
--- trunk/mcs/class/System.XML/System.Xml.Serialization/ChangeLog
2006-11-22 11:27:33 UTC (rev 68340)
+++ trunk/mcs/class/System.XML/System.Xml.Serialization/ChangeLog
2006-11-22 11:37:28 UTC (rev 68341)
@@ -1,3 +1,13 @@
+2006-11-22 Atsushi Enomoto <[EMAIL PROTECTED]>
+
+ Fixed bug #78611.
+ * TypeData.cs, TypeTranslator.cs: improved TypeData to actually store
+ "nullable" information. Introduced new primitiveNullableTypes pool
+ whose TypeData has 'true' for new IsNullable property. This
+ property is also used for non-primitive types.
+ * XmlReflectionImporter.cs : there, use TypeData.IsNullable instead
+ of IsValueType, to handle nullable types.
+
2006-11-20 Atsushi Enomoto <[EMAIL PROTECTED]>
* TypeTranslator.cs : Hari knew better way to do it.
Modified: trunk/mcs/class/System.XML/System.Xml.Serialization/TypeData.cs
===================================================================
--- trunk/mcs/class/System.XML/System.Xml.Serialization/TypeData.cs
2006-11-22 11:27:33 UTC (rev 68340)
+++ trunk/mcs/class/System.XML/System.Xml.Serialization/TypeData.cs
2006-11-22 11:37:28 UTC (rev 68341)
@@ -54,6 +54,7 @@
TypeData mappedType;
XmlSchemaPatternFacet facet;
bool hasPublicConstructor = true;
+ bool genericNullable;
public TypeData (Type type, string elementName, bool
isPrimitive) :
this(type, elementName, isPrimitive, null, null) {}
@@ -216,6 +217,31 @@
}
}
+ public bool IsNullable
+ {
+ get
+ {
+ if (genericNullable)
+ return true;
+#if NET_2_0
+ return !IsValueType ||
+ (type != null &&
+ type.IsGenericType &&
+ type.GetGenericTypeDefinition () ==
typeof (Nullable<>));
+#else
+ return !IsValueType;
+#endif
+ }
+ }
+
+ public bool IsGenericNullable
+ {
+ set
+ {
+ genericNullable = value;
+ }
+ }
+
public TypeData ListItemTypeData
{
get
Modified: trunk/mcs/class/System.XML/System.Xml.Serialization/TypeTranslator.cs
===================================================================
--- trunk/mcs/class/System.XML/System.Xml.Serialization/TypeTranslator.cs
2006-11-22 11:27:33 UTC (rev 68340)
+++ trunk/mcs/class/System.XML/System.Xml.Serialization/TypeTranslator.cs
2006-11-22 11:37:28 UTC (rev 68341)
@@ -43,6 +43,7 @@
static Hashtable nameCache;
static Hashtable primitiveTypes;
static Hashtable primitiveArrayTypes;
+ static Hashtable primitiveNullableTypes;
#if TARGET_JVM
const string AppDomainCacheName =
"System.Xml.Serialization.TypeTranslator.AppDomainCache";
@@ -133,6 +134,16 @@
primitiveTypes.Add ("normalizedString", new TypeData
(typeof (string), "normalizedString", true));
primitiveTypes.Add ("anyURI", new TypeData (typeof
(string), "anyURI", true));
primitiveTypes.Add ("base64", new TypeData (typeof
(byte[]), "base64", true));
+
+#if NET_2_0
+ primitiveNullableTypes = new Hashtable ();
+ foreach (DictionaryEntry de in primitiveTypes) {
+ TypeData td = (TypeData) de.Value;
+ TypeData ntd = new TypeData (td.Type,
td.XmlType, true);
+ td.IsGenericNullable = true;
+ primitiveNullableTypes.Add (de.Key, td);
+ }
+#endif
}
public static TypeData GetTypeData (Type type)
@@ -140,12 +151,28 @@
return GetTypeData (type, null);
}
- public static TypeData GetTypeData (Type type, string
xmlDataType)
+ public static TypeData GetTypeData (Type runtimeType, string
xmlDataType)
{
+ Type type = runtimeType;
+ bool isNullableRuntimeType = false;
+
#if NET_2_0
// Nullable<T> is serialized as T
if (type.IsGenericType && type.GetGenericTypeDefinition
() == typeof (Nullable<>)) {
+ isNullableRuntimeType = true;
type = type.GetGenericArguments () [0];
+
+ TypeData pt = GetTypeData (type); // beware
this recursive call btw ...
+ if (pt != null) {
+ lock (primitiveNullableTypes) {
+ TypeData tt = (TypeData)
primitiveNullableTypes [pt.XmlType];
+ if (tt == null) {
+ tt = new TypeData
(type, pt.XmlType, true);
+ primitiveNullableTypes
[pt.XmlType] = tt;
+ }
+ return tt;
+ }
+ }
}
#endif
@@ -172,12 +199,12 @@
}
lock (nameCache) {
- TypeData typeData = nameCache[type] as TypeData;
+ TypeData typeData = nameCache[runtimeType] as
TypeData;
if (typeData != null) return typeData;
#if TARGET_JVM
Hashtable dynamicCache = AppDomainCache;
- typeData = dynamicCache[type] as TypeData;
+ typeData = dynamicCache[runtimeType] as
TypeData;
if (typeData != null) return typeData;
#endif
@@ -190,10 +217,12 @@
name = XmlConvert.EncodeLocalName
(type.Name);
typeData = new TypeData (type, name, false);
+ if (isNullableRuntimeType)
+ typeData.IsGenericNullable = true;
#if TARGET_JVM
- dynamicCache[type] = typeData;
+ dynamicCache[runtimeType] = typeData;
#else
- nameCache[type] = typeData;
+ nameCache[runtimeType] = typeData;
#endif
return typeData;
}
Modified:
trunk/mcs/class/System.XML/System.Xml.Serialization/XmlReflectionImporter.cs
===================================================================
---
trunk/mcs/class/System.XML/System.Xml.Serialization/XmlReflectionImporter.cs
2006-11-22 11:27:33 UTC (rev 68340)
+++
trunk/mcs/class/System.XML/System.Xml.Serialization/XmlReflectionImporter.cs
2006-11-22 11:37:28 UTC (rev 68341)
@@ -954,8 +954,8 @@
if (elem.Form != XmlSchemaForm.Unqualified)
elem.Namespace = (att.Namespace !=
null) ? att.Namespace : defaultNamespace;
elem.IsNullable = att.IsNullable;
-
- if (elem.IsNullable &&
elem.TypeData.IsValueType)
+
+ if (elem.IsNullable &&
!elem.TypeData.IsNullable)
throw new InvalidOperationException
("IsNullable may not be 'true' for value type " + elem.TypeData.FullTypeName +
" in member '" + defaultName + "'");
if (elem.TypeData.IsComplexType)
Modified: trunk/mcs/class/System.XML/Test/System.Xml.Serialization/ChangeLog
===================================================================
--- trunk/mcs/class/System.XML/Test/System.Xml.Serialization/ChangeLog
2006-11-22 11:27:33 UTC (rev 68340)
+++ trunk/mcs/class/System.XML/Test/System.Xml.Serialization/ChangeLog
2006-11-22 11:37:28 UTC (rev 68341)
@@ -1,3 +1,8 @@
+2006-11-22 Atsushi Enomoto <[EMAIL PROTECTED]>
+
+ * XmlReflectionImporterTests.cs : added test for class that contains
+ nullable value type field whose XmlElementAttribute IsNullable.
+
2006-11-20 Atsushi Enomoto <[EMAIL PROTECTED]>
* XmlSerializerTests.cs : added test for nullable type.
Modified:
trunk/mcs/class/System.XML/Test/System.Xml.Serialization/XmlReflectionImporterTests.cs
===================================================================
---
trunk/mcs/class/System.XML/Test/System.Xml.Serialization/XmlReflectionImporterTests.cs
2006-11-22 11:27:33 UTC (rev 68340)
+++
trunk/mcs/class/System.XML/Test/System.Xml.Serialization/XmlReflectionImporterTests.cs
2006-11-22 11:37:28 UTC (rev 68341)
@@ -1535,6 +1535,14 @@
new XmlSerializer (typeof(WrongChoices));
}
+#if NET_2_0
+ [Test]
+ public void ImportNullableContainer ()
+ {
+ new XmlSerializer (typeof (NullableContainer));
+ }
+#endif
+
public class Employee : IXmlSerializable
{
private string _firstName;
@@ -2000,6 +2008,14 @@
private IEnumerator _baseEnumerator;
}
+
+#if NET_2_0
+ public class NullableContainer
+ {
+ [XmlElement (IsNullable = true)]
+ public int? NilInt;
+ }
+#endif
}
}
_______________________________________________
Mono-patches maillist - [email protected]
http://lists.ximian.com/mailman/listinfo/mono-patches