473,237 Members | 1,081 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,237 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 1551
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Steven Bethard | last post by:
So I thought I'd try to summarize a few things here and maybe we can move toward filing a PEP. I'm not really sure I'm the right person to champion it because, as I've mentioned, I usually...
15
by: David Lozzi | last post by:
Howdy, I have a function that uploads an image and that works great. I love ..Nets built in upload, so much easier than 3rd party uploaders! Now I am making a public function that will take the...
8
by: JAL | last post by:
Here is my first attempt at a deterministic collection using Generics, apologies for C#. I will try to convert to C++/cli. using System; using System.Collections.Generic; using System.Text; ...
4
by: Charles Churchill | last post by:
I apologize if this question has been asked before, but after about half an hour of searching I haven't been able to find an answer online. My code is beloiw, with comments pertaining to my...
3
by: BuddyWork | last post by:
Hello, Could someone please explain why the Socket.Send is slow to send to the same process it sending from. Eg. Process1 calls Socket.Send which sends to the same IP address and port, the...
0
by: Buddy Home | last post by:
There is two examples of code. Example 1. Send and Receive within the same process. Put this code in a console app called SendAndReceive and run the code. using System; using...
13
by: rkausch | last post by:
Hello everyone, I'm writing because I'm frustrated with the implementation of C#'s generics, and need a workaround. I come from a Java background, and am currently writing a portion of an...
10
by: phancey | last post by:
I'm quite new to generics. I have 2 generic classes: MyClass<Tand MyOtherClass<T>. MyClass<Thas 2 public Add methods Add(MyOtherClass<T>); Add(MyOtherClass<Wrapper<T>>); (Wrapper<Tis another...
26
by: raylopez99 | last post by:
Here is a good example that shows generic delegate types. Read this through and you'll have an excellent understanding of how to use these types. You might say that the combination of the generic...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

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.