Returning different object types via Generics

I wanted to use generics to create a Load method that would return different results based on the Generic Type. This is how I did it;

     
public T Load(string logicalChoice, params object[] arguments)         
{                              
  T o = (T)Activator.CreateInstance(typeof(T)); 		
  if (logicalChoice=="choice1") 		
  {                		
    MyList list = o as MyList;                 	
    list.Add(new Item { Id = 100, Description = "Fruit" });                 	
    list.Add(new Item { Id = 110, Description = "Dairy" });                 
  } 		
  else 		
  { 			
    Item item = o as Item;                 	
    item.Id = 100; 			
    item.Description = "Fruit";  		
  }                 
  return o;                     
}     

Advertisements
This entry was posted in Uncategorized. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s