// Tricky method to return anonymous List
public static CreateList<T>(T ob)
{
var list = new List();
return list;
}
// Create anonymous object, and let's generate our list
var person = new { Name = "Hello", Age = 10 };
var list = CreateList(person);
list.Add(person);
list.Add(new { Name="John", Age = 47 });
It is useful in some cases, but be careful that anonymous objects you created are read-only objects, we can't modify their data after we created them.
Pages
Profile
|
|
Hi, I'm Veerapat Sriarunrungrueang, an expert in technology field, especially full stack web development and performance testing.This is my coding diary. I usually develop and keep code snippets or some tricks, and update to this diary when I have time.
Nowadays, I've been giving counsel to many well-known firms in Thailand.
view more... |
Wednesday, May 30, 2012
How to generate List of Anonymous Types in C#
Normally, we can use List<T> when T is a strong type, then we can add Class T to that List. However, we can trick it by creating a method that receive generic type T, and create List of type T as a return.
Labels:
C#,
Programming,
Windows
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment