Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / C#

Complete Assembly Reference in C#.NET

3.46/5 (15 votes)
2 Jan 2016CPOL3 min read 25.1K   749  
A complete assembly reference tool for .NET which will display details on all methods, properties, enum for all classes in all namespaces

Introduction

When we write code in C#, VB.NET, ASP.NET etc, we do use lot of inbuilt .NET framework classes to accomplish various tasks. When we have to include new classes, most we refer MSDN to find the exact class name and its namespace. Then we get to know about its methods, properties etc. This tool tries to accomplish most of the above said work without internet. 

It takes data from Assembly reference files present in .NET framework folder and loads all data so that we can easily do a wild card search to search across classes, methods, properties, 

Tool snapshot

Image 1

How to use the tool

  1. The path of the assembly files is hardcoded to a 64 bit .NET 4.5 installer path

  2. As needed, it can be updated per our wish. (UNC path also works)

  3. Any non-existing path will be validated and only correct path is allowed

  4. Once done, hit the big "Load data" button which loads all possible data to the tool memory and pops up a message box stating its complete

  5. This is a one time process when tool is opened, and the memory is destroyed once tool is closed

  6. Once loaded. you can see the count of data (classes, enums, structures etc) which are loaded

  7. Then there is a search box which allows you to input the desired text we need to search in loaded data. 

  8. Once the search is hit, it loads every possible values into combo box present in the tool

  9. As you choose the desired data in combo box automatically, all associated methods, properties and enum values are loaded in their respective result boxes

  10. Also all the above are logged in a log file which will be named as AssemblyInformation<date>.log in the desired path defined in App.config file

Snapshot after a search

Image 2

Using the code

The code uses System.Reflection namespace. Class Assembly and all its methods help to achieve this task. Important functions are below. Detailed explanation are given in comments and summary itself

1. Validate if DLL is a valid assembly or not. Not all DLLs are .NET assembly files

C#
/// <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. Load data from the assembly files. Load all data like namespaces, classes, enumerators from each DLL file. Say mscorlib.dll, System.dll etc

C#
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. Once thats done, get methods, properties, enumerator details from Assembly functions like GetMethods(), GetProperties() etc. 

C#
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. Log all data to a log file for our future reference

C#
/// <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);
            }
        }

Points of Interest

Whenever we are writing code, we do need lot of references, which we normally search across MSDN with internet. With this tool, we dont need internet and it gives almost all related .NET assembly information. Also searches are saved for our future use, so that we dont need to search them again. 

The idea came into mind, when I was working on a code, and my internet didnt work. It was very hard to find certain related class information, which is very easy for me to find out now. 

References

Reflection : https://msdn.microsoft.com/en-us/library/ms173183.aspx
System.Reflection Namespace : https://msdn.microsoft.com/en-us/library/system.reflection(v=vs.110).aspx

History

1st revision - 02-Jan-2016. Trying to add more features, will update as I do the changes.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)