I came across to this attribute in this post and this one. It seems that it's very useful when we need to upgrade an old system. Then I create a test solution(with 3 projects in it) in order to use this attribute. Firstly there is a class library project named "Animal".
namespace Animal
{
public class Dog
{
public static string Name = "old version";
}
}
Then I create a console application project, add "Animal" as a reference, and in the Main
method I have:
Console.WriteLine(Animal.Dog.Name);
Now it prints "old version". Great! Now I begin to "upgrade" the existing project. I remove the class Dog
in "Animal" add another class library project named "AdvancedAnimal" which contains:
namespace Animal
{
public class Dog
{
public static string Name = "new version";
}
}
Add "AdvancedAnimal" as a reference in "Animal". Also AssemblyInfo.cs
of "Animal" is modified by adding:
[assembly: TypeForwardedTo(typeof(Animal.Dog))]
From the usage of this attribute, from now on all Animal.Dog
is forwarded to the Dog
class in "AdvancedAnimal"(actually there is no Dog
class in Animal any more). I re-compile the whole solution and hope the console application prints "new version". But it gives me a compile error:
The type name 'Dog' could not be found in the namespace 'Animal'. This type has been forwarded to assembly 'AdvancedAnimal, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' Consider adding a reference to that assembly.
Oh, I'm told to add "AdvancedAnimal" as a reference to my console application! But if I do so, I don't need the attribute any more, because if I add "AdvancedAnimal" as a reference in the console application, of course Animal.Dog
refers the one in "AdvancedAnimal"! 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. And more important is, I can't find any usage of "TypeForwardedToAttribute" in this solution, removing it doesn't matter anything. Can you tell me what's wrong with my test/idea?