Look Ma! No Classes – Creating Generic List(T) collection of Anonymous Types and calling Extension Methods
In one of my previous articles, I explained some Common Generic List(T) Operations using C# 2.0 and VB.NET . However the approach was very specific to a List<T> collection that we pre-created. What if we wanted to create classes on the fly, create a collection of the class and also add some custom generic functionality? In this article, we will explore how to create collection of Anonymous Types and also call Extension Methods.
For those who are not familiar with Anonymous types and Extension Methods, please read the new features of C# 3.0 and in VB.NET. In simple words, Anonymous Types enables creation of unnamed types on the fly without first writing the definition of the type.
Since this article also focuses on creating a List(T) collection of Anonymous Types, I will be creating a Helper class (TypeCreator) that will return me a List(T) of anonymous type using generic type inference.
Note: If you see the possibility of passing in your type across layers or services, give it a structure.
Create a Console Application Project and add a class called ‘TypeCreator’. The code will look similar to as shown below. This code template returns a List(T) collection:
C#
static class TypeCreator
{
public static List<T> TypeGenerator<T>(this T[] t)
{
return new List<T>(t);
}
}
VB.NET
Module TypeCreator
Public Function TypeGenerator(Of T)(ByVal at As T()) As List(Of T)
Return New List(Of T)(at)
End Function
End Module
In order to call this TypeGenerator function, add the following code in the Main() method of your Program.cs/Module1.vb as shown below:
C#
static void Main(string[] args)
{
var Person = TypeCreator.TypeGenerator(new[]{
new {ID=1, FirstName="John", MiddleName="", LastName="Shields", Age=29, Sex='M'},
new {ID=2, FirstName="Mary", MiddleName="Matthew", LastName="Shields", Age=29, Sex='M'},
new {ID=3, FirstName="Amber", MiddleName="Carl", LastName="Shields", Age=29, Sex='M'},
new {ID=4, FirstName="Kathy", MiddleName="", LastName="Shields", Age=29, Sex='M'}
});
}
VB.NET
Sub Main()
Dim Person = TypeCreator.TypeGenerator(New Object() { _
New With {.ID = 1, .FirstName = "John", .MiddleName = "", .LastName = "Shields", .Age = 29, .Sex = "M"c}, _
New With {.ID = 2, .FirstName = "Mary", .MiddleName = "Matthew", .LastName = "Shields", .Age = 29, .Sex = "M"c}, _
New With {.ID = 3, .FirstName = "Amber", .MiddleName = "Carl", .LastName = "Shields", .Age = 29, .Sex = "M"c}, _
New With {.ID = 4, .FirstName = "Kathy", .MiddleName = "", .LastName = "Shields", .Age = 29, .Sex = "M"c}})
End Sub
As evident in the code above, we are creating a List(T) collection of anonymous types by passing anonymous classes to the TypeGenerator() function. How do we test it? We can surely use for each loop and then print the collection members on the console. But what I would like to do, is just call something like MyColl.PrintToConsole() and that’s it. I want to encapsulate the looping mechanism as well as be able to pass in any List(T) collection of anonymous types to be looped. Enter Extension Methods!
Extension Methods are static methods that extend existing classes and can be invoked by using instance method syntax.
We will extend the ‘TypeCreator’ class and add in an Extension Method called ‘PrintToConsole’. The PrintToConsole method uses reflection to extract the properties(name, value) of the List(T) passed to it. The code will look similar to the following:
C#
public static void PrintToConsole<T>(this List<T> items)
{
PropertyInfo[] pi = typeof(T).GetProperties();
// extra loop required to print column names only once
foreach (var p in pi)
{
Console.Write("{0,-12}", p.Name);
}
Console.WriteLine();
foreach (var item in items)
{
foreach (var p in pi)
{
Console.Write("{0,-12}", p.GetValue(item, null));
}
Console.WriteLine();
}
Console.ReadLine();
}
VB.NET
<System.Runtime.CompilerServices.Extension()> _
Public Sub PrintToConsole(Of T)(ByRef items As List(Of T))
Dim pi As PropertyInfo() = GetType(T).GetProperties()
' extra loop required to print column names only once
For Each p In pi
Console.Write("{0,-12}", p.Name)
Next
Console.WriteLine()
For Each item In items
For Each p In pi
Console.Write("{0,-12}", p.GetValue(item, Nothing))
Next
Console.WriteLine()
Next
Console.ReadLine()
End Sub
Now all you have to do in your Main() method is to call this ‘Extension Method’
Person.PrintToConsole();
In order to test if this Extension Method is generic enough, just try creating a few more anonymous type collections and pass it to the ‘PrintToConsole’. That’s it.
The entire source code will look similar to the following:
C#
Program.cs
using System;
using System.Collections.Generic;
using System.Text;
using System.Linq;
namespace CommonGenericOperationsUsingLINQ
{
class Program
{
static void Main(string[] args)
{
var Person = TypeCreator.TypeGenerator(new[]{
new {ID=1, FirstName="John", MiddleName="", LastName="Shields", Age=29, Sex='M'},
new {ID=2, FirstName="Mary", MiddleName="Matthew", LastName="Shields", Age=29, Sex='M'},
new {ID=3, FirstName="Amber", MiddleName="Carl", LastName="Shields", Age=29, Sex='M'},
new {ID=4, FirstName="Kathy", MiddleName="", LastName="Shields", Age=29, Sex='M'}
});
Person.PrintToConsole();
var Products = TypeCreator.TypeGenerator(new[]{
new {PID=1, ProductName="Chai", Quantity=12, Price=18.10},
new {PID=2, ProductName="Coffee",Quantity=23 , Price=28.20},
new {PID=3, ProductName="Chains", Quantity=42, Price=21.60},
new {PID=4, ProductName="Chips", Quantity=21, Price=21.20}
});
Products.PrintToConsole();
}
}
}
TypeCreator.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
namespace CommonGenericOperationsUsingLINQ
{
static class TypeCreator
{
public static List<T> TypeGenerator<T>(this T[] at)
{
return new List<T>(at);
}
public static void PrintToConsole<T>(this List<T> items)
{
PropertyInfo[] pi = typeof(T).GetProperties();
// extra loop required to print column names only once
foreach (var p in pi)
{
Console.Write("{0,-12}", p.Name);
}
Console.WriteLine();
foreach (var item in items)
{
foreach (var p in pi)
{
Console.Write("{0,-12}", p.GetValue(item, null));
}
Console.WriteLine();
}
Console.ReadLine();
}
}
}
VB.NET
Module1.vb
Module Module1
Sub Main()
Dim Person = TypeCreator.TypeGenerator(New Object() { _
New With {.ID = 1, .FirstName = "John", .MiddleName = "", .LastName = "Shields", .Age = 29, .Sex = "M"c}, _
New With {.ID = 2, .FirstName = "Mary", .MiddleName = "Matthew", .LastName = "Shields", .Age = 29, .Sex = "M"c}, _
New With {.ID = 3, .FirstName = "Amber", .MiddleName = "Carl", .LastName = "Shields", .Age = 29, .Sex = "M"c}, _
New With {.ID = 4, .FirstName = "Kathy", .MiddleName = "", .LastName = "Shields", .Age = 29, .Sex = "M"c}})
Person.PrintToConsole()
Dim Products = TypeCreator.TypeGenerator(New Object() { _
New With {.PID = 1, .ProductName = "Chai", .Quantity = 12, .Price = 18.1}, _
New With {.PID = 2, .ProductName = "Coffee", .Quantity = 23, .Price = 28.2}, _
New With {.PID = 3, .ProductName = "Chains", .Quantity = 42, .Price = 21.6}, _
New With {.PID = 4, .ProductName = "Chips", .Quantity = 21, .Price = 21.2}})
Products.PrintToConsole()
End Sub
End Module
TypeCreator.vb
Imports System.Reflection
Module TypeCreator
Public Function TypeGenerator(Of T)(ByRef at As T()) As List(Of T)
Return New List(Of T)(at)
End Function
<System.Runtime.CompilerServices.Extension()> _
Public Sub PrintToConsole(Of T)(ByVal items As List(Of T))
Dim pi As PropertyInfo() = GetType(T).GetProperties()
' extra loop required to print column names only once
For Each p In pi
Console.Write("{0,-12}", p.Name)
Next
Console.WriteLine()
For Each item In items
For Each p In pi
Console.Write("{0,-12}", p.GetValue(item, Nothing))
Next
Console.WriteLine()
Next
Console.ReadLine()
End Sub
End Module
The entire source code in C# and VB.NET can be downloaded from here. I hope this article was useful and I thank you for viewing it.