0

Serializable の属性を持つオブジェクトがあります。このクラスは、インターフェイスから継承する Serializable も継承する他のクラスから継承する抽象クラスから継承します

利用した

 string included = JsonConvert.SerializeObject(msg,
                              Formatting.Indented,
                              new JsonSerializerSettings { /*ContractResolver = new NotificationPropertyResolver()*/ TypeNameHandling  = TypeNameHandling.All});

msg は SignalR でこのオブジェクトを送信したいインターフェイスであるため、メンバーを無視していないことがわかります。インターフェイスとクラスを装飾しました

その解決策はありますか?私は自分の属性でリゾルバーも使用しようとしましたが、それでも同じ結果です

クラスは大きすぎますが...

 [Serializable]
[WebAPINotification(Type = typeof(CSensor), Group = "Sensor")]
public class SensorsStateModeNotification : SensorNotification, IBPMPackagedNotification

public abstract class SensorNotification : BasicLanNotification, ISensNotification

[Serializable]
public class BasicLanNotification : BasicNotification, ILanNotification

[Serializable]
public abstract class BasicNotification : INotification, ISerializable, IOpSerializable

[JsonIgnore]
    public long SentAt 
    {
        get
        {
            return m_sentAt;
        }
        set
        {
            m_sentAt = value;
        }
    }

    /// <summary>
    /// 
    /// </summary>
    [JsonIgnore]
    public ENotificationGateway NotificationGateway
    {
        get
        {
            return m_NotifyGateway;
        }
        set
        {
            m_NotifyGateway = value;
        }
    }
4

2 に答える 2

0
public class NotificationPropertyResolver : DefaultContractResolver
    {
        public NotificationPropertyResolver()
        {
            IgnoreSerializableAttribute = true;
            IgnoreSerializableInterface = true;
        }

    }
于 2013-08-09T03:55:17.180 に答える
0

あなたの型はISerializableインターフェースを実装しています - それは属性よりも優先されます。クラスのメンバーをシリアライズしたくない場合は、単にISerializable.GetObjectData実装でそれらを返さないでください。例として、以下のSSCCEを参照してください(ちなみに、将来、より良い回答が必要な場合は、それも提供する必要があります)。

public class StackOverflow_18127665
{
    public class WebAPINotificationAttribute : Attribute
    {
        public Type Type { get; set; }
        public string Group { get; set; }
    }
    public class CSensor { }
    public interface INotification { }
    public interface IOpSerializable { }
    public interface IBPMPackagedNotification { }
    public interface ILanNotification { }
    public interface ISensNotification { }
    [Serializable]
    [WebAPINotification(Type = typeof(CSensor), Group = "Sensor")]
    public class SensorsStateModeNotification : SensorNotification, IBPMPackagedNotification { }

    public abstract class SensorNotification : BasicLanNotification, ISensNotification { }

    [Serializable]
    public class BasicLanNotification : BasicNotification, ILanNotification { }

    [Serializable]
    public abstract class BasicNotification : INotification, ISerializable, IOpSerializable
    {
        long m_sentAt;
        ENotificationGateway m_NotifyGateway;
        [JsonIgnore]
        public long SentAt
        {
            get
            {
                return m_sentAt;
            }
            set
            {
                m_sentAt = value;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        [JsonIgnore]
        public ENotificationGateway NotificationGateway
        {
            get
            {
                return m_NotifyGateway;
            }
            set
            {
                m_NotifyGateway = value;
            }
        }

        #region ISerializable Members

        public void GetObjectData(SerializationInfo info, StreamingContext context)
        {
            // Comment the lines below not to have this serialized
            info.AddValue("NotificationGateway", this.NotificationGateway);
            info.AddValue("SentAt", this.SentAt);
        }

        #endregion
    }
    public enum ENotificationGateway { First, Second }
    public static void Test()
    {
        BasicNotification msg = new BasicLanNotification
        {
            SentAt = 123,
            NotificationGateway = ENotificationGateway.First
        };
        var str = JsonConvert.SerializeObject(
            msg,
            Newtonsoft.Json.Formatting.Indented,
            new JsonSerializerSettings
            {
                TypeNameHandling = TypeNameHandling.All
            });
        Console.WriteLine(str);
    }
}
于 2013-08-08T19:26:42.787 に答える