I am extending my C# J2K library to dynamically register codecs from other assemblies. That part is working, but I am having trouble understanding IsAssignableFrom(TypeInfo) and how I can deduce which class instance to handle a given type (SKBitmap, Image, etc).
So far this is what I have come up with. I have an interface named IImageCreator and all codecs are instantiated into
static readonly List<IImageCreator> _creators
I then have an abstract base class that implements IImageCreator like so:
abstract class ImageCreator<TBase> : IImageCreator
The codecs are then implemented into sealed classes like so:
sealed class SKBitmapImageCreator : ImageCreator<SKBitmap> {...}
sealed class WindowsBitmapImageCreator : ImageCreator<Image> {...}
etc.
I have a static method for encoding an image:
static BlkImgDataSrc ToPortableImageSource(object imageObject)
{
_creators.Single(c => c.GetType().GetTypeInfo().IsAssignableFrom(imageObject.GetType().GetTypeInfo()));
This is where I am getting hung up as it throws an exception Sequence contains no matching element
Note that imageObject will be of a handled type such as SKBitmap
I was under the impression that this would deduce the proper instance to handle the imageObject based on the documentation:
Returns true if any of the following conditions is true:
(...)
c is a generic type parameter, and the current instance represents one of the constraints of c.
Where have I gone astray? How can I deduce the proper instance to handle the object?