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
|