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:
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:
Last modified on 2013-07-24