什么是使用TypeForwardedToAttribute正确的方法是什么? [英] What is the correct way to use TypeForwardedToAttribute?

查看:334
本文介绍了什么是使用TypeForwardedToAttribute正确的方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这个帖子碰到这个属性和的这个。看来,这是非常有用的,当我们需要升级旧系统。然后,我为了使用这个属性创建一个测试解决方案(含3个项目的话)。首先有一个名为动物。

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";
   }
}

然后,我创建一个控制台应用程序项目,增加了动物为参考,并在方法,我有:

Console.WriteLine(Animal.Dog.Name);

现在它打印旧版本。大!现在,我开始升级现有的项目。我删除类中的动物添加另一个名为AdvancedAnimal类库项目,其中包括:

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";
   }
}

添加AdvancedAnimal,如动物的参考。此外的AssemblyInfo.cs 动物是通过添加修改:

Add "AdvancedAnimal" as a reference in "Animal". Also AssemblyInfo.cs of "Animal" is modified by adding:

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

从这个属性的使用,从现在开始,所有的 Animal.Dog 转发到班AdvancedAnimal(实际上没有类动物更多)。我重新编译的整体解决方案,并希望控制台应用程序打印新版本。但它给了我一个编译错误:

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:

的类型名称'狗'不能在命名空间'动物'被发现。此类型已转发到程序集​​AdvancedAnimal,版本= 1.0.0.0,文化=中性公钥=空'考虑增加一个引用该程序集。

呵呵,我告诉添加AdvancedAnimal作为参考,我的控制台应用程序!但是,如果我这样做,我不需要的属性更多,因为如果我添加当然AdvancedAnimal作为控制台应用程序的参考, Animal.Dog 指一个在AdvancedAnimal!我想到的是,修改动物,添加AdvancedAnimal,没有必要改变所有其它项目/类库,因为集信息已经提供了足够的信息。升级系统时,这真的很方便。否则,我有20个项目指的是动物,我需要添加AdvancedAnimal作为参考,所有的人都。而且更重要的是,我不能找到在此解决方案TypeForwardedToAttribute的任何使用,除去不要紧任何东西。你能告诉我什么地方错了我的测试/想法?

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?

推荐答案

通过MSDN

使用的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 doesnt make any sense.

让我们说清楚。假设,如果你有一类狗在装配oldAssembly.dll

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");
      }
   }
}

和引用它在其他一些组件(x.dll)

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

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

后,你想改变printName功能,但没有触及来电(x.dll)(假定如果DLL部署和不想要被感动)

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)

让你创建一个新的组件(DLL),它使用了

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

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

现在,你现在可以通过添加引用新的DLL并添加编译旧的DLL

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

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

现在无论调用来转发到AnimalNew.Dog的Animal.Dog进行。

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

于是

!我想到的是,修改   动物,添加AdvancedAnimal,不   要改变所有其它   项目/类库,因为   集信息已经提供了足够的   信息。这真的很方便   升级系统时。否则,我   有20个项目指   动物,我需要添加   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.

你不必AdvancedAnimal添加到您的所有20个项目。所有你需要做的就是增加AdvancedAnimal到动物。

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

编辑:

我重新编译整个解决方案,   希望控制台应用程序打印   新版本。但它给了我一个   编译错误:

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

这整个的一点是,我们可以调用一个新的组件,而无需修改调用者。你不应该重新编译的整体解决方案,因为你的来电者仍然指向INT旧组件的方法。这就是为什么你得到了错误

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. thats why you got the error

类型名称'狗'找不到   命名空间中的动物。这种类型的   已转发到组装   AdvancedAnimal

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

只需重新编译老和放大器;新assemblied,放入调用者bin和运行exe。它会像一个魅力

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

这篇关于什么是使用TypeForwardedToAttribute正确的方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆