Why can DateTime.MinValue not be serialized in timezones ahead of UTC?

后端 未结 7 1018
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-24 02:09

I am experiencing issues with a WCF REST service. The wire object that I try to return has certain properties not set, resulting in DateTime.MinValue for properties of type

7条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-24 02:26

    using this constructor:

    public DataContractJsonSerializer(Type type, IEnumerable knownTypes, int maxItemsInObjectGraph, bool ignoreExtensionDataObject, IDataContractSurrogate dataContractSurrogate, bool alwaysEmitTypeInformation)
    

    example code:

    DataContractJsonSerializer serializer = new DataContractJsonSerializer(o.GetType(), null, int.MaxValue, false, new DateTimeSurrogate(), false);
    
     public class DateTimeSurrogate : IDataContractSurrogate
        {
    
            #region IDataContractSurrogate 成员
    
            public object GetCustomDataToExport(Type clrType, Type dataContractType)
            {
                return null;
            }
    
            public object GetCustomDataToExport(System.Reflection.MemberInfo memberInfo, Type dataContractType)
            {
                return null;
            }
    
            public Type GetDataContractType(Type type)
            {
                return type;
            }
    
            public object GetDeserializedObject(object obj, Type targetType)
            {
                       return obj;
            }
    
            public void GetKnownCustomDataTypes(System.Collections.ObjectModel.Collection customDataTypes)
            {
    
            }
    
            public object GetObjectToSerialize(object obj, Type targetType)
            {
                if (obj.GetType() == typeof(DateTime))
                {
                    DateTime dt = (DateTime)obj;
                    if (dt == DateTime.MinValue)
                    {
                        dt = DateTime.MinValue.ToUniversalTime();
                        return dt;
                    }
                    return dt;
                }
                if (obj == null)
                {
                    return null;
                }
                var q = from p in obj.GetType().GetProperties()
                        where (p.PropertyType == typeof(DateTime)) && (DateTime)p.GetValue(obj, null) == DateTime.MinValue
                        select p;
                q.ToList().ForEach(p =>
                {
                    p.SetValue(obj, DateTime.MinValue.ToUniversalTime(), null);
                });
                return obj;
            }
    
            public Type GetReferencedTypeOnImport(string typeName, string typeNamespace, object customData)
            {
                return null;
            }
    
            public System.CodeDom.CodeTypeDeclaration ProcessImportedType(System.CodeDom.CodeTypeDeclaration typeDeclaration, System.CodeDom.CodeCompileUnit compileUnit)
            {
                return typeDeclaration;
            }
    
            #endregion
        }
    

提交回复
热议问题