问个Attribute的小问题???????????????
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
UserService.AddUser();
}
}
class UserService
{
[My]
public static void AddUser()
{
Console.WriteLine("数据写入到数据库成功!");
}
}
class MyAttribute : System.Attribute
{
public MyAttribute()
{
Console.WriteLine("=============================");
Console.WriteLine("时间:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
Console.WriteLine("=============================");
}
}
}
static void Main(string[] args)
{
UserService.AddUser();
Type classType = typeof(UserService);
foreach (MethodInfo method in classType.GetMethods())
{
foreach (Attribute attr in Attribute.GetCustomAttributes(method))
{
if (attr.GetType() == typeof(MyAttribute))
{
MyAttribute att = (MyAttribute)attr;
}
}
}
}
class UserService
{
[My]
public static void AddUser()
{
Console.WriteLine("数据写入到数据库成功!");
}
}
[AttributeUsage(AttributeTargets.All)]
class MyAttribute : System.Attribute
{
public MyAttribute()
{
Console.WriteLine("=============================");
Console.WriteLine("时间:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
Console.WriteLine("=============================");
}
}