How to add a Interface type Object to list where Interface type has Generic types

50 views Asked by At

How to add an Interface to list where Interface type has Generic types?

Below are the code snippet for the same

namespace MyNamespace
{
    public interface ITest<TMessageKey, TEventMessage, TProducerSetting, TProducerClient>
    {
        Task ProcesMessage(Func<TMessageKey, TEventMessage, TProducerSetting, IResult<TEventMessage>> message);
    }

    public class Test<TMessageKey, TEventMessage, TProducerSetting, TProducerClient> : ITest<TMessageKey, TEventMessage, TProducerSetting, TProducerClient>
    {
        public async Task ProcesMessage(Func<TMessageKey, TEventMessage, TProducerSetting, IResult<TEventMessage>> message)
        {
            Console.WriteLine("Processed...");
        }
    }


    public interface IResult<TEventMessage>
    {
        TEventMessage ProcessedMessage { get; set; }
        bool Status { get; set; }
    }

    public class Result<TEventMessage> : IResult<TEventMessage>
    {
        public TEventMessage ProcessedMessage { get; set; }
        public bool Status { get; set; }
    }

    public interface IType { }
    public class Message { }
    public class Message1 : Message { }
    public class Message2 : Message { }
    public class Message3 : Message { }
    public class TestProducerSetting { }
    public class SomeClient { }

    public class Execute
    {
        private List<ITest<string, Message, TestProducerSetting, SomeClient>> lst = new();

        public void TestMethod()
        {
            ITest<string, Message1, TestProducerSetting, SomeClient> test1 = new Test<string, Message1, TestProducerSetting, SomeClient>();
            ITest<string, Message2, TestProducerSetting, SomeClient> test2 = new Test<string, Message2, TestProducerSetting, SomeClient>();
            ITest<string, Message3, TestProducerSetting, SomeClient> test3 = new Test<string, Message3, TestProducerSetting, SomeClient>();

            var res1 = test1.ProcesMessage(Method1);
            var res2 = test2.ProcesMessage(Method2);
            var res3 = test3.ProcesMessage(Method3);

            //Compile time error comes here
            lst.Add(test1);
            lst.Add(test2);
            lst.Add(test3);
        }

        private IResult<Message1> Method1(string messageKey, Message1 message, TestProducerSetting setting)
        {
            return new Result<Message1> { ProcessedMessage = message, Status = true };
        }

        private IResult<Message2> Method2(string messageKey, Message2 message, TestProducerSetting setting)
        {
            return new Result<Message2> { ProcessedMessage = message, Status = true };
        }

        private IResult<Message3> Method3(string messageKey, Message3 message, TestProducerSetting setting)
        {
            return new Result<Message3> { ProcessedMessage = message, Status = true };
        }
    }
}

In real time the test1, test2 and test3 objects are created in the loop.
Note that "ProcesMessage" takes the Func input where TEventMessage in input as well as part of the return type as well. I was not able to achieve this using covariance and contravariance.

Please note that, just for the queries I created the object manually. Is it possible to resolve it?

0

There are 0 answers