なので、CultureInfoクラスの仕様上は【Serializable】です。ただし、CultureInfo インスタンスへの参照を持つ別の [Serializable] クラス (ClassA など) があり、ClassA を使用して XmlSerializer インスタンスを作成しようとすると、例外が発生します。誰でも回避策を知っていますか?CultureInfo は [Serializable] であるため、以下が機能するはずです。
どうもありがとう!
-- コード --
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; }
}
}
- 例外 -
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:
--追加されたクラス--(以下の回答から)
[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);
}
}