471,313 Members | 1,964 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,313 software developers and data experts.

Sending Generic Object

ADN
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?
Jul 29 '08 #1
2 1476
ADN wrote:
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>();
IList<TmyPeopleList = DataLayer.GetMyPeople();
Or...
public List<TFetch<T>()
{
return (List<T>)DataLayer.GetMyPeople();
}
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?
Jul 29 '08 #2
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?
Jul 30 '08 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by Steven Bethard | last post: by
8 posts views Thread by JAL | last post: by
4 posts views Thread by Charles Churchill | last post: by
13 posts views Thread by rkausch | last post: by
10 posts views Thread by phancey | last post: by
26 posts views Thread by raylopez99 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.