TimeZoneInfo Property Will not Serialize

TimeZoneInfo Property Will not Serialize

Old forum URL: forums.lhotka.net/forums/t/11961.aspx


thaehn posted on Monday, April 29, 2013

I am getting an error when my business object is being returned from the server to Silverlight:

Type 'System.TimeZoneInfo' with data contract name 'TimeZoneInfo:http://schemas.datacontract.org/2004/07/System' is not expected. Consider using a DataContractResolver or add any types not known statically to the list of known types - for example, by using the KnownTypeAttribute attribute or by adding them to the list of known types passed to DataContractSerializer.

I have a property that is of type TimeZoneInfo which is needed for a Telerik control (it is in the implementation of an interface):

         public static readonly PropertyInfo<TimeZoneInfo> TimeZoneProperty = RegisterProperty<TimeZoneInfo>(c => c.TimeZone);

        public TimeZoneInfo TimeZone

        {

            get { return GetProperty(TimeZoneProperty); }

            set { SetProperty(TimeZoneProperty, value); }

        }

I don't really know how to resolve this issue.  Can someome offer some help? 

Todd

JonnyBee replied on Saturday, May 04, 2013

It is obviously a known problem. See these links:

com/questions/7704891/c-sharp-timezoneinfo-serialization

http://social.msdn.microsoft.com/Forums/en-US/wcf/thread/f164f185-ae18-4775-a2ff-a814813d262d

So my best suggestion would be to make TimeZoneInfo property with a private backing field markes as [NonSerialized] and either

For example:

    public static readonly PropertyInfo<TimeZoneInfo> TimeZoneProperty = 
      RegisterProperty<TimeZoneInfo>(c => c.TimeZone, RelationshipTypes.PrivateField);
    
    [NotUndoableNonSerialized]
    private TimeZoneInfo _timeZone = TimeZoneProperty.DefaultValue;
    public TimeZoneInfo TimeZone
    {
      get { return GetProperty(TimeZoneProperty, _timeZone); }
      set { SetProperty(TimeZoneProperty, ref _timeZone, value); }
    }
 
    protected override void OnSetState(Csla.Serialization.Mobile.SerializationInfo info, StateMode mode)
    {
      base.OnSetState(info, mode);
      _timeZone = TimeZoneInfo.FromSerializedString(
info.GetValue<string>("myTimeZoneInfoProperty")));     }     protected override void OnGetState(Csla.Serialization.Mobile.SerializationInfo info, StateMode mode)     {       base.OnGetState(info, mode);      info.AddValue("myTimeZoneInfoProperty", _timeZone.ToSerializedString());     }

Copyright (c) Marimer LLC