/// <summary> /// Finds the appriopriate decoder within specified category /// </summary> /// <param name="o">Object to find the decoder for</param> /// <param name="category">Category of decoders to consider</param> /// <returns>An encoder method appropriate for that object or null if not found</returns> protected virtual BinaryDecoderData GetSpecificDecoder(object o, ConverterCategory category, ConverterCategory[] disallowed = null) { BinaryDecoderMethod decoder; Func <IEnumerable <byte>, object> decoderMethod; if (disallowed == null) { disallowed = new ConverterCategory[] { } } ; switch (category) { case ConverterCategory.ANY: return(GetDecoder(o)); case ConverterCategory.CUSTOM: decoder = GetCustomDecoder(o.GetType()); return((decoder == null && !disallowed.Contains(ConverterCategory.CUSTOM)) ? null : new BinaryDecoderData(decoder.Decoder)); case ConverterCategory.CUSTOM_PREDEFINED: decoder = GetPredefinedCustomDecoder(o.GetType()); return((decoder == null && !disallowed.Contains(ConverterCategory.CUSTOM_PREDEFINED)) ? null : new BinaryDecoderData(decoder.Decoder)); case ConverterCategory.CUSTOM_FOR_CLASS: decoderMethod = GetCustomClassEncoder(o); return((decoderMethod == null && !disallowed.Contains(ConverterCategory.CUSTOM_FOR_CLASS)) ? null : new BinaryDecoderData(decoderMethod)); case ConverterCategory.GLOBAL_FOR_CLASS: decoderMethod = GetGlobalClassDecoder(o.GetType()); return((decoderMethod == null && !disallowed.Contains(ConverterCategory.GLOBAL_FOR_CLASS)) ? null : new BinaryDecoderData(decoderMethod)); case ConverterCategory.DEFAULT: decoder = GetDefaultDecoder(o.GetType()); return((decoder == null && !disallowed.Contains(ConverterCategory.DEFAULT)) ? null : new BinaryDecoderData(decoder.Decoder)); case ConverterCategory.DEFAULT_PREDEFINED: decoder = GetPredefinedDefaultDecoder(o.GetType()); return((decoder == null && !disallowed.Contains(ConverterCategory.DEFAULT_PREDEFINED)) ? null : new BinaryDecoderData(decoder.Decoder)); case ConverterCategory.PRIMITIVE: if (o.GetType().IsPrimitive&& !disallowed.Contains(ConverterCategory.PRIMITIVE)) { return(new BinaryDecoderData(PrimitiveTypeDecoder)); } return(null); case ConverterCategory.SERIALIZABLE: if (o.GetType().IsSerializable&& !disallowed.Contains(ConverterCategory.SERIALIZABLE)) { return(new BinaryDecoderData(SerializableTypeDecoder)); } return(null); case ConverterCategory.COMPLEX: if (!o.GetType().IsPrimitive&& !disallowed.Contains(ConverterCategory.COMPLEX)) { return(new BinaryDecoderData(ComplexTypeDecoder)); } return(null); default: throw new NotImplementedException("Provided category was not implemented"); } }
/// <summary> /// Finds the appropriate decoder for provided object /// </summary> /// <param name="o">Object to find the decoder for</param> /// <returns>An decoder method appropriate for that object</returns> protected virtual BinaryDecoderData GetDecoder(object o, ConverterCategory[] disallowed = null) { Type objectType = o.GetType(); BinaryDecoderMethod decoder; Func <IEnumerable <byte>, object> decoderMethod; if (disallowed == null) { disallowed = new ConverterCategory[] { } } ; // Step 1. Looking for a custom converter added manually to the converters list decoder = GetCustomDecoder(objectType); if (decoder != null && !disallowed.Contains(ConverterCategory.CUSTOM)) { // Found the appropriate decoder return(new BinaryDecoderData(decoder.Decoder)); } // Step 2. Looking for a custom converter defined within the converter class decoder = GetPredefinedCustomDecoder(objectType); if (decoder != null && !disallowed.Contains(ConverterCategory.CUSTOM_PREDEFINED)) { // Found the appropriate decoder return(new BinaryDecoderData(decoder.Decoder)); } // Step 3. Looking for a custom converter defined within the converted class decoderMethod = GetCustomClassDecoder(o); if (decoderMethod != null && !disallowed.Contains(ConverterCategory.CUSTOM_FOR_CLASS)) { return(new BinaryDecoderData(decoderMethod)); } // Step 4. Looking for a global converter defined for the class decoderMethod = GetGlobalClassDecoder(objectType); if (decoderMethod != null && !disallowed.Contains(ConverterCategory.GLOBAL_FOR_CLASS)) { return(new BinaryDecoderData(decoderMethod)); } // Step 5. Looking for a default converter added manually to the converters list decoder = GetDefaultDecoder(objectType); if (decoder != null && !disallowed.Contains(ConverterCategory.DEFAULT)) { return(new BinaryDecoderData(decoder.Decoder)); } // Step 6. Looking for a default converter defined within the converter class decoder = GetPredefinedDefaultDecoder(objectType); if (decoder != null && !disallowed.Contains(ConverterCategory.DEFAULT_PREDEFINED)) { return(new BinaryDecoderData(decoder.Decoder)); } // Step 7. Attempting a converter for a primitive type if (objectType.IsPrimitive && !disallowed.Contains(ConverterCategory.PRIMITIVE)) { return(new BinaryDecoderData(PrimitiveTypeDecoder, ConverterType.INCLUSIVE_PRIMITIVE)); } // Step 8. Attempting a converter for a serializable type if (objectType.IsSerializable && !disallowed.Contains(ConverterCategory.SERIALIZABLE)) { return(new BinaryDecoderData(SerializableTypeDecoder, ConverterType.INCLUSIVE_SERIALIZABLE)); } // Step 9. None of the above is valid, apply the default decoder if (!disallowed.Contains(ConverterCategory.COMPLEX)) { return(new BinaryDecoderData(ComplexTypeDecoder)); } return(null); }