How can I safely download code from several sources dealing with an unknown type?

I need to be able to load two classes at runtime that have an unknown type, for example:

interface IProducer<out A>
{
    public A CookOne();
}

interface IConsumer<in A>
{
    public void EatOne(A food);
}

<edit>

Here are two example classes.

class IntChef : IProducer<int>
{
    private int _counter = 0;

    public int CookOne()
    {
        return counter++;
    }
}

class IntCustomer : IConsumer<int>
{
    public void EatOne(int food)
    {
        Console.WriteLine("I ate a {0}", food);
    }
}

The goal is to load the implementation of each interface, share the type they exchange with, and create a new object that does not care about how the two loaded types exchange.

</ edit>

The entire list of possible types cannot be known at compile time because they do not yet exist. Software using these types looks something like this:

interface IStrategyFactory<A>
{
    public IStrategy MakeAStrategy(IProducer<A> producer, IConsumer<A> consumer);
}

interface IStrategy
{
    public void DoSomething();;
}

( ) factory ( - ) , , , IProducer IConsumer?

, , - http://codebetter.com/blogs/glenn.block/archive/2009/08/20/open-generic-support-in-mef.aspx

:

interface IStrategyFactory<A>
{
    public IStrategy MakeAStrategy(IProducer<A> producer, IConsumer<A> consumer);
}

interface IStrategy
{
    public void DoSomething();;
}
+3
1

, . factory, , . , IOC. , Windsor-Castle, . , , .

, CookOne EatOne, IProducer IConsumer.

interface ISomethingOrOther{}

interface IProducer<out A> where A : ISomethingOrOther
{
    public A CookOne();
}

interface IConsumer<in A> where A : ISomethingOrOther
{
    public void EatOne(A food);
}
0

Source: https://habr.com/ru/post/1758583/


All Articles