主要通过一个示例来解释这个概念。
using System;

using System.Collections.Generic;

using System.Text;

using System.Reflection ;


namespace AttributeTest


...{


/**//// <summary>

/// 代码属性类

/// </summary>

[AttributeUsage (AttributeTargets .Method | AttributeTargets .Class )]

public class CodeAttirbute:System .Attribute


...{

private string name = string.Empty;

private string email = string.Empty;


public CodeAttirbute(string initName,string initEmail)


...{

name = initName;

email = initEmail;

}


public string Name


...{

get


...{

return name;

}

set


...{

name = value;

}

}


public string Email


...{

get


...{

return email;

}

set


...{

email = value;

}

}

}



/**//// <summary>

/// 工具类,通过自定义的代码属性查找某个方法的作者和Email

/// </summary>

public class AttributeTool


...{

public AttributeTool()


...{


}


public static CodeAttirbute getAttirbute(MethodBase method)


...{


/**//*

* 返回由System.Type标识的自定义属性的数组

* 很难理解,大概意思就是把该方法的代码属性类型化成CodeAttirbute的代码属性

*/

object [] attributes = method.GetCustomAttributes(typeof(CodeAttirbute), true);

return (CodeAttirbute )attributes[0];

}

}



/**//// <summary>

/// 使用上面的工具类,在出现异常后,显示该方法的作者信息

/// </summary>

public class UseAttributeTool


...{

public UseAttributeTool()


...{


}


[CodeAttirbute("1", "2")]

public void test()


...{

try


...{

int a = 1;

int b = 0;

int c = a / b;

}

catch (Exception e)


...{

Console.WriteLine(e.Message);


//MethodBase.GetCurrentMethod():返回表示当前正在执行的方法的MethodBase对象

CodeAttirbute ca = AttributeTool.getAttirbute(MethodBase.GetCurrentMethod());


//显示代码属性

Console.WriteLine("Name:{0},Email:{1}",ca.Name ,ca.Email );

}


}


static void Main()


...{

UseAttributeTool u = new UseAttributeTool();

u.test();

Console.Read();

}


}


}

这个示例主要用来当方法中出现异常后,能显示编写该 方法的作者信息,以便修改。
代码如下:
using System;
using System.Collections.Generic;
using System.Text;
using System.Reflection ;

namespace AttributeTest

...{ 

/**//// <summary>
/// 代码属性类
/// </summary>
[AttributeUsage (AttributeTargets .Method | AttributeTargets .Class )]
public class CodeAttirbute:System .Attribute 

...{
private string name = string.Empty;
private string email = string.Empty;

public CodeAttirbute(string initName,string initEmail)

...{
name = initName;
email = initEmail;
}

public string Name

...{
get

...{
return name;
}
set

...{
name = value;
}
}

public string Email

...{
get

...{
return email;
}
set

...{
email = value;
}
}
}


/**//// <summary>
/// 工具类,通过自定义的代码属性查找某个方法的作者和Email
/// </summary>
public class AttributeTool

...{
public AttributeTool()

...{

}

public static CodeAttirbute getAttirbute(MethodBase method)

...{

/**//*
* 返回由System.Type标识的自定义属性的数组
* 很难理解,大概意思就是把该方法的代码属性类型化成CodeAttirbute的代码属性
*/
object [] attributes = method.GetCustomAttributes(typeof(CodeAttirbute), true);
return (CodeAttirbute )attributes[0];
}
}


/**//// <summary>
/// 使用上面的工具类,在出现异常后,显示该方法的作者信息
/// </summary>
public class UseAttributeTool

...{
public UseAttributeTool()

...{ 

}

[CodeAttirbute("1", "2")]
public void test()

...{
try

...{
int a = 1;
int b = 0;
int c = a / b;
}
catch (Exception e)

...{
Console.WriteLine(e.Message);

//MethodBase.GetCurrentMethod():返回表示当前正在执行的方法的MethodBase对象
CodeAttirbute ca = AttributeTool.getAttirbute(MethodBase.GetCurrentMethod());

//显示代码属性
Console.WriteLine("Name:{0},Email:{1}",ca.Name ,ca.Email );
}

}

static void Main()

...{
UseAttributeTool u = new UseAttributeTool();
u.test();
Console.Read();
}

}

}

本文介绍了一个 C# 中自定义属性的应用示例。通过为方法添加自定义属性,可以在运行时获取到方法作者的名字和邮箱信息,方便在异常处理时快速定位到责任人。
1641

被折叠的 条评论
为什么被折叠?



