What is the correct way to use TypeForwardedToAttribute?(使用 TypeForwardedToAttribute 的正确方法是什么?)
问题描述
我在 这篇文章 和 这个.当我们需要升级旧系统时,它似乎非常有用.然后我创建了一个测试解决方案(其中包含 3 个项目)以使用此属性.首先有一个名为Animal"的类库项目.
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";
}
}
然后我创建一个控制台应用程序项目,添加Animal"作为参考,在 Main
方法中我有:
Then I create a console application project, add "Animal" as a reference, and in the Main
method I have:
Console.WriteLine(Animal.Dog.Name);
现在它打印旧版本".伟大的!现在我开始升级"现有项目.我在Animal"中删除类 Dog
添加另一个名为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";
}
}
在Animal"中添加AdvancedAnimal"作为参考.此外,Animal"的 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"中的Dog
类中(实际上没有DogAnimal 中的
类).我重新编译了整个解决方案,并希望控制台应用程序打印新版本".但它给了我一个编译错误:
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:
在命名空间Animal"中找不到类型名称Dog".此类型已转发到程序集 'AdvancedAnimal, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' 考虑添加对该程序集的引用.
哦,我被告知要添加AdvancedAnimal"作为对我的控制台应用程序的引用!但是如果我这样做了,我就不再需要该属性了,因为如果我在控制台应用程序中添加AdvancedAnimal"作为引用,当然 Animal.Dog
指的是AdvancedAnimal"中的那个!我期望的是修改Animal",添加AdvancedAnimal",没有必要更改所有其他项目/类库,因为程序集信息已经提供了足够的信息.这在升级系统时非常方便.否则,我有 20 个项目引用动物",我需要添加高级动物"作为对所有项目的引用.更重要的是,我在这个解决方案中找不到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?
推荐答案
来自 TypeForwardedToAttribute
的文档:
From the documentation for TypeForwardedToAttribute
:
使用 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.
让我们说清楚.假设你在程序集 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))]
现在对 Animal.Dog 的任何调用都转发到 AdvancedAnimal.Dog.
Now whatever calls made to the Animal.Dog forwarded to AdvancedAnimal.Dog.
所以
!我期望的是修改动物",添加高级动物",否有必要改变所有其他项目/类库,因为装配信息已经提供了足够的信息.这真的很方便升级系统时.否则,我有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 添加到 Animal.
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:
重点是我们可以在不修改调用者的情况下调用新程序集.您不应该重新编译整个解决方案,因为您的调用者仍然指向旧程序集中的方法.这就是你得到错误的原因
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
只需重新编译旧的 &新程序集并将其放入调用方bin并运行exe.它会像魅力一样发挥作用.
Just recompile your old & new assemblies and put it into the caller bin and run the exe. It'll work like a charm.
这篇关于使用 TypeForwardedToAttribute 的正确方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:使用 TypeForwardedToAttribute 的正确方法是什么?
基础教程推荐
- 将 XML 转换为通用列表 2022-01-01
- SSE 浮点算术是否可重现? 2022-01-01
- 如何在 IDE 中获取 Xamarin Studio C# 输出? 2022-01-01
- 将 Office 安装到 Windows 容器 (servercore:ltsc2019) 失败,错误代码为 17002 2022-01-01
- c# Math.Sqrt 实现 2022-01-01
- 如何激活MC67中的红灯 2022-01-01
- MS Visual Studio .NET 的替代品 2022-01-01
- 有没有办法忽略 2GB 文件上传的 maxRequestLength 限制? 2022-01-01
- 为什么Flurl.Http DownloadFileAsync/Http客户端GetAsync需要 2022-09-30
- rabbitmq 的 REST API 2022-01-01