Fun with LINQ - Find the Longest and Shortest Type Name in .NET 3.5 using LINQ :While browsing through the forums a couple of days ago, I came across this query where the user wanted to list down the types in .NET 3.5 in the order of the length of their names. He also wanted to find out the total number of types in .NET 3.5 and 2.0. Here’s my attempt of how to do so using LINQ. If you know of a better way or find any discrepancy in the results displayed, please do let me know.
I have created a Console Application in Visual Studio 2008. Let’s take the problem one at a time.
Before I begin, here’s a disclaimer. The results may vary on your machine. Since we are referring to the GetExportedTypes() which returns type visible outside the assembly, you can get different results by adding new references (Right click project > Add Reference ) or by changing the access modifiers of the types. Having said that, let’s get started:
Listing down the public types in .NET 3.5:We can use reflection and a simple LINQ query to get the Types in .NET 3.5 as shown below:
C#
static void Main(string[] args)
{
var assemb = from assembly in AppDomain.CurrentDomain.GetAssemblies()
from aType in assembly.GetExportedTypes()
select aType;
var aList = assemb
.Where(x => x.Assembly.FullName.Contains("Version=3.5.0.0"))
foreach (var a in aList)
{
Console.WriteLine("{0}", a.Name);
}
Console.ReadLine();
}
VB.NET
Sub Main()
Dim assemb = _
From assembly In AppDomain.CurrentDomain.GetAssemblies(),aType In assembly.GetExportedTypes() _
Select aType
Dim aList = assemb.Where(Function(x) x.Assembly.FullName.Contains("Version=3.5.0.0"))
For Each a In aList
Console.WriteLine("{0}", a.Name)
Next
Console.ReadLine()
End Sub
Note: If you want to Fully qualify the type name, use v.FullName instead of v.Name
Listing down the types in the order of the length of their names:An OrderBy Descending would do the trick here as shown below:
C#
static void Main(string[] args)
{
var assemb = from assembly in AppDomain.CurrentDomain.GetAssemblies()
from aType in assembly.GetExportedTypes()
select aType;
var aList = assemb
.Where(x => x.Assembly.FullName.Contains("Version=3.5.0.0"))
.OrderByDescending(x => x.Name.Length);
foreach (var a in aList)
{
Console.WriteLine("Length: {0} {1}", a.Name.Length, a.Name);
}
Console.ReadLine();
}
VB.NET
Sub Main(ByVal args() As String)
Dim assemb = _
From assembly In AppDomain.CurrentDomain.GetAssemblies(),aType In assembly.GetExportedTypes() _
Select aType
Dim aList = assemb.Where(Function(x) x.Assembly.FullName.Contains("Version=3.5.0.0")).OrderByDescending(Function(x) x.Name.Length)
For Each a In aList
Console.WriteLine("Length: {0} {1}", a.Name.Length, a.Name)
Next a
Console.ReadLine()
End Sub
Total types in .NET 2.0 and .NET 3.5:In order to find the total types in .NET 2.0 and .NET 3.5, we will use the GroupBy clause as shown here:
C#
static void Main(string[] args)
{
var assemb = from assembly in AppDomain.CurrentDomain.GetAssemblies()
from aType in assembly.GetExportedTypes()
select aType;
var vers = assemb.Select(
m => m.Assembly.FullName.Split(",".ToCharArray())[1])
.GroupBy(x => x)
.Select(x => new { VerName = x.Key, Count = x.Count() });
foreach (var ver in vers)
{
Console.WriteLine(".NET {0} has {1} types\n",ver.VerName, ver.Count);
}
Console.ReadLine();
}
VB.NET
Sub Main(ByVal args() As String)
Dim assemb = _
From assembly In AppDomain.CurrentDomain.GetAssemblies(), aType In assembly.GetExportedTypes() _
Select aType
Dim vers = assemb.Select(Function(m) m.Assembly.FullName.Split(",".ToCharArray())(1)) _
.GroupBy(Function(x) x) _
.Select(Function(x) New With {Key .VerName = x.Key, Key .Count = x.Count()})
For Each ver In vers
Console.WriteLine(".NET {0} has {1} types" & Constants.vbLf, ver.VerName, ver.Count)
Next ver
Console.ReadLine()
End Sub
http://www.dotnetcurry.com/ShowArticle.aspx?ID=272
Post a Comment