public static void Register(RuntimeTypeModel typeModel, Assembly assembly) { foreach (var type in GetTypesSafely(assembly)) { if (type.IsClass == false && type.IsValueType == false) { continue; } if (Attribute.GetCustomAttribute(type, typeof(ProtoBuf.ProtoContractAttribute)) == null) { continue; } var loweredTypeName = type.Name.ToLower(); if (loweredTypeName.Contains("surrogatedirectives")) { foreach (var field in type.GetFields()) { var sourceType = FindSurrogateSourceType(field.FieldType); if (sourceType != null) { if (typeModel.CanSerialize(sourceType)) { continue; } try { typeModel.Add(sourceType, false).SetSurrogate(field.FieldType); } catch (InvalidOperationException) { } } } } else if (type.Name.ToLower().Contains("surrogate")) { var sourceType = FindSurrogateSourceType(type); if (sourceType != null) { if (typeModel.CanSerialize(sourceType)) { continue; } try { typeModel.Add(sourceType, false).SetSurrogate(type); } catch (InvalidOperationException) { } } } } }
/// <inheritdoc /> public override bool IsSupportedType(Type type) { if (type == null) { throw new ArgumentNullException(nameof(type)); } if (s_unsupportedTypeInfoSet.Contains(type)) { return(false); } #if NET40 if (type.IsGenericType && type.IsConstructedGenericType() == false) { s_unsupportedTypeInfoSet.TryAdd(type); return(false); } #else if (type.IsGenericType && type.IsConstructedGenericType == false) { s_unsupportedTypeInfoSet.TryAdd(type); return(false); } #endif if (!s_model.CanSerialize(type)) { s_unsupportedTypeInfoSet.TryAdd(type); return(false); } return(true); }
private void EnsureTypeCanSerialize <T>() { if (!_model.CanSerialize(typeof(T))) { ReflectionRuntimeTypeModel.AddType <T>(_model); } }
public static void Main(string[] args) { RuntimeTypeModel model = TypeModel.Create(); // There seems to be a bug in protobuf-net, // if I don't do those deepclones they doesn't get registered... if (model.CanSerialize(typeof(Character))) { Character state = new Character(); model.DeepClone(state); Console.WriteLine("deep cloned Character"); } // There seems to be a bug in protobuf-net, // if I don't do those deepclones they doesn't get registered... if (model.CanSerialize(typeof(HUD))) { HUD state = new HUD(); model.DeepClone(state); Console.WriteLine("deep cloned HUD"); } Console.WriteLine("compiling.."); model.Compile(SerializationConstants.StorageSerializerName, SerializationConstants.StorageDllName); }
public static void Main(string[] args) { RuntimeTypeModel model = TypeModel.Create(); //RuntimeTypeModel.Default; // Add new derived models // Console.WriteLine("1"); // model[typeof(Model)].AddSubType(100, typeof(WorldModel)); Console.WriteLine("1"); model[typeof(NetworkPlayerData)].AddSubType(100, typeof(NetworkSorPlayerData)); // There seems to be a bug in protobuf-net, // if I don't do those deepclones they doesn't get registered... if (model.CanSerialize(typeof(InternalState))) { InternalState state = new InternalState(); model.DeepClone(state); Console.WriteLine("deep cloned InternalState"); } Console.WriteLine("compiling.."); model.Compile(SerializationConstants.StateSerializerName, SerializationConstants.StateDllName); }
public bool CanSerialize(Type type) => _typeModel.CanSerialize(type);
/// <summary> /// Indicates whether a type should be considered as a serializable data type /// </summary> protected internal override bool CanSerialize(Type type) => Has(Options.ContractTypesOnly) ? _model.CanSerializeContractType(type) : _model.CanSerialize(type);
static void AvoidJit(RuntimeTypeModel model) { Assert.False(model.CanSerialize(typeof(System.Windows.Media.Color))); }