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)
- 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.)
- Write one public method (eg: Method1() ) in that class “Class1″.
- Write one piece of code to show the messagebox when this method is invoked.
- Add Window application in Solution Explorer
- Add ClassLibrary1 as a reference to Window application
- Add one button on winform.
- Invoke the method from Class1 in the click event of this button. (eg: ClassLibrary1.Class1.Method1(); )
- Build and Run the application. (Check whether the messagebox is shown or not in button’s click.)
- End the running.
- 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. )
- Add new class library project called “ClassLibrary2″ in Solution Explorer.
- Move Class1 from ClassLibrary1 to ClassLibrary2. (You should note that the name of namespace must not be changed. )
- Add ClassLibrary2 as a reference to ClassLibrary1 project.
- Open the assembly.cs in ClassLibrary1 project (this file is under the properties folder of project file.)
- Add the following code in assembly.cs of ClassLibrary1
- Recompile the ClassLibrary1 and ClassLibrary2. ( it’s not necessary to recompile the window application which is using ClassLibrary1. )
- Copy the ClassLibrary1.dll and ClassLibrary2.dll from bin/debug/ folders.
- Place them into bin/debug folder of window application.
- 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.