本文整理汇总了C#中System.ObsoleteAttribute.Message属性的典型用法代码示例。如果您正苦于以下问题:C# ObsoleteAttribute.Message属性的具体用法?C# ObsoleteAttribute.Message怎么用?C# ObsoleteAttribute.Message使用的例子?那么, 这里精选的属性代码示例或许可以为您提供帮助。您也可以进一步了解该属性所在类System.ObsoleteAttribute
的用法示例。
在下文中一共展示了ObsoleteAttribute.Message属性的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.");
}
}
输出:
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