2013-10-31 2 views
13

Newtonsoft.JsonのShouldSerializeメソッドを使用して、プロパティに新しい値を割り当てずにtypeオブジェクトのプロパティを無効にしようとしました。しかし、私はので、私はこれを解決するために助けてください...ここでObject型のプロパティに対してShouldSerialize [MemberName]()メソッドを使用する方法?

サンプルコード

public class Sample1 
{ 
    public String name{get;set;} 
    public int Id{get;set;}; 
} 

であり、これはそのプロパティの1

として、上記のクラスを含む、私のクラスで、それを実装する方法を知りません
public class Container 
{ 
    public String Cname{get;set;} 
    public Sample1 Sample{get;set;}; 

    public bool ShouldSerializeSample() 
    { 
     //What should I write here to prevent the Sample property from being serialized when its properties are assigned no new values. 

    } 
} 
+0

あなたが「割り当てられていない新しい価値」とはどういう意味ですか?あなたは例を挙げることができますか? –

+0

これは、Object型のプロパティがデフォルト値を持つ独自のプロパティを持つことを意味します。 – Madhu

+0

答えは 'Sample1'の例に依存します。たとえば、 'Sample1'が参照型で、デフォルト値が' null'の場合、 'Sample'プロパティが' null'でなければ 'true'を返し、そうでなければfalseを返します。 'Sample1'が独自のフィールドを持つオブジェクトである場合、' Sample1'のフィールドのどれかがデフォルト以外の値であれば 'true'を返し、そうでなければ' false'を返します。 – William

答えて

16

あなたの例のクラスを考えると、私はあなたがこのような何かを探していると思う:

public bool ShouldSerializeSample() 
{ 
    return (Sample != null && (Sample.Id != 0 || Sample.name != null)); 
} 

ここで働いてデモです:ここでは

class Program 
{ 
    static void Main(string[] args) 
    { 
     List<Container> list = new List<Container> 
     { 
      new Container 
      { 
       Cname = "Will serialize Sample because it has a name", 
       Sample = new Sample1 { name = "sample 1" } 
      }, 
      new Container 
      { 
       Cname = "Will serialize Sample because it has a non-zero Id", 
       Sample = new Sample1 { Id = 2 } 
      }, 
      new Container 
      { 
       Cname = "Will serialize Sample because it has a name and an Id", 
       Sample = new Sample1 { name = "sample 3", Id = 3 } 
      }, 
      new Container 
      { 
       Cname = "Will not serialize Sample because it has default values", 
       Sample = new Sample1() 
      }, 
      new Container 
      { 
       Cname = "Will not serialize Sample because it is null", 
       Sample = null 
      } 
     }; 

     string json = JsonConvert.SerializeObject(list, Formatting.Indented); 
     Console.WriteLine(json); 
    } 
} 

public class Sample1 
{ 
    public String name { get; set; } 
    public int Id { get; set; } 
} 

public class Container 
{ 
    public String Cname { get; set; } 
    public Sample1 Sample { get; set; } 

    public bool ShouldSerializeSample() 
    { 
     return (Sample != null && (Sample.Id != 0 || Sample.name != null)); 
    } 
} 

が出力されます。

[ 
    { 
    "Cname": "Will serialize Sample because it has a name", 
    "Sample": { 
     "name": "sample 1", 
     "Id": 0 
    } 
    }, 
    { 
    "Cname": "Will serialize Sample because it has a non-zero Id", 
    "Sample": { 
     "name": null, 
     "Id": 2 
    } 
    }, 
    { 
    "Cname": "Will serialize Sample because it has a name and an Id", 
    "Sample": { 
     "name": "sample 3", 
     "Id": 3 
    } 
    }, 
    { 
    "Cname": "Will not serialize Sample because it has default values" 
    }, 
    { 
    "Cname": "Will not serialize Sample because it is null" 
    } 
] 
+0

@Brainこんにちは**ありがとうございました。しかし、私は 'ShouldSerialize'メソッドで' Reflection'を使う必要があります。アイデアがあればそれを共有してください。 – Madhu

+0

@Madhuより具体的にする必要があります。反射で何を達成しようとしていますか? –

関連する問題