473,795 Members | 3,231 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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<Personmyp eople = __serviceFactor y.Fetch(new Person())

Here is my generic method:

public List<TFetch<T>( T fetchobject)
{
Type __type = fetchobject.Get Type();

// return a generic list of all of the people
IList<__typemyP eopleList = new IList<__type>() ;

myPeopleList = DataLayer.GetMy People(); // will return a
IList<Person>

return myPeopleList
}

I am getting an error when I try to cast the IList<typebecau se 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 1575
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<Personmyp eople = __serviceFactor y.Fetch(new Person())

Here is my generic method:

public List<TFetch<T>( T fetchobject)
{
Type __type = fetchobject.Get Type();

// return a generic list of all of the people
IList<__typemyP eopleList = new IList<__type>() ;
IList<TmyPeople List = DataLayer.GetMy People();
Or...
public List<TFetch<T>( )
{
return (List<T>)DataLa yer.GetMyPeople ();
}
myPeopleList = DataLayer.GetMy People(); // will return a
IList<Person>

return myPeopleList
}

I am getting an error when I try to cast the IList<typebecau se 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<Personmyp eople = __serviceFactor y.Fetch(new Person())

Here is my generic method:

public List<TFetch<T>( T fetchobject)
{
* * *Type __type = fetchobject.Get Type();

* * *// return a generic list of all of the people
* * *IList<__typemy PeopleList = new IList<__type>() ;

* * *myPeopleList = DataLayer.GetMy People(); // will return a
IList<Person>

* * *return myPeopleList

}

I am getting an error when I try to cast the IList<typebecau se 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<TmyPeople List = 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.GetMy People(); // 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
1450
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 eventually replace generic objects with concrete classes, but I'm certainly willing to do some of the work writing it up, etc. If you're interested in helping me, let me know (off-list). Some quotes from this thread: Hung Jung Lu wrote:
15
5367
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 path of the uploaded image, and resize it with the provided dimensions. My function is below. The current function is returning an error when run from the upload function: A generic error occurred in GDI+. Not sure what exactly that means. From what...
8
1833
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; namespace DeterminedGenericCollection { // I got tired of copy and pasting IDisposable
4
3145
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 question In short my question is why when I pass a generic type directly to the formatObject function it works fine, but when I pass it to the checkText function where it is itself a generic argument, and then it is passed to formatObject, it is seen...
3
4314
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 receiver is running within Process1. If I move the receiver into Process2 then its fast. Please can someone explain.
0
3168
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 System.Collections.Generic; using System.Net; using System.Net.Sockets; using System.Runtime.Serialization.Formatters.Binary;
13
3837
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 application that needs implementations in both Java and C#. I have the Java side done, and it works fantastic, and the C# side is nearly there. The problem I'm running into has to do with the differences in implementations of Generics between the two...
10
2715
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 class that contains an object of type T) I have a third generic class: MyHandler<Twhich contains 2 Add methods and a generic method: Add(T obj)
26
3631
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 delegate type expression in just the right place and a well-named method means we can almost read the code out loud and understand it without even thinking. Note that since 'boxing' and 'unboxing' is involved (I think), you don't get what you...
0
9519
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10213
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10000
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9037
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7538
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5436
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5563
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4113
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3721
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.