Dictionary of classes that implement a generic interface

SWeko

I have a generic interface with two type parameters with severe generic constraints, and several implementations for different combinations.

public interface IResolver<TIn, TOut> where ... {...}

I want to create a (static) resolver factory that will store instances of the known implementations, and just serve them, along the lines:

public static ResolverFactory{

   public static IResover<TIn, TOut>  GetResolver<TIn, TOut> where ... ()
   {
       //access some storage dictionary to return the correctly typed instance
   }
}

How can I create such a containter, that will store both IResover<Entity1, Entity2> and IResolver<Entity3, Entity4>?

One option I can think of is to use a separate non-generic "marker" interface like:

public interface IResolver {} 
public interface IResolver<TIn, TOut> : IResolver where .... 
{...}

and use

Dictionary<Type, Dictionary <Type, IResolver>> storage;

public RegisterResolver(IResolver resolver)
{
   //add to storage - how?
}

but this scenario basically invalidates the constraints put on the generic parameters. Also when adding the IResolver, getting the generic types of the IResolver<TIn, TOut> is more or less impossible.

Is there a better solution to this?

Kevin Gosse

There may be something obvious I'm missing in your question, because I don't understand where the issue is.

First, I declare a IResolver<TIn, TOut> interface with a constraint:

public interface IResolver<TIn, TOut>
    where TIn : Stream 
{

}

Then, I create a ResolverFactory, where the constraints are enforced by both the RegisterResolver and GetResolver method. The way the objects are actually stored doesn't matter, because the storage isn't exposed outside of the class. The encapsulation maintains the consistency:

public static class ResolverFactory
{
    private static Dictionary<Type, object> storage = new Dictionary<Type, object>();

    public static void RegisterResolver<TIn, TOut>(IResolver<TIn, TOut> resolver) where TIn : Stream 
    {
        storage[typeof(IResolver<TIn, TOut>)] = resolver;
    }

    public static IResolver<TIn, TOut> GetResolver<TIn, TOut>() where TIn : Stream
    {
        return storage[typeof(IResolver<TIn, TOut>)] as IResolver<TIn, TOut>;
    }
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Dictionary of classes that implement a generic interface

From Dev

C# Factory of classes that implement generic Interface

From Dev

implement generic generic interface in scala

From Dev

Implement Interface Generic Function

From Dev

Implement a generic interface

From Dev

Implement Interface Generic Function

From Dev

How to implement a generic interface with a child generic interface

From Dev

How to implement a generic interface with a child generic interface

From Dev

How to implement generic dictionary class?

From Dev

How to implement an interface with generic types?

From Dev

How implement an interface Generic Method

From Dev

How to implement an interface with generic types?

From Dev

How implement an interface Generic Method

From Dev

Implement a generic C++/CLI interface in a C# generic interface

From Dev

Implement a generic C++/CLI interface in a C# generic interface

From Dev

Restrict a generic Class parameter to classes that implement Map

From Dev

Using Reflection.Emit to implement generic interface

From Dev

Abstract class implement a generic Interface Type

From Dev

Why can't a Java Generic implement an Interface?

From Dev

Implement interface with enum value as generic parameter

From Dev

Typescript Can't implement generic function interface

From Dev

Implement a dictionary of classes for a pseudo class factory

From Dev

Classes that implement an interface have more methods

From Dev

2 classes implement same interface, duplicated code

From Dev

How to obtain a list of classes that implement an interface?

From Dev

How to get all classes that implement same interface?

From Dev

Implement an interface using inheritance (child classes)

From Dev

Same method for multiple classes that implement the same interface

From Dev

Restrict classes that can implement interface in Java

Related Related

  1. 1

    Dictionary of classes that implement a generic interface

  2. 2

    C# Factory of classes that implement generic Interface

  3. 3

    implement generic generic interface in scala

  4. 4

    Implement Interface Generic Function

  5. 5

    Implement a generic interface

  6. 6

    Implement Interface Generic Function

  7. 7

    How to implement a generic interface with a child generic interface

  8. 8

    How to implement a generic interface with a child generic interface

  9. 9

    How to implement generic dictionary class?

  10. 10

    How to implement an interface with generic types?

  11. 11

    How implement an interface Generic Method

  12. 12

    How to implement an interface with generic types?

  13. 13

    How implement an interface Generic Method

  14. 14

    Implement a generic C++/CLI interface in a C# generic interface

  15. 15

    Implement a generic C++/CLI interface in a C# generic interface

  16. 16

    Restrict a generic Class parameter to classes that implement Map

  17. 17

    Using Reflection.Emit to implement generic interface

  18. 18

    Abstract class implement a generic Interface Type

  19. 19

    Why can't a Java Generic implement an Interface?

  20. 20

    Implement interface with enum value as generic parameter

  21. 21

    Typescript Can't implement generic function interface

  22. 22

    Implement a dictionary of classes for a pseudo class factory

  23. 23

    Classes that implement an interface have more methods

  24. 24

    2 classes implement same interface, duplicated code

  25. 25

    How to obtain a list of classes that implement an interface?

  26. 26

    How to get all classes that implement same interface?

  27. 27

    Implement an interface using inheritance (child classes)

  28. 28

    Same method for multiple classes that implement the same interface

  29. 29

    Restrict classes that can implement interface in Java

HotTag

Archive