相关疑难解决方法(0)

使用TypeForwardedToAttribute的正确方法是什么?

我在这篇文章和这篇文章中看到了这个属性.当我们需要升级旧系统时,它似乎非常有用.然后我创建一个测试解决方案(其中包含3个项目)以使用此属性.首先是一个名为"Animal"的类库项目.

namespace Animal
{
   public class Dog
   {
      public static string Name = "old version";
   }
}
Run Code Online (Sandbox Code Playgroud)

然后我创建一个控制台应用程序项目,添加"Animal"作为参考,并在Main我有的方法中:

Console.WriteLine(Animal.Dog.Name);
Run Code Online (Sandbox Code Playgroud)

现在它打印"旧版本".大!现在我开始"升级"现有项目.我删除了Dog"Animal" 中的类,添加了另一个名为"AdvancedAnimal"的类库项目,其中包含:

namespace Animal
{
   public class Dog
   {
      public static string Name = "new version";
   }
}
Run Code Online (Sandbox Code Playgroud)

添加"AdvancedAnimal"作为"Animal"中的引用.还AssemblyInfo.cs"动物"的,通过添加改性:

[assembly: TypeForwardedTo(typeof(Animal.Dog))]
Run Code Online (Sandbox Code Playgroud)

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

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, …

.net c# attributes

12
推荐指数
1
解决办法
4690
查看次数

标签 统计

.net ×1

attributes ×1

c# ×1