【问题标题】:Obsolete an entire namespace?过时的整个命名空间?
【发布时间】:2012-02-07 18:45:18
【问题描述】:

我能否以某种方式在 .NET Framework(3.5 和 4)中将整个命名空间标记为过时,或者将整个项目标记为过时?

【问题讨论】:

标签: .net attributes namespaces obsolete


【解决方案1】:

这是不可能的。我猜原因是命名空间可以跨越不同的程序集,甚至库的用户也可以使用相同的命名空间,所以它不仅会过时你的代码。命名空间基本上是名称前缀的语法糖。命名空间甚至不能成为属性的目标。

【讨论】:

  • 最终将项目和命名空间重命名为 Foo.Obsolete.Bar。在这种情况下有效,因为它仅在同一解决方案中使用
【解决方案2】:

过时的属性不能应用于命名空间。 ObsoleteAttribute 装饰有 [AttributeUsageAttribute(AttributeTargets.Class|AttributeTargets.Struct|AttributeTargets.Enum|AttributeTargets.Constructor|AttributeTargets.Method|AttributeTargets.Property|AttributeTargets.Field|AttributeTargets.Event|AttributeTargets.Interface|AttributeTargets.Delegate, Inherited = false)] .

【讨论】:

    【解决方案3】:

    您不能将整个命名空间标记为过时,但如果您拥有命名空间中的所有代码,则可以使用继承同时拥有两个命名空间:

    namespace New.Namespace
    {
        /// <summary>
        /// Application contract
        /// </summary>
        public class Application
        {
            /// <summary>
            /// Application Name
            /// </summary>
            public string ApplicationName { get; set; }
            /// <summary>
            /// Application Id
            /// </summary>
            public int ApplicationId { get; set; }
        }
    }
    
    
    namespace old.namespace
    {
        /// <summary>
        /// Application contract
        /// </summary>
        [Obsolete]
        public class Application : New.Namespace.Application
        {       
    
        }
    }
    

    您还必须对旧命名空间中的所有类(枚举、结构等)执行此操作,以提供向后和向前兼容性。

    【讨论】:

      猜你喜欢
      • 2012-07-06
      • 1970-01-01
      • 1970-01-01
      • 2018-03-11
      • 2011-11-06
      • 2010-12-20
      • 1970-01-01
      • 2011-08-28
      • 1970-01-01
      相关资源
      最近更新 更多