TypeForwardedTo Attribute: Forward a Type to a Different Assembly

Introduction

In .NET, one often refers to other assemblies that contain specific modules that you can use in your application. Say, you reference a DLL that contains some classes that you will use in your application. Suppose the application is deployed. Now, suppose you want to move one class of the DLL to another assembly. What can you do in this situation with old coding methodoligies? The old methodologies say,

  • Remove the class from the existing DLL.
  • Create another DLL (assembly) using that class.
  • Recompile both the DLLs.
  • From your application, add a reference to the new DLL.
  • Recompile the application.
  • Re-deploy the whole thing.

Wouldn’t it be nice to leave the deployed application untouched, and make whatever changes are needed in the DLLs? Obviously, that would be nicer. That’s where the TypeForwardedTo attribute comes into the scene. By using this, you can move your necessary classes out to a new assembly. Now, when your application looks for the class in the old DLL, the old DLL tells your application (the JIT compiler—to be precise), “Well, the person (class) who lived here has moved to another location; here is the address.” It gives in the address, your application follows the address, there it finds the class, and things go on as is.

So, it’s time to get inside the new idea.

Your First DLL

Create a new Windows Class Library project. Name it ‘Library1’. The only class it has is ‘Class1’. Now, following is the code of ‘Class1’.

using System;
using System.Collections.Generic;
using System.Text;

namespace MyNameSpace
{
   public class MyClass
   {
      public int Calculate(int x, int y)
      {
         return x + y;
      }
   }

   public class MyResidentClassInLib1
   {
      public float CalculateRadius(float Radius)
      {
         return (float)(2 * Radius * 3.141592654);
      }
   }
}

This is your very neat and clean DLL that contains a class inside a namespace, and the class has two methods in it—one calculates the sum of the parameters, and the other calculates the radius using the parameter.

Choose Release Build, compile the code, and you have Library1.dll in the Release folder. (Of course, you can have a Debug Build also.)

Your Application

Start a new project of Windows Application type. Create the project in a different directory. This will be your only application form.

Simple. It just has two buttons on it. Clicking the buttons will refer two classes in a DLL that you have just created. First, refer to the DLL that you just created. From the Reference pane, add a reference to the new DLL. After successfully refering to the DLL, your solution explorer looks like the following:

Now, open Form1 in design mode and double-click the Calculate button. This will open the default click event handler for the Calculate button. Add the following code in the Click event handler.

private void btnCalculate_Click(object sender, EventArgs e)
{
   MyNameSpace.MyClass TestClass = new MyNameSpace.MyClass();
   MessageBox.Show(TestClass.Calculate(10, 20).ToString());
}

What is going on here? You referred to a MyClass type that resides in MyNameSpace and instantiated an object of the type. Then, you just called a method of the class. Please remember, MyNameSpace and MyClass are in the DLL that you just added.

Similarly, in the Click event handler of the other button of the form, you have the following code:

private void btnCalculateRadius_Click(object sender, EventArgs e)
{
   MyNameSpace.MyResidentClassInLib1 TestClass2 =
      new MyNameSpace.MyResidentClassInLib1();
   MessageBox.Show(TestClass2.CalculateRadius(200).ToString());
}

Thus, one button calculates a sum, and the other button calculates a radius using two mathematical methods that are shipped in a DLL.

Now choose Release Build, compile the code, and you have your released executable file MyApplication in the Release folder. Please look inside the Release folder, and you will find Library1.dll there. As you might know, it is a mechanism of .NET to create a copy of any referenced DLL in the same location of the executable.

Run the application and click the Calculate button. The output will be as follows:

Now, click the Calculate Radius button. The output will be as follows:

Now that you have a full-fledged application, say you have deployed it. At a future date, you felt the need to move one type from the DLL to another assembly (DLL, in this case). Here comes what you want to do with the TypeForwardedTo attribute. You will create another assembly (DLL) and move the class to the new assembly. Then, you have to compile the new DLL and add a reference to it from the previous DLL. Then, you will add a TypeForwardedTo attribute in the previous DLL to mean a specified type is forwarded to some other assembly. Then, you have to recompile the old DLL and, as a result, you will have two DLLs in the release folder of the previous DLL. Now, you just have to place both the DLLs in the root of the deployed application (or wherever the old DLL is).

More by Author

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Must Read