Creating Dynamic Assemblies and Running Dynamic Code in VB | CodeGuru

Creating Dynamic Assemblies and Running Dynamic Code in VB

Introduction Today, I would like to cover a simple yet advanced topic: How to create dynamic assemblies and how to run dynamic code. First, the basics (as usual)…. Assemblies Assemblies form the fundamental unit of deployment, version control, reuse, activation scoping, and security permissions for a .NET-based application. Assemblies take the form of an executable […]

Written By
Hannes DuPreez
Hannes DuPreez
Nov 23, 2015
2 minute read
CodeGuru content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More

Introduction

Today, I would like to cover a simple yet advanced topic: How to create dynamic assemblies and how to run dynamic code.

First, the basics (as usual)….

Assemblies

Assemblies form the fundamental unit of deployment, version control, reuse, activation scoping, and security permissions for a .NET-based application. Assemblies take the form of an executable (.exe) file or dynamic link library (.dll) file, and are the building blocks of the .NET Framework. They provide the common language runtime with the information it needs to be aware of type implementations. You can think of an assembly as a collection of types and resources that form a logical unit of functionality and are built to work together. For more information about assemblies, read here.

System.Reflection Namespace

The System.Reflection namespace contains types that retrieve information about modules, members, assemblies, parameters, and other entities in managed code by examining their metadata. These types also can be used to manipulate instances of loaded types. Here is more information on the System.Reflection Namespace.

Advertisement

System.Reflection.Emit Namespace

The System.Reflection.Emit namespace contains classes that allow a compiler or tool to emit metadata and Microsoft intermediate language (MSIL) and optionally generate a PE file on disk. The primary clients of these classes are script engines and compilers. Here is more information about the System.Reflection.Emit namespace

AssemblyName Class

Describes an assembly’s unique identity in full. Here is more information about the AssemblyName class.

AppDomain Class

Represents an application domain, which is an isolated environment where applications execute. This class cannot be inherited. Here is more information on the AppDomain class.

AssemblyBuilder Class

Defines and represents a dynamic assembly. Here is more information about the AssemblyBuilder class.

ModuleBuilder Class

Defines and represents a module in a dynamic assembly. Here is more information covering the ModuleBuider class.

TypeBuilder Class

Defines and creates new instances of classes during run time. Here is more information concerning the TypeBuilder class.

Our Project

Create a new Console Application and add the following code to the Module that has been created.

Imports System.Reflection
Imports System.Reflection.Emit

Class Program

   Public Shared Sub Main(ByVal args() As String)

      ' Create an Assembly Name
         Dim strANName As AssemblyName = New AssemblyName
         strANName.Name = "DemoAssembly"
         strANName.Version = New Version("1.0.0.0")

         ' Get the AppDomain to put our assembly in
         Dim adDomain As AppDomain = AppDomain.CurrentDomain

         ' Create the Assembly
         Dim assemBldr As AssemblyBuilder = _
            adDomain.DefineDynamicAssembly(strANName, _
            AssemblyBuilderAccess.ReflectionOnly)

         ' Define a module to hold our type
         Dim mbBuilder As ModuleBuilder = _
            assemBldr.DefineDynamicModule("TestModule", _
            "TestAssembly.dll")

         ' Create a new type
         Dim tbCar As TypeBuilder = _
            mbBuilder.DefineType("Car", TypeAttributes.Public)

         ' Display the new Type
         Dim tCar As Type = tbCar.CreateType
         Console.WriteLine(tCar.FullName)
         For Each m As MemberInfo In tCar.GetMembers
            Console.WriteLine(" Member ({0}): {1}", _
               m.MemberType, m.Name)
         Next

      Console.Read()
   End Sub
End Class

I added the necessary Reflection namespaces to be able to work on the inner-workings of assemblies. Also, I created a dynamic Assembly and set the Assembly’s Name and Version properties. An AppDomain object is created to determine the domain in which the assembly has to be run.

Gradually, with the help of AssemblyBuilder, ModuleBuilder, and TypeBuilder the Assembly gets built. Lastly, the information of the new Assembly gets displayed, as shown in Figure 1.

Advertisement

Dynamic
Figure 1: Assembly

Conclusion

Assemblies are very powerful and useful, as you learned from working through this article. Until next time, cheers!

Hannes DuPreez

Ockert J. du Preez is a passionate coder and always willing to learn. He has written hundreds of developer articles over the years detailing his programming quests and adventures. He has written the following books: Visual Studio 2019 In-Depth (BpB Publications) JavaScript for Gurus (BpB Publications) He was the Technical Editor for Professional C++, 5th Edition (Wiley) He was a Microsoft Most Valuable Professional for .NET (2008–2017).

CodeGuru Logo

CodeGuru covers topics related to Microsoft-related software development, mobile development, database management, and web application programming. In addition to tutorials and how-tos that teach programmers how to code in Microsoft-related languages and frameworks like C# and .Net, we also publish articles on software development tools, the latest in developer news, and advice for project managers. Cloud services such as Microsoft Azure and database options including SQL Server and MSSQL are also frequently covered.

Property of TechnologyAdvice. © 2026 TechnologyAdvice. All Rights Reserved

Advertiser Disclosure: Some of the products that appear on this site are from companies from which TechnologyAdvice receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. TechnologyAdvice does not include all companies or all types of products available in the marketplace.