I have the following classes:
public interface IClass
{
// empty interface
}
Two classes implements the interface as a signature only
public abstract class TypeA : IClass
{
...
}
public abstract class TypeB : IClass
{
...
}
Then each of them has some concrete classes:
public class TypeA_1 : TypeA
{
...
}
public class TypeA_2 : TypeA
{
...
}
public class TypeB_1 : TypeB
{
...
}
public class TypeB_2 : TypeB
{
...
}
Then I want a model to have a list of object that under IClass
, but that object will depend on the implementation of two methods.
When doing ReadMethod, I want the Model to have a list of TypeA (either TypeA_1 or TypeA_2) which is dynamically determined at run-time.
When doing WriteMethod, I want to Model to be a list of TypeB (either TypeB_1 or TypeB2) -- this can be done by using generic type e.g. public class Model<T> where T : IClass
public class Model
{
IList<IClass> AListOfObject { get; set; }
}
Methods:
public Model ReadMethod(int a)
{
List<IClass> list = null;
// prepare Model with list of object (different kind based on argument "a")
if (a == 1)
{
list = new List<TypeA_1>{ ... }; ----- seems to be not working as instantiating different type?
}
if (a == 2)
{
list = new List<TypeA_2>{ ... };
}
return new Model { AListOfObject = list };
}
public void WriteMethod(Model model)
{
...
}
Actually how can I define the Model
that could fulfill this two methods at the same time? If I use generic type, it will fulfill the WriteMethod's need, but it won't work on the ReadMethod as it is dynamic.
Am I conceptually wrong to expect one single Model
? Is it must to be 2 separate Model
? But all of them are under IClass
?
Also I'm just a bit confused on the reason why List<IClass> list = new List<TypeA_1>()
won't work.