当前位置: 首页>>代码示例>>C#>>正文


C# ObsoleteAttribute.IsError属性代码示例

本文整理汇总了C#中System.ObsoleteAttribute.IsError属性的典型用法代码示例。如果您正苦于以下问题:C# ObsoleteAttribute.IsError属性的具体用法?C# ObsoleteAttribute.IsError怎么用?C# ObsoleteAttribute.IsError使用的例子?那么恭喜您, 这里精选的属性代码示例或许可以为您提供帮助。您也可以进一步了解该属性所在System.ObsoleteAttribute的用法示例。


在下文中一共展示了ObsoleteAttribute.IsError属性的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: OldMethod

//引入命名空间
using System;
using System.Reflection;

public class Example
{
   // Mark OldProperty As Obsolete.
   [ObsoleteAttribute("This property is obsolete. Use NewProperty instead.", false)] 
   public string OldProperty
   { get { return "The old property value."; } }
   
   public string NewProperty
   { get { return "The new property value."; } }

   // Mark OldMethod As Obsolete.
   [ObsoleteAttribute("This method is obsolete. Call NewMethod instead.", true)] 
   public string OldMethod()
   {
      return "You have called OldMethod.";
   }
      
   public string NewMethod() 
   {   
      return "You have called NewMethod.";
   }   

   public static void Main()
   {                 
      // Get all public members of this type.
      MemberInfo[] members = typeof(Example).GetMembers();
      // Count total obsolete members.
      int n = 0;
      
      // Try to get the ObsoleteAttribute for each public member.
      Console.WriteLine("Obsolete members in the Example class:\n");
      foreach (var member in members) {
         ObsoleteAttribute[] attribs = (ObsoleteAttribute[]) 
                                        member.GetCustomAttributes(typeof(ObsoleteAttribute), 
                                                                   false);
         if (attribs.Length > 0) {
            ObsoleteAttribute attrib = attribs[0];
            Console.WriteLine("Member Name: {0}.{1}", member.DeclaringType.FullName, member.Name);
            Console.WriteLine("   Message: {0}", attrib.Message);             
            Console.WriteLine("   Warning/Error: {0}", attrib.IsError ? "Error" : "Warning");      
            n++;
         }
      }
      
      if (n == 0)
         Console.WriteLine("The Example type has no obsolete attributes.");
   } 
}
开发者ID:.NET开发者,项目名称:System,代码行数:52,代码来源:ObsoleteAttribute.IsError

输出:

Obsolete members in the Example class:

Member Name: Example.OldMethod
Message: This method is obsolete. Call NewMethod instead.
Warning/Error: Error
Member Name: Example.OldProperty
Message: This property is obsolete. Use NewProperty instead.
Warning/Error: Warning


注:本文中的System.ObsoleteAttribute.IsError属性示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。