Jul 242013
 
 July 24, 2013  Posted by at 2:50 pm C# & F#, Not So Stupid Questions  Add comments

[To celebrate my first year of programming I will ask a 'stupid' questions daily on my blog for a year, to make sure I learn at least 365 new things during my second year as a developer]

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

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

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.

typreforwarding

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

2 comments on “Stupid Question 222: What is type forwarding in C#?

  1. This is a nice introductory article about type forwarding. One small remark. Your diagram is not correct. Once you move TypeX from AssemblyY to AssemblyX you must add reference to AssemblyX and you have to recompile your Application assembly as well. That’s the drawback of type forwarding. In practice this is a severe limitation and type forwarding is rarely used.

  2. Iris Classon on said:

    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 :)

Leave a Reply

Your email address will not be published. Required fields are marked *

*

HTML tags are not allowed.

What is 3 + 13 ?
Please leave these two fields as-is:
IMPORTANT! To be able to proceed, you need to solve the following simple math (so we know that you are a human) :-)