Click here to Skip to main content
Email Password   helpLost your password?

Introduction

Type forwarding is a CLR feature that allows us to move a type from its original assembly to another assembly in such a way that there is no need to recompile the applications referring to the original assembly.

The Requirement

Suppose you have created an assembly named MyLibrary which contains a class named MyClass. Let's say, some of your applications refer to the MyLibrary assembly and utilizes MyClass. In a later phase, you decide to move MyClass from MyLibrary to a newly created assembly called MyAdvancedLibrary. If you ship a new version of MyLibrary (which now doesn't have MyClass), along with MyAdvancedLibrary, then your existing applications looking for MyClass in MyLibrary will not find the MyClass and end up with errors.

The Solution

Now, in order to run your applications without recompiling them, you can use the Type Forwarding feature of the Common Language Runtime.

In the above scenario, you need to apply TypeForwardedToAttribute to the new version of your MyLibrary, so that requests for MyClass are now forwarded to the newly created library MyAdvancedLibrary that now contains MyClass.

Steps

  1. Move the MyClass code from MyLibrary to MyAdvancedLibrary.
  2. Put TypeForwardedToAtrribute in MyLibrary for the MyClass type.
  3. // C# Example
    [assembly:TypeForwardedToAttribute(typeof(MyClass))] 
  4. Compile the newly created MyAdvancedLibrary.
  5. Add a reference of MyAdvancedLibrary into MyLibrary.
  6. Recompile MyLibrary (because MyClass used to be located in that).

Now, you can ship the new version of MyLibrary along with MyAdvancedLibrary and run your applications without a recompile!

The Limitation

The .NET Framework version 2.0 does not allow type forwarding from assemblies written in Visual Basic. However, a Visual Basic application can consume forwarded types if it uses the assemblies coded in C# or C++.

You must Sign In to use this message board.
 
 
Per page   
 FirstPrevNext
GeneralType Forwarding in .NET
Aslam Parvez Memon
10:54 25 Dec '09  
Thanks for providing nice info.
GeneralThank you
Ted James
0:42 20 Nov '09  
this is so well explained far better than in the Microsoft Self-Paced training kit!!!
GeneralGreat Info
merlin981
12:06 4 Sep '08  
Very easy to follow instructions in your article. Good job. 5 from me



~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
LINQ Exchange - Learn about LINQ and Lambda Expressions
Web SEO Specialists - The SEO Expert
Joke of the Day and Random Jokes - ReallyFunnyQuickJokes.com
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

GeneralNice 1
ashu fouzdar
1:45 4 Sep '08  
However, Simple query. Is it possible without shipping MyLibrary with MyAdvancedLibrary.

dnpro
"Very bad programmer"

GeneralRe: Nice 1
Qwertie
15:42 9 Sep '08  
I think that's impossible. But I guess you could ship a really tiny version of MyLibrary.dll that contains nothing but forwards.
GeneralRe: Nice 1
Bhupindra Singh
21:24 9 Sep '08  
Yes Its Impossible without shippign MyLibrary.dll, Qwertie's Suggestion for deploying a tiny version is a really good work around.
Thanks!!

Bhupen

GeneralNice info
Dinesh Mani
18:40 25 Aug '08  
Clear and concise! Great job!


Last Updated 25 Aug 2008 | Advertise | Privacy | Terms of Use | Copyright © CodeProject, 1999-2010