fork download
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace Test
  6. {
  7. class Program
  8. {
  9. class MyClass1 { }
  10. class MyClass2 { }
  11.  
  12. delegate void DeserializeDelegate(byte[] data);
  13.  
  14. static void Deserialize<T>(byte[] data)
  15. {
  16. // デシリアライズ処理
  17. Console.WriteLine($"Deserialize<{typeof(T)}> is called");
  18. }
  19.  
  20. static void Main(string[] args)
  21. {
  22. var data = new byte[] { 1, 2, 3 };
  23.  
  24. var delegates = new Dictionary<int, DeserializeDelegate>
  25. {
  26. [1] = new DeserializeDelegate(Deserialize<MyClass1>),
  27. [2] = new DeserializeDelegate(Deserialize<MyClass2>),
  28. };
  29.  
  30. // data の1バイト目で、
  31. // どのクラスとしてデシリアライズするかを別ける
  32. var deserialize = delegates[data[0]];
  33. // data の1バイト目を飛ばす
  34. data = data.Skip(1).ToArray();
  35.  
  36. deserialize(data);
  37. }
  38. }
  39. }
  40.  
Success #stdin #stdout 0.03s 16020KB
stdin
Standard input is empty
stdout
Deserialize<Test.Program+MyClass1> is called