One of the ways of learning a technology is to add some fun to the learning. LINQ is fun! In this article, we will use LINQ to print the Longest and Shortest Type Name in .NET 4.0.
Disclaimer: The results I have shown in this article may vary on your machine. Since we are referring to the GetExportedTypes() which returns type visible outside the assembly [public types], you can get different results by adding new references (Right click project > Add Reference ) or by changing the access modifiers of the types.
Let us see the steps to use LINQ and extract information about .NET 4.0 types. This article uses a Console Application targetting .NET Framework 4.0.
Step 1: The first step is to get a list of public types defined in the assembly in the current application domain.
C#
var assemb = from assembly in AppDomain.CurrentDomain.GetAssemblies()
from aType in assembly.GetExportedTypes()
select aType;
VB.NET
Dim assemb = From assembly In AppDomain.CurrentDomain.GetAssemblies() , aType In assembly.GetExportedTypes() _
Select aType
Step 2: We then filter the assembly name, group the records by length and then apply an orderby on the filtered records. We use the GroupBy and then the OrderBy as we can have multiple types with the same length.
C#
var filtered = assemb
.Where(x => x.Assembly.FullName.Contains("Version=4.0.0.0"))
.GroupBy(a => a.Name.Length)
.OrderByDescending(x => x.Key);
VB.NET
Dim filtered = assemb.Where(Function(x) x.Assembly.FullName.Contains("Version=4.0.0.0")).GroupBy(Function(a) a.Name.Length).OrderByDescending(Function(x) x.Key)
Step 3: Since the records are OrderByDescending, the longest type will be listed first and the shortest, the last.
C#
// Find the Longest and Shortest Types
var longestType = filtered.First();
var shortestType = filtered.Last();
VB.NET
Dim longestType = filtered.First()
Dim shortestType = filtered.Last()
Step 4: The final step is to loop through the records and print the TypeName, FullName and the Length.
C#
// Loop and print the Longest Type Names
foreach (var nm in longestType)
{
Console.WriteLine("Longest Type Name: {0} \nFullName: {1} \nTypeLength: {2}",
nm.Name, nm.FullName, nm.Name.Length);
Console.WriteLine("-------------------------------------------");
}
// Loop and print the Shortest Type Names
foreach (var nm in shortestType)
{
Console.WriteLine("Shortest Type Name: {0} \nFullName: {1} \nTypeLength: {2}",
nm.Name, nm.FullName, nm.Name.Length);
Console.WriteLine("-------------------------------------------");
}
VB.NET
Imports Microsoft.VisualBasic
For Each nm In longestType
Console.WriteLine("Longest Type Name: {0} " & vbLf & "FullName: {1} " & vbLf & "TypeLength: {2}", nm.Name, nm.FullName, nm.Name.Length)
Console.WriteLine("-------------------------------------------")
Next nm
For Each nm In shortestType
Console.WriteLine("Shortest Type Name: {0} " & vbLf & "FullName: {1} " & vbLf & "TypeLength: {2}", nm.Name, nm.FullName, nm.Name.Length)
Console.WriteLine("-------------------------------------------")
Next nm
OUTPUT
Here’s the entire source code:
C#
static void Main(string[] args)
{
var assemb = from assembly in AppDomain.CurrentDomain.GetAssemblies()
from aType in assembly.GetExportedTypes()
select aType;
var filtered = assemb
.Where(x => x.Assembly.FullName.Contains("Version=4.0.0.0"))
.GroupBy(a => a.Name.Length)
.OrderByDescending(x => x.Key);
// Find the Longest and Shortest Types
var longestType = filtered.First();
var shortestType = filtered.Last();
// Loop and print the Longest Type Names
foreach (var nm in longestType)
{
Console.WriteLine("Longest Type Name: {0} \nFullName: {1} \nTypeLength: {2}",
nm.Name, nm.FullName, nm.Name.Length);
Console.WriteLine("-------------------------------------------");
}
// Loop and print the Shortest Type Names
foreach (var nm in shortestType)
{
Console.WriteLine("Shortest Type Name: {0} \nFullName: {1} \nTypeLength: {2}",
nm.Name, nm.FullName, nm.Name.Length);
Console.WriteLine("-------------------------------------------");
}
Console.ReadLine();
}
VB.NET
Sub Main()
Dim assemb = From assembly In AppDomain.CurrentDomain.GetAssemblies(), aType In assembly.GetExportedTypes()
Select aType
Dim filtered = assemb.Where(Function(x) x.Assembly.FullName.Contains("Version=4.0.0.0")).GroupBy(Function(a) a.Name.Length).OrderByDescending(Function(x) x.Key)
' Find the Longest and Shortest Types
Dim longestType = filtered.First()
Dim shortestType = filtered.Last()
' Loop and print the Longest Type Names
For Each nm In longestType
Console.WriteLine("Longest Type Name: {0} " & vbLf & "FullName: {1} " & vbLf & "TypeLength: {2}", nm.Name, nm.FullName, nm.Name.Length)
Console.WriteLine("-------------------------------------------")
Next nm
' Loop and print the Shortest Type Names
For Each nm In shortestType
Console.WriteLine("Shortest Type Name: {0} " & vbLf & "FullName: {1} " & vbLf & "TypeLength: {2}", nm.Name, nm.FullName, nm.Name.Length)
Console.WriteLine("-------------------------------------------")
Next nm
Console.ReadLine()
End Sub
I hope you liked this article and I thank you for viewing it. The entire source code of this article can be downloaded over here
Give me a +1 if you think it was a good article. Thanks!