Hi All,
I have a set of classes (MyClass1, MyClass2, MyClass3
) that implement an interface (IMyClass
).
I am interested in creating a method to fill and return a list (Collection.Generics.Lists<>
) of objects that have common interface MyClass
X
(List<IMyClass>
). But how?
Each class has a constructor MyClass
X
which does something different. So I can not create a BaseClass and use a common constructor for all.
By interface I can not do:
public List<IMyClass> Fill(string[] ItemsPassedByParam, IMyClass InstanceOfMyClassX)
List MyList = new List<IMyClass>();
foreach (item in ItemsPassedByParam)
{
MyList.Add (new IMyClass (item)); //can't do new from an Interface!!
}
}
Also I tried using generics type, TMyClass in the next example, but neither has worked for me (though I have not very deep notions about generic types):
public List<TMyClass> Fill(string[] ItemsPassedByParam, Type TypeOfMyClassX)
List MyList = new List <TMyClass> ();
foreach (item in ItemsPassedByParam)
{
MyList.Add (new TMyClass (item)); //can't do new from a Generic Type!!
}
}
I tried also to externalize the code inside the Constructor of MyClass
X
in a method, but I can't call the method since de object is not initialized (there is no new ()
).
In these cases which is the solution?
Thanks in advance!