WEBINAR: On-demand webcast
How to Boost Database Development Productivity on Linux, Docker, and Kubernetes with Microsoft SQL Server 2017 REGISTER >
Today, I will speak about getting Assembly information from a file.
What Is an Assembly?
According to MSDN: Assemblies are the building blocks of .NET Framework applications; they form the fundamental unit of deployment, version control, reuse, activation scoping, and security permissions. An assembly is a collection of types and resources that are built to work together and form a logical unit of functionality. An assembly provides the common language runtime with the information it needs to be aware of type implementations. To the runtime, a type does not exist outside the context of an assembly.
Now that you have a basic understanding of assemblies and what information can be stored inside an assembly, you can follow my sample project. Start Visual Studio and create a new Windows application. Add one list box to the form. Resize the form and list box as necessary.
First, add the imports statement to import Assembly reading capabilities:
Declare the following modular variable that will be used by more than one procedure:
Dim path As String = "C:\WINDOWS\Microsoft.NET\Framework\ _ v2.0.50727\System.ServiceProcess.dll"
This variable will hold the path of the file that contains the information we want to read.
Add the following Sub procedure:
Private Sub ShowAssemblyInfo(ByVal a As Assembly) ListBox1.items.add(a.FullName) ListBox1.Items.Add(a.GlobalAssemblyCache) ListBox1.Items.Add(a.Location) ListBox1.Items.Add(a.ImageRuntimeVersion) ' Show Modules For Each m As [Module] In a.GetModules ListBox1.Items.Add(m.Name) Next End Sub
The purpose of this sub procedure is to read the assembly information in the given file. It reads the full name of the assembly present inside the file along with its version information. Then, it determines whether or not this assembly is loaded from the GlobalAssemblyCache. It then reads the file's location and the .NET file runtime version. It adds all this information to the listbox. The loop runs through the assembly and lists all the modules present.
Call this sub procedure from Form_Load:
Private Sub Form1_Load(sender As Object, _ e As EventArgs) Handles Me.Load ' Load a specific Assembly Dim a As Assembly = Assembly.LoadFile(path) ShowAssemblyInfo(a) End Sub
In Form_Load, I just simply called the ShowAssemblyInfo sub; nothing too complicated. When run, it looks like Figure 1:
Figure 1: Basic Assembly Information
Add the following Sub procedure to your code:
Private Sub Flaggs() ' Using BindingFlags to only get declared and ' instance members Dim flags As BindingFlags = _ BindingFlags.DeclaredOnly Or BindingFlags.Public _ Or BindingFlags.Instance ' Load the Assembly from the path Dim theAssembly As Assembly = Assembly.LoadFrom(path) ListBox1.Items.Add(theAssembly.FullName) Dim types() As Type = theAssembly.GetTypes For Each t As Type In types ListBox1.Items.Add(t.Name) Dim members() As MemberInfo = t.GetMembers(flags) For Each member As MemberInfo In members ListBox1.Items.Add(member.MemberType _ & " " & member.Name) Next Next End Sub
This one is a bit trickier... With the Flaggs (stupidly named, but hey, this is only an example) sub procedure, I delved a bit deeper into what type of information I can obtain from an assembly. In this case, I obtained the object data types declared inside the assembly as well as each member (object)'s information. This information includes the name of the objects as well as its type information.
Once run, your form will resemble Figure 2.
Figure 2: Advanced Assembly Information
Obviously, I haven't even scratched the surface on Assemblies and the Reflection Namespace, so here is a bit more studying for you:
I hope you have enjoyed today's article. Until next time, this is me signing off!