On Jul 30, 1:36*am, ADN <vcuan...@gmail.comwrote:
Hi, I have a method which calls my service factory:
class person
{
* * *public int ID { get; set: }
* * *public string Name {get; set;}
}
IList<Personmypeople = __serviceFactory.Fetch(new Person())
Here is my generic method:
public List<TFetch<T>(T fetchobject)
{
* * *Type __type = fetchobject.GetType();
* * *// return a generic list of all of the people
* * *IList<__typemyPeopleList = new IList<__type>();
* * *myPeopleList = DataLayer.GetMyPeople(); // will return a
IList<Person>
* * *return myPeopleList
}
I am getting an error when I try to cast the IList<typebecause I
don't think you can do that. Is there a way I can pass in a generic
object and create a generic collection from a generic object?
You cannot pass Type values as generic parameters. But in your case, I
do not see why you'd even need to. First of all, would you be okay
with this?
public List<TFetch<T>(T fetchobject)
{
// return a generic list of all of the people
IList<TmyPeopleList = new IList<T>();
...
}
This doesn't use the actual run-time type of the passed object, but
rather the type of expression that was passed as an argument.
The second problem is that your generic method is not really generic -
here's why:
myPeopleList = DataLayer.GetMyPeople(); // will return a
IList<Person>
Well, if it can only handle lists of Person, then why are you trying
to make it generic in the first place?