What is the correct way to use TypeForwardedToAttribute?

This is the first time I've heard about that attribute, interesting :)

The way I read the documentation, the attribute can be used in scenario's where you're only delivering a set of library dll's.

Let's say you have some application, that uses a set of widgets from a DLL. Later on, you decide to move parts of that DLL to a separate DLL. Using the TypeForwardedToAttribute, you can do that by having the old DLL reference the new one. You can now deploy both DLL's to the installations of the existing application, without recompiling the application itself

This could be useful in scenario's where you don't have control over the application itself, but are merely supplying a set of library dll's. In order for your codebase to move forward, you can use the TypeForwardedToAttribute, and have users deploy it safely to their application, without breaking anything.


From the documentation for TypeForwardedToAttribute:

Use the TypeForwardedToAttribute attribute to move a type from one assembly to another without disrupting callers that compiled against the old assembly.

But what you are doing is forwarding the type from the same assembly to another type in the same assembly. It doesn't make any sense.

Lets make it clear. Assume if you have a class dog in assembly oldAssembly.dll

namespace Animal
{
   public class Dog
   { 
      public void printName() {      
           console.writeline("old version");
      }
   }
}

and referenced it in some other assembly (x.dll)

   Dog dg=new Dog();
   dg.printName()

later you wanted to change the printName functionality, but without touching the caller (x.dll) (assume if the dll is deployed and dont want to be touched)

so you create a new assembly (dll), which got

namespace AdvancedAnimal 
{
   public class Dog
   { 
      public void printName() {      
           console.writeline("new version");
      }
   }
}

Now you can now recompile the old dll by adding reference to the new dll and adding

[assembly:TypeForwardedTo(typeof(AdvancedAnimal.Dog))]

Now whatever calls made to the Animal.Dog forwarded to AdvancedAnimal.Dog.

So

! What I expect is that modifying "Animal", Adding "AdvancedAnimal", no necessary to change all other projects/class libraries because the assembly info already provides enough information. That is really convenient when upgrading a system. Otherwise, I have 20 projects referring to "Animal", I need to add "AdvancedAnimal" as a reference to all of them.

You dont have to add AdvancedAnimal to all your 20 projects. All you have to do is adding AdvancedAnimal to Animal.

Hope this clarifies the context where it can be useful

EDIT:

I re-compile the whole solution and hope the console application prints "new version". But it gives me a compile error:

Whole point of this is we can call a new assembly without modifying the caller. You should not recompile the whole solution, because your caller is still pointing to the method int the old assembly. That's why you got the error

The type name 'Dog' could not be found in the namespace 'Animal'. This type has been forwarded to assembly 'AdvancedAnimal

Just recompile your old & new assemblies and put it into the caller bin and run the exe. It'll work like a charm.