Stupid Question 222: What is type forwarding in C#?
Type forwarding is explained on MSDN as:
‘Type forwarding allows you to move a type to another assembly without having to recompile applications that use the original assembly.’
Fair enough. But what does that really mean? I want a better explanation.
You an app called TypeForwardExample (a console app for example) and it calls the assembly ClasslibraryY and news up a new object of the type TypeX.
At some point you realize (or somebody tells you) that you need to move the TypeX or swap the TypeX to another assembly, but you don’t want to have to recompile the application (the calling assembly). Wouldn’t it be neat if you just could just move the type to another assembly, and then have that type be called automatically? Well that is exactly what type forwarding is. And it sure comes in handy when you need to create one (or several) assemblies that call types and members that might not look the same or be located the same place. You basically redirect.
A simple diagram explaining type forwarding
That is a part of the beauty of Portable Class Libraries made (in part) possible by type forwarding. When you start a new PCL project and you target this and that platform/framework it will look up a matching profile found under
*C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework.NETPortable\v4.0\Profile*
Portable class libraries profiles
The metadata found there will direct you to the actual type implementation, and type forwarding will allow us to swap the types. So if you were to go to:
C:\Windows\Microsoft.NET\Framework64\v4.0.30319
and decompile the System.Runtime DLL (by for example using the free decompiler JustDecompile by Telerik Disclaimer: I work as a technical evangelist for Telerik) you would see type forwards, a lot of them.
This is just type forwarding, there are still other problems Portable Class Libraries need to solve such as assembly unification (calling the right assembly), but that is another blog post (basically referenced assemblies are retargatable and also can be ‘swaped’).
Tomorrow I’ll walk you through a working example (that you can download) :)
To read more about TypeForwarding:
TypeForwardedToAttribute Class
Type Forwarding in the Common Language Runtime
Comments
Hi Don, thank you for the comment. The diagram is shouldn't be wrong (I had a few extra pair of eyes double checking it), I move the Type used to assemblyX and add a reference. You need to recompile the X and Y assemblies, but not the application. That is the beauty of type forwarding, and the whole point of its existence If you take a look at the guide in the next blog post (you can download the example and try out for yourself, just don't recompile the application but just run the executable in the bin - debug folder for the console app). The blog post is here: http://www.irisclasson.com/2013/07/25/stupid-question-223-how-do-you-do-type-forwarding-in-c/ Thank you again for the reply, I would hate to have something inaccurate on the blog and comments are very important. In this case though I can't find see how the diagram is wrong, maybe I didn't explain to well? Let me know :)
Last modified on 2013-07-24