Incognition

Untitled

Jul 28th, 2015
226
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.93 KB | None | 0 0
  1. // Surrogate
  2. [ProtoContract]
  3. public class SurrogateCultureInfo
  4. {
  5.     [ProtoMember(1)]
  6.     public string Culture { get; set; }
  7.  
  8.     public static implicit operator SurrogateCultureInfo( CultureInfo info )
  9.     {
  10.         return
  11.             info != null
  12.             ? new SurrogateCultureInfo { Culture = info.Name }
  13.             : null;
  14.     }
  15.  
  16.     public static implicit operator CultureInfo( SurrogateCultureInfo surrogate )
  17.     {
  18.         CultureInfo info = new CultureInfo(surrogate.Culture);
  19.  
  20.         return info;
  21.     }
  22. }
  23.  
  24. // Setting the surrogate
  25. var model = ProtoBuf.Meta.TypeModel.Create();
  26. AddTypeToModel<CultureInfo>( model ).SetSurrogate( typeof( Surrogates.SurrogateCultureInfo ) );
  27.  
  28. private static MetaType AddTypeToModel<T>( RuntimeTypeModel typeModel )
  29. {
  30.     var properties = typeof(T).GetProperties().Select(p => p.Name).OrderBy(name => name);
  31.  
  32.     return typeModel.Add(typeof(T), true).Add(properties.ToArray());
  33. }
Advertisement
Add Comment
Please, Sign In to add comment