What is the total number of classes in .NET? The number that is in the downloaded runtime for .NET 2.0, .NET 3.0 and .NET 3.5 SP1.
We are writing a scientific paper about an application that is based on .NET and currently state that there are more than 6000 classes. But I am not sure if this is the correct number.
For instance this page states the number of assemblies, namespaces, methods, etc., but not the number of classes.
Test platform: Windows XP 64 bit SP2, 8 GB RAM.
Update 4: Our paper has been published! I used 9911 for the number of classes (see update 3 below). The journal is Journal of Proteome Research and the title is: "MSQuant, an Open Source Platform for Mass Spectrometry-Based Quantitative Proteomics". Unfortunately the full text of the paper is not freely available, only the abstract.
Update 3: I think I have come very close to a solution now: 9911 public classes for .NET 3.5 SP1. Extending on update 1, I have made the function recursive and extended it so the number of types, classes and public classes are reported for any sub-folder and its subfolders. Running this on C:\WINDOWS\Microsoft.NET gives 40414 types, only 0.2 % from the number in the referenced article. Full transcript - HTML source is tab separated so it can be imported into a spreadsheet, e.g. OpenOffice Calc. Here is a break-down for public classes:
Framework:
Total: 6025
v1.1.4322
0
v2.0.50727
5265
v3.0
641
v3.5
119
Framework64:
Total: 3886
v2.0.50727
3126
v3.0
641
v3.5
119
Update 2: I tried using NDepend and CQL suggested by lextm and it gave a 10% higher number for .NET 2.0 (89 DLLs in C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727): 5855 classes. This was on a different system than for the programmatic solution (see below).
Procedure:
Download NDepend (NDepend_2_12_1_3122.zip), via http://www.ndepend.com/NDependDownload.aspx
Unzip with 7-Zip
Run VisualNDepend.exe
Menu File/Select .NET Assemblies to Analyze/ <Selected the 89 DLL files in C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727>/ <Selected all>/ OK.
Press "Create Query" (lower right) and type/paste:
SELECT TYPES FROM ASSEMBLIES "Accessibility", "cscompmgd", "CustomMarshalers", "IEExecRemote", "IEHost", "IIEHost", "ISymWrapper", "Microsoft.Build.Engine", "Microsoft.Build.Framework", "Microsoft.Build.Tasks", "Microsoft.Build.Utilities", "Microsoft.JScript", "Microsoft.VisualBasic", "Microsoft.VisualBasic.Compatibility", "Microsoft.VisualBasic.Compatibility.Data", "Microsoft.VisualBasic.Vsa", "Microsoft.VisualC", "Microsoft.Vsa", "Microsoft.Vsa.Vb.CodeDOMProcessor", "Microsoft_VsaVb", "mscorlib", "sysglobl", "System", "System.configuration", "System.Configuration.Install", "System.Data", "System.Data.OracleClient", "System.Data.SqlXml", "System.Deployment", "System.Design", "System.DirectoryServices", "System.DirectoryServices.Protocols", "System.Drawing", "System.Drawing.Design", "System.EnterpriseServices", "System.Management", "System.Messaging", "System.Runtime.Remoting", "System.Runtime.Serialization.Formatters.Soap", "System.Security", "System.ServiceProcess", "System.Transactions", "System.Web", "System.Web.Mobile", "System.Web.RegularExpressions", "System.Web.Services", "System.Windows.Forms", "System.XML" WHERE IsPublic AND IsClass
Update 1: based on Jon Skeet's answer I have developed a function (listed below). The preliminary result is 5265 public classes, 12626 classes in total, 18317 types for .NET 2.0. 802 public classes from mscorlib.dll and 678 public classes from System.dll. This is from 89 DLL files of which 40 fails with Assembly.LoadFrom(). But I am not sure I measure the right thing or in the right place.
Call:
DotNetClassCount("C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727")
Function:
Imports System.Reflection 'For Assembly
Imports System.IO 'For Path
Private Function DotNetClassCount(ByRef aBaseDirectory As String) _
As Integer
Dim classCount As Integer = 0
Dim failCount As Integer = 0 'For statistics only.
Dim folderItems As String() = Directory.GetFiles(aBaseDirectory)
Dim someFolderItem As String
For Each someFolderItem In folderItems
Dim fileName As String = Path.GetFileName(someFolderItem)
If Path.GetExtension(fileName) = ".dll" Then
Try
Dim asm3 As Assembly = _
Assembly.LoadFrom(someFolderItem)
Dim types As System.Type() = asm3.GetTypes()
Dim DLLclassCount As Integer = 0
Dim someType As System.Type
For Each someType In types
If someType.IsClass AndAlso someType.IsPublic Then
DLLclassCount += 1
End If
Next
classCount += DLLclassCount
Catch ex As Exception
'Fail silently...
failCount += 1
End Try
End If
Next
Return classCount
End Function 'DotNetClassCount()