Note that there are some explanatory texts on larger screens.

plurals
  1. POSerialization of CultureInfo object as property of another class
    primarykey
    data
    text
    <p>So, according to CultureInfo class specifications, it is [Serializable]. However, when I have another [Serializable] class (say ClassA) that has a reference to a CultureInfo instance, and I try to create an XmlSerializer instance with ClassA, I get an exception. Anyone know a work around? I would assume since CultureInfo is [Serializable] the below should work.</p> <p>Many thanks!</p> <p>-- Code--</p> <pre><code>using System; using System.Xml.Serialization; using System.Globalization; namespace CultureInfoSerializationTest { class Program { static void Main(string[] args) { ClassA aClass = new ClassA(); aClass.UsedCulture = CultureInfo.CurrentCulture; try { XmlSerializer serializer = new XmlSerializer(typeof(ClassA)); } catch (Exception e) { } } } [Serializable] public class ClassA { public CultureInfo UsedCulture { get; set; } } } </code></pre> <p>--Exception--</p> <pre><code>System.InvalidOperationException was caught HResult=-2146233079 Message=There was an error reflecting type 'CultureInfoSerializationTest.ClassA'. Source=System.Xml StackTrace: at System.Xml.Serialization.XmlReflectionImporter.ImportTypeMapping(TypeModel model, String ns, ImportContext context, String dataType, XmlAttributes a, Boolean repeats, Boolean openModel, RecursionLimiter limiter) at System.Xml.Serialization.XmlReflectionImporter.ImportElement(TypeModel model, XmlRootAttribute root, String defaultNamespace, RecursionLimiter limiter) at System.Xml.Serialization.XmlReflectionImporter.ImportTypeMapping(Type type, XmlRootAttribute root, String defaultNamespace) at System.Xml.Serialization.XmlSerializer..ctor(Type type, String defaultNamespace) at System.Xml.Serialization.XmlSerializer..ctor(Type type) at CultureInfoSerializationTest.Program.Main(String[] args) in c:\users\asbeug\documents\visual studio 2010\Projects\CultureInfoSerializationTest\CultureInfoSerializationTest\Program.cs:line 18 InnerException: System.InvalidOperationException HResult=-2146233079 Message=Cannot serialize member 'CultureInfoSerializationTest.ClassA.UsedCulture' of type 'System.Globalization.CultureInfo', see inner exception for more details. Source=System.Xml StackTrace: at System.Xml.Serialization.StructModel.CheckSupportedMember(TypeDesc typeDesc, MemberInfo member, Type type) at System.Xml.Serialization.StructModel.GetPropertyModel(PropertyInfo propertyInfo) at System.Xml.Serialization.StructModel.GetFieldModel(MemberInfo memberInfo) at System.Xml.Serialization.XmlReflectionImporter.InitializeStructMembers(StructMapping mapping, StructModel model, Boolean openModel, String typeName, RecursionLimiter limiter) at System.Xml.Serialization.XmlReflectionImporter.ImportStructLikeMapping(StructModel model, String ns, Boolean openModel, XmlAttributes a, RecursionLimiter limiter) at System.Xml.Serialization.XmlReflectionImporter.ImportTypeMapping(TypeModel model, String ns, ImportContext context, String dataType, XmlAttributes a, Boolean repeats, Boolean openModel, RecursionLimiter limiter) InnerException: System.InvalidOperationException HResult=-2146233079 Message=System.Globalization.CultureInfo cannot be serialized because it does not have a parameterless constructor. InnerException: </code></pre> <p>--Appended class-- (from answer below)</p> <pre><code>[Serializable] public class ClassA : ISerializable { public ClassA() { } public CultureInfo UsedCulture { get; set; } public void GetObjectData(SerializationInfo info, StreamingContext context) { if (info == null) { throw new ArgumentNullException("info"); } info.AddValue("Culture", UsedCulture); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload