#define DEBUG
using System.Diagnostics;
namespace Attribute
{
using System;
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello World!");
//Function1();
HelpAttribute helpAttribute;
foreach (var attr in typeof(AnyClass).GetCustomAttributes(true))
{
helpAttribute = attr as HelpAttribute;
if (null != helpAttribute)
{
Console.WriteLine("AnyClass Name: {0}",
helpAttribute.Name);
Console.WriteLine("AnyClass Description: {0}",
helpAttribute.Description);
}
}
Console.ReadLine();
}
[Obsolete("Don't use Old Method!", true)] // 表示方法已经过时了,不推荐使用,第二个参数 是否提示报错
static void Function1()
{
MyClass.Message("In Function1");
Function2();
}
static void Function2()
{
MyClass.Message("In Function2");
}
}
// value type, System.Type, object, enum
[Help("this is a do-nothing class", Name = "Jikexueyuan")]
public class AnyClass
{
}
// AllowMultiple 是否可以放多次
// Inherited 是否可以继承
[AttributeUsage(AttributeTargets.Class, AllowMultiple = false, Inherited = false)]
public class HelpAttribute : Attribute
{
private string description;
public HelpAttribute(string description)
{
this.description = description;
}
public string Description { get => description; }
public string Name
{
get;
set;
}
}
public class MyClass
{
[Conditional("DEBUG")]
public static void Message(string msg)
{
Console.WriteLine(msg);
}
}
}