What is TypeForwardedToAttribute?

Note: The actual class name is TypeForwardedToAttribute. (not TypeForwardedToAttributes )

TypeForwardedToAttribute class is new class in .NET framework 2.0. It helps you to move the existing classes (types) from one assembly to another without breaking to the existing application.

Before this feature is not available in .NET framework, it’s too much work if we wanna move the existing classes to another assembly for any reason because it can’t be done without recompiling the existing applications which are using that assembly. For example, there are one class called Class1 in assembly1. There are one window application called WinApp which is using that assembly “assembly1”. We wanna move that class “Class1” from assembly1 to another assembly called “assembly2”. In order to do that, we have to remove that class from assembly and add it to assembly2. Then, we have to add this new assembly as a reference to WinApp. Then, we have to recompile this application again. It’s gonna be the problem if we don’t have the source code of WinApp. Even we have the source code, it’s like too work to do.

Now, things has changed. TypeForwardedToAttribute has come for this need. I will show you how this class is useful with the following demonstration (or steps)

  1. Create a new class library called “ClassLibrary1” in Visual Studio .NET 2005.(There will be one class called Class1 already included in this class library.)
  2. Write one public method (eg: Method1() ) in that class “Class1”.
  3. Write one piece of code to show the messagebox when this method is invoked.
  4. Add Window application in Solution Explorer
  5. Add ClassLibrary1 as a reference to Window application
  6. Add one button on winform.
  7. Invoke the method from Class1 in the click event of this button. (eg: ClassLibrary1.Class1.Method1(); )
  8. Build and Run the application. (Check whether the messagebox is shown or not in button’s click.)
  9. End the running.
  10. Remove the window application. (we already ensure that this window application is working fine with ClassLibrary1.Class1.Method1(); If you wanna ensure more, you can run and test the executable file of window application under bin/debug folder. )
  11. Add new class library project called “ClassLibrary2” in Solution Explorer.
  12. Move Class1 from ClassLibrary1 to ClassLibrary2. (You should note that the name of namespace must not be changed. )
  13. Add ClassLibrary2 as a reference to ClassLibrary1 project.
  14. Open the assembly.cs in ClassLibrary1 project (this file is under the properties folder of project file.)
  15. Add the following code in assembly.cs of ClassLibrary1
    [assembly: TypeForwardedTo(typeof(ClassLibrary1.Class1))]
  16. Recompile the ClassLibrary1 and ClassLibrary2. ( it’s not necessary to recompile the window application which is using ClassLibrary1. )
  17. Copy the ClassLibrary1.dll and ClassLibrary2.dll from bin/debug/ folders.
  18. Place them into bin/debug folder of window application.
  19. Then, run the exe file (eg: windowapplication.exe) and check whether the messagebox is shown or not. (I’m sure that your window application works just fine and you will see the msgbox. :) )

So, We dont need to touch Window application for adding ClassLibrary2.dll. Window application keep using ClassLibrary1. When window application requests the type to ClassLibrary1 then ClassLibrary1 will forword to ClassLibrary2 by using TypeForwardedToAttribute. So, it’s not necessary to recompile the window application.. It makes the developers’ lift easier. :)

Okay. That’s all from my side. What do you think? If you have any question for this class or my explanation or the steps mentioned in my article, please feel free to let me know.


6 Replies to “What is TypeForwardedToAttribute?”

  1. shouldnt point 15 read:

    Add the following code in assembly.cs of ClassLibrary1
    [assembly: TypeForwardedTo(typeof(ClassLibrary2.Class1))]


  2. Excellent example, however tha name is AssemblyInfo.cs, not Assembly.cs

    any way excellente job,

Leave a Reply