5,666,132 members and growing! (17,488 online)
Email Password   helpLost your password?
Platforms, Frameworks & Libraries » .NET Framework » General     Intermediate License: The Code Project Open License (CPOL)

Type Forwarding in .NET

By Bhupindra Singh

Type forwarding is a CLR feature that allows us to move a type from its original assembly to another assembly.
C# (C# 1.0, C# 2.0, C# 3.0, C#), Windows, .NET (.NET, .NET 3.5, .NET 3.0, .NET 1.0, .NET 1.1, .NET 2.0), Dev

Posted: 25 Jun 2008
Updated: 24 Aug 2008
Views: 3,167
Bookmarked: 15 times
Announcements
Loading...



Search    
Advanced Search
Sitemap
15 votes for this Article.
Popularity: 4.86 Rating: 4.13 out of 5
1 vote, 6.7%
1
0 votes, 0.0%
2
2 votes, 13.3%
3
3 votes, 20.0%
4
9 votes, 60.0%
5

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++.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Bhupindra Singh


I started programming for fun when I was in Higher Secondary (1995). It was a table writing program in C..haah!! those were the best days of my life Smile

But now it has brought me to no. of jobs in roles of developer, Lead, Architect, Coach and Consultant....

.Net c# is my passion specially when it comes to build api and frameworks to facilitates development teams....

I always learned things by example, now its a pleasure and responsiblity to give learnings back to next generation of coders.
Location: India India

Other popular .NET Framework articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 5 of 5 (Total in Forum: 5) (Refresh)FirstPrevNext
GeneralGreat Infomembermerlin98112:06 4 Sep '08  
GeneralNice 1memberashu fouzdar1:45 4 Sep '08  
GeneralRe: Nice 1memberQwertie15:42 9 Sep '08  
GeneralRe: Nice 1memberBhupindra Singh21:24 9 Sep '08  
GeneralNice infomemberDinesh Mani18:40 25 Aug '08  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 24 Aug 2008
Editor: Smitha Vijayan
Copyright 2008 by Bhupindra Singh
Everything else Copyright © CodeProject, 1999-2008
Web09 | Advertise on the Code Project