如何计算项目中的类数

时间:2014-07-09 09:56:42

标签: c# vb.net class vb6

我们正在处理VB6和.NET项目,需要计算类的数量。

对于vb6,我们总结了标准和类模块的总数,它们给出了项目中的类总数。

对于.NET,我们计算文件后面的代码总数,在独立的.vb或.cs文件中,我们计算内部定义的类数。该总数被视为最终计数。

这是对的吗?或者在.NET中是独立的.vb或.cs文件,应该被认为是1类,而不是计算这个文件中的类?

2 个答案:

答案 0 :(得分:9)

使用Reflection,您可以获取特定命名空间上的类数,这样的帮助我:

int num = (from cal in Assembly.GetExecutingAssembly().GetTypes()
           where cal.Namespace == "ProjNameSpace" && cal.IsClass
           select cal).ToList().Count();

AppDomain.CurrentDomain
         .GetAssemblies()
         .SelectMany(cyp => typ.GetTypes())
         .Where(cal => cal.IsClass && cal.Namespace == "ProjNameSpace")
         .ToList().Count();

答案 1 :(得分:3)

For .NET we calculate the total number of code behind files, and in standalone .vb or .cs files we calculate the number of classes defined inside. This total is considered as final count.

Is this correct? or in .NET is the standalone .vb or .cs file supposed to be considered as 1 class rather than counting the classes inside this file??

不,您无法确定计数文件是否可以显示总类数。

<强>原因
1。 .cs或.vb文件中可以有多个类。尽管在代码文件中只有一个类是一个好习惯,但.Net并不限制您在文件中包含多个类。
2。通过在C#中使用partial关键字并在VB.Net中使用Partial关键字,类可以存在于多个文件中。

因此,总是在.Net中计算类的不同名称。