TypeForwardedToAttribute

Microsoft .NET Framework, ASP.NET, Visual C# (CSharp, C Sharp, C-Sharp) Developer Training, Visual Studio


Jump to: navigation, search
Exam Prep. Guides
Exam 70-536 Study Guide

1. Types and collections

2. Process, threading,…
3. Embedding features
4. Serialization, I/O
5. .NET Security
6. Interop., reflection,…
7. Global., drawing, text

edit

Contents


TypeForwardedTo is an attribute that allows you to move a type declaration to another assembly, without disrupting users of a previous version of the assembly.

It takes one parameter which specifies the destination Type which will be used instead of the type that was in the assembly. The destination type must have the same name and namespace as the type it is replacing.

Example

If you have a class named Dog in an assembly named Animal

namespace Animal {
   public class Dog {
      }
   }

You may at a later date decide that Dog would go better in an assembly called Canine However, if you just yank Dog out of Animal any application looking to find Dog in Animal is going to fail.

You don’t want applications to fail and you don’t want to have to find all the programs that reference Animal.Dog and require them to change their code and recompile. The TypeForwardedToAttribute makes both these scenarios unnecessary.

  1. Move the type declaration to the new assembly
  2. In the old assembly, add a reference to the new assembly
  3. Add the following to the old assembly:
[assembly: TypeForwardedTo( typeof( Animal.Dog ) )]

Usually this assembly attribute would be placed in Assembly.cs.

Note: The namespace must not change. The following would result in an error:

namespace Canine {
   public class Dog {
      }
   }


Note: If you are forwarding a type to an assembly that an application does not already have a reference to, adding a TypeForwardedTo will work as long as the new assembly is present. However, once you reopen the application in visual studio, you will have to add a reference to the new assembly. This becomes obvious once you examine the namespace where TypeForwardedTo lives, System.Runtime.CompilerServices.


Personal tools