public ILBasedExceptionSerializer(ILSerializerGenerator generator, TypeSerializer typeSerializer) { this.generator = generator; this.typeSerializer = typeSerializer; // Exceptions are a special type in .NET because of the way they are handled by the runtime. // Only certain fields can be safely serialized. this.exceptionFieldFilter = field => { // Any field defined below Exception is acceptable. if (field.DeclaringType != ExceptionType) { return(true); } // Certain fields from the Exception base class are acceptable. return(field.FieldType == typeof(string) || field.FieldType == ExceptionType); }; // When serializing the fallback type, only the fields declared on Exception are included. // Other fields are manually serialized. this.fallbackBaseExceptionSerializer = this.generator.GenerateSerializer( typeof(RemoteNonDeserializableException), field => { // Only serialize base-class fields. if (field.DeclaringType != ExceptionType) { return(false); } // Certain fields from the Exception base class are acceptable. return(field.FieldType == typeof(string) || field.FieldType == ExceptionType); }, fieldComparer: ExceptionFieldInfoComparer.Instance); // Ensure that the fallback serializer only ever has its base exception fields serialized. this.serializers[typeof(RemoteNonDeserializableException)] = this.fallbackBaseExceptionSerializer; }
/// <summary> /// Informs the serialization manager whether this serializer supports the type for serialization. /// </summary> /// <param name="t">The type of the item to be serialized</param> /// <returns>A value indicating whether the item can be serialized.</returns> public bool IsSupportedType(Type t) => this.serializers.ContainsKey(t) || ILSerializerGenerator.IsSupportedType(t.GetTypeInfo());