PC Review


Reply
Thread Tools Rate Thread

Based on calling time Type, have to return the list of the same type

 
 
velsankar
Guest
Posts: n/a
 
      17th May 2011
Hi All,

The objective is simple. Based on calling time Type, have to return
the list of the same type

I am having the following classes. In static List<T> getList<T>(),
just I am trying to return the list of classes whatever I am
determining at the calling time, Say Product. The problem is I am not
able to add Product to List<T>. If try to return List<Product>,
obviously, we cannot due to return type mismatch. How can I add
Product (or any other class) to the List<T>?

The code is as below

namespace LinqApplication1
{

public class Customer
{
public int Id { get; set; }
public string Name { get; set; }
public string PhoneNo { get; set; }
public string City { get; set; }

public override string ToString()
{
string _toString="Customer Id: {0} Name: {1} Phone
No: {2} City: {3}";
return string.Format(_toString, Id, Name, PhoneNo, City);
}
}

class Order
{
public int Id { get; set; }
public Customer Customer { get; set; }
public Product Product { get; set; }
public double Quantity { get; set; }
}

class Product
{
public int Id { get; set; }
public string Name { get; set; }
public double Price { get; set; }
}

class Program
{
static void Main(string[] args)
{
List<Product> ProductList= getList<Product>();
Console.ReadLine();
}

static List<T> getList<T>()
{
Console.WriteLine(typeof(T).Name);
List<T> list = new List<T>();
var type = typeof(T);
switch (type.Name)
{
case "Product":
{
Product pdt = new Product { Id = 1, Name =
"Soap", Price = 10.0 };
list.Add(pdt);
break;
}
default:
return list;
}
return list;
}
}
}

Suggestion pls....


Thanks,
Velsankar
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
function return type based on input parameter. mathieu Microsoft C# .NET 3 6th Nov 2008 10:44 AM
Is it possible to pass a type as a generic parameter to a function and return an instance of that type? foolmelon@hotmail.com Microsoft C# .NET 2 4th Apr 2007 07:32 PM
Calling an Oracle function with return type PL/SQL RECORD Taavi Kuusik Microsoft ADO .NET 3 9th Jun 2005 06:08 PM
Add more type to Journal type entries under the entry type drop down list? msw Microsoft Outlook Discussion 1 7th Jan 2005 02:09 PM
Is it possible to find the System.Type of the expected function return type in VB.NET? Tony Cox Microsoft VB .NET 2 26th Sep 2003 02:27 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 03:11 AM.