Departing point is that I have an interface with generics with 2 implementations:
Interface:
interface IFoo<T , U>
{
U Method1(T item);
T Method2(U id);
}
Implementations:
public class Type1 { }
public class Implementation1 : IFoo<Type1, int> {
public int Method1(Type1 item)
{
throw new NotImplementedException();
}
public Type1 Method2(int id)
{
throw new NotImplementedException();
}
//....
public int Method3()
{
int myReturnedInt = 0;
//....
return myReturnedInt;
}
public void Method4()
{
}
}
public class Type2 { }
public class Implementation2 : IFoo<Type2, int> {
public int Method1(Type2 item)
{
throw new NotImplementedException();
}
public Type2 Method2(int id)
{
throw new NotImplementedException();
}
public int Method3()
{
int myReturnedInt = 0;
//....
return myReturnedInt;
}
public void Method4()
{
}
}
With this departing point, supposed that I got my instances of each of the implementation types available, I would like to map each instance in a dictionary, to acces to the respective Method3
and Method4
respectively, so I tried:
Group both of the types and abstract from generics:
public interface IContainerInterface : IFoo<Type1, int>, IFoo<Type2, int>
{
int Method3();
void Method4();
}
Fill the dictionary in:
public enum EnumType
{
type1,
type2
}
Implementation1 type1Instance;
Implementation2 type2Instance;
private Dictionary<EnumType, IContainerInterface> _container;
void Main(string[] args)
{
_container = new Dictionary<EnumType, IContainerInterface>{
{EnumType.type1, type1Instance as IContainerInterface},
{EnumType.type2, type2Instance as IContainerInterface}
};
}
But does not work. Although Implementation1Instance
is available, the value in the dictionary is null.
A working example of this with no generics would be the answer to this question, where the binding from interface to implementation is achieved when there are no generics involved, but how to achieve this with generics in the interface?
My objective is to be able to access to the methods of the types respectively like this:
//for type1
int myInt = _container[EnumType.type1].Method3();
_container[EnumType.type1].Method4();
//for type2
int myInt = _container[EnumType.type2].Method3();
_container[EnumType.type2].Method4();
Edit:
Corrected all the typos in the read through and find the full compiling snippet (not to be executed, but to be able to understand and follow along what the problem Im trying to explain):
using System;
using System.Collections.Generic;
namespace ConsoleApp10 {
public interface IFoo<T, U>
{
U Method1(T item);
T Method2(U id);
}
public class Type1 { }
public class Implementation1 : IFoo<Type1, int>
{
public int Method1(Type1 item)
{
throw new NotImplementedException();
}
public Type1 Method2(int id)
{
throw new NotImplementedException();
}
//....
public int Method3()
{
int myReturnedInt = 0;
//....
return myReturnedInt;
}
public void Method4()
{
}
}
public class Type2 { }
public class Implementation2 : IFoo<Type2, int>
{
public int Method1(Type2 item)
{
throw new NotImplementedException();
}
public Type2 Method2(int id)
{
throw new NotImplementedException();
}
public int Method3()
{
int myReturnedInt = 0;
//....
return myReturnedInt;
}
public void Method4()
{
}
}
class Program
{
public interface IContainerInterface : IFoo<Type1, int>, IFoo<Type2, int>
{
int Method3();
void Method4();
}
public enum EnumType
{
type1,
type2
}
Implementation1 type1Instance;
Implementation2 type2Instance;
private Dictionary<EnumType, IContainerInterface> _container;
void Main(string[] args)
{
_container = new Dictionary<EnumType, IContainerInterface>{
{EnumType.type1, type1Instance as IContainerInterface},
{EnumType.type2, type2Instance as IContainerInterface}
};
}
}
}