0

私はいくつかのクラスを持っています

public class JsonWorldRanking
{
    public int no { get; set; }
    public string deviceid { get; set; }
    public string name { get; set; }
    public int clicks { get; set; }
    public int country { get; set; }
}

public class JsonNationalRanking
{
    public int no { get; set; }
    public string deviceid { get; set; }
    public string name { get; set; }
    public int clicks { get; set; }
}

public class JsonCountryRanking
{
    public int no { get; set; }
    public int countryIndexRanking { get; set; }
    public int clicks { get; set; }
}

特定の条件に基づいて、Json デシリアライズ操作用にこれら 3 つのクラスのいずれかを選択したいと考えています。たとえば、スイッチケースを使用する場合、以下の WHICHLASS が正しいクラスを表すようにするにはどうすればよいですか? 何かのようなもの

switch value

{
  case 0:
  WHICHCLASS = JsonWorldRanking;
  break;

  case 1:
  WHICHCLASS = JsonNationalRanking;
  break;

  case 2:
  WHICHCLASS = JsonCountryRanking
  break;
}

...

var deserialized = JsonConvert.DeserializeObject<List<WHICHCLASS>>(r.EventArgs.Result);
4

1 に答える 1

0

Tのジェネリックメソッドを作ったほうがいいと思いますが、これでもうまくいきます

Type destinationType = null;

switch value

{
  case 0:
  destinationType  = typeof(JsonWorldRanking);
  break;

  case 1:
  destinationType  = typeof(JsonNationalRanking);
  break;

  case 2:
  destinationType  = typeof(JsonCountryRanking);
  break;
}

...

Type listType = typeof(List<>);
Type[] typeArgs = {destinationType};
type requiredGenericListType = listType.MakeGenericType(typeArgs);

var deserialized = JsonConvert.Deserialize(r.EventArgs.Result, requiredGenericListType );
于 2013-01-09T13:16:01.580 に答える