473,385 Members | 2,028 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,385 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 1554
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: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.