65.9K
CodeProject 正在变化。 阅读更多。
Home

C#.NET 中的完整程序集参考

starIconstarIconstarIcon
emptyStarIcon
starIcon
emptyStarIcon

3.46/5 (15投票s)

2016 年 1 月 2 日

CPOL

3分钟阅读

viewsIcon

25723

downloadIcon

795

一个完整的 .NET 程序集参考工具,它将显示所有命名空间中所有类的所有方法、属性、枚举的详细信息

引言

当我们在 C#、VB.NET、ASP.NET 等中编写代码时,我们确实使用了大量内置的 .NET Framework 类来完成各种任务。当我们需要包含新类时,大多数时候我们会参考 MSDN 来查找确切的类名及其命名空间。然后我们了解它的方法、属性等。这个工具试图完成上述大部分工作,而无需互联网。

它从 .NET Framework 文件夹中存在的程序集参考文件中获取数据,并加载所有数据,以便我们可以轻松地进行通配符搜索以跨类、方法、属性进行搜索。

工具快照

如何使用该工具

  1. 程序集文件的路径被硬编码到 64 位 .NET 4.5 安装程序路径

  2. 根据需要,可以根据我们的意愿进行更新。(UNC 路径也有效)

  3. 任何不存在的路径都将被验证,并且只允许使用正确的路径

  4. 完成后,点击大的“加载数据”按钮,它会将所有可能的数据加载到工具内存中,并弹出一个消息框说明已完成

  5. 这是工具打开时的一次性过程,一旦工具关闭,内存就会被销毁

  6. 加载后,您可以看到加载的数据(类、枚举、结构等)的计数

  7. 然后有一个搜索框,允许您输入我们需要在加载的数据中搜索的所需文本。

  8. 一旦点击搜索,它会将所有可能的值加载到工具中存在的组合框中

  9. 当您在组合框中自动选择所需数据时,所有关联的方法、属性和枚举值都会加载到它们各自的结果框中

  10. 此外,以上所有内容都记录在一个日志文件中,该日志文件将命名为 AssemblyInformation<date>.log,位于 App.config 文件中定义的所需路径中

搜索后的快照

使用代码

该代码使用 System.Reflection 命名空间。类 Assembly 及其所有方法有助于完成此任务。重要的函数如下。详细的解释在注释和摘要本身中给出

1. 验证 DLL 是否为有效的程序集。并非所有 DLL 都是 .NET 程序集文件

        /// <summary>
        /// This function determines if the DLL is a valid assembly files or not. 
        /// Not all DLLs are valid assemblies. Any exception will result a false
        /// Reference : https://msdn.microsoft.com/en-us/library/ms173100.aspx
        /// </summary>
        /// <param name="strFilePath"></param>
        /// <returns></returns>
        private bool isValidAssembly(string strFilePath)
        {
            try
            {
                Type[] asm = (Assembly.LoadFile(strFilePath)).GetTypes();
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

2. 从程序集文件中加载数据。从每个 DLL 文件加载所有数据,如命名空间、类、枚举器。例如 mscorlib.dll、System.dll 等

if (file.Substring(file.Length - 4).ToLower() == ".dll") // Check only DLL files
                    {
                        if (isValidAssembly(file)) // Validate if its a validAssembly file
                        {
                            Assembly asm = Assembly.LoadFile(file);
                            foreach (Type t in asm.GetTypes())
                            {
                                assemblyList.Add(t.AssemblyQualifiedName); // Load all possible classes, enum, structures to a List<>
                            }
                            assemblyList = assemblyList.Distinct().ToList(); // Remove duplicates from the list
                            assemblyList.Sort(); // Sort in alphabetical order
                        }
                        else
                        {
                            assemblyfailedList.Add(file); // Add failed assembly files. Not used anywhere as of now.
                        }
                    }

3. 完成后,从程序集函数(如 GetMethods()、GetProperties() 等)获取方法、属性、枚举器详细信息。

Type myType1 = Type.GetType(strClassName);

            foreach (MethodInfo methinfo in myType1.GetMethods()) // Get method information from GetMethods()
            {
                if (!methinfo.Name.Contains("_"))
                {
                    methodList.Add(methinfo.Name + "()");
                }
            }

            methodList = methodList.Distinct().ToList();
            methodList.Sort(); // Remove duplicates and sort data.

            foreach (string strMethod in methodList)
            {
                methodname.AppendLine(strMethod); // Append data to StringBuilder
            }

            // Reason to assign values to SB first and then to textBox is due to performance issues

            txtMethods.Text += methodname.ToString(); // Assign StringBuilder data to textbox.  

4. 将所有数据记录到日志文件中,以供将来参考

/// <summary>
        /// Log all the data to a text file. Path and log name defined in App.Config file
        /// Considering Sunday = 0, Monday = 1 ... Saturday = 6
        /// Log file name will be AssemblyInformation0.log, AssemblyInformation1.log ..... AssemblyInformation6.log
        /// </summary>
        /// <param name="strContent"></param>
        private void logData(string strContent)
        {
            // Path and log name defined in App.Config file
            string strLogPath = ConfigurationManager.AppSettings["logpath"].ToString();
            string strLogName = ConfigurationManager.AppSettings["logname"].ToString();

            if (!strLogPath.Substring(strLogPath.Length - 1).Contains(@"\")) // validate is path name ends with '\'
            {
                strLogPath += @"\";
            }
            using (StreamWriter sr = new StreamWriter(strLogPath + strLogName + (int)DateTime.Now.DayOfWeek + ".log", true))
            {
                sr.WriteLine(strContent);
            }
        }

关注点

每当我们编写代码时,我们都需要大量的参考资料,我们通常通过互联网在 MSDN 中搜索。使用此工具,我们不需要互联网,它可以提供几乎所有相关的 .NET 程序集信息。此外,搜索结果会保存下来以供将来使用,因此我们无需再次搜索它们。

当我在编写代码时,我的互联网无法工作时,这个想法就出现在脑海中。很难找到某些相关的类信息,现在对我来说很容易找到。

参考文献

反射:https://msdn.microsoft.com/en-us/library/ms173183.aspx
System.Reflection 命名空间:https://msdn.microsoft.com/en-us/library/system.reflection(v=vs.110).aspx

历史

第一次修订 - 2016 年 1 月 2 日。尝试添加更多功能,会在我进行更改时更新。

© . All rights reserved.