.NET(C#):获取方法返回值的自定义特性(Attribute)
2013-05-08 10:54
483 查看
来自:http://www.cnblogs.com/mgen/archive/2011/11/02/2233374.html
.NET中特性的索取就是围绕着ICustomAttributeProvider接口(System.Reflection命名空间内),而MethodInfo类的ReturnTypeCustomAttributes属性直接返回方法返回值的ICustomAttributeProvider接口对象。同时MethodBase的ReturnParameter属性返回方法返回值信息(ParameterInfo),而ParameterInfo也是继承ICustomAttributeProvider的,所以这两个属性都可以得到方法返回值的特性。注意基类MethodBase没有相应属性,由于ConstructorInfo(代表构造函数信息)没有返回值。
代码:
using System;
using System.Reflection;
namespace Mgen
{
[AttributeUsage(AttributeTargets.ReturnValue)]
class
MyAttr :
Attribute
{
public
int Data {
get; set; }
}
class
Program
{
static
void Main(string[] args)
{
var method
= typeof(Program).GetMethod("doo");
test(method.ReturnTypeCustomAttributes);
test(method.ReturnParameter);
}
static
void test(ICustomAttributeProvider customAttrProvider)
{
if (customAttrProvider.IsDefined(typeof(MyAttr),
false))
{
var att
= (MyAttr)customAttrProvider.GetCustomAttributes(typeof(MyAttr),
false)[0];
Console.WriteLine(att.Data);
}
}
[return:
MyAttr(Data
= 17)]
public
static
string doo()
{
return
"hehe";
}
}
}
输出两个17. 两种方法都可以获取返回值的自定义特性。
.NET中特性的索取就是围绕着ICustomAttributeProvider接口(System.Reflection命名空间内),而MethodInfo类的ReturnTypeCustomAttributes属性直接返回方法返回值的ICustomAttributeProvider接口对象。同时MethodBase的ReturnParameter属性返回方法返回值信息(ParameterInfo),而ParameterInfo也是继承ICustomAttributeProvider的,所以这两个属性都可以得到方法返回值的特性。注意基类MethodBase没有相应属性,由于ConstructorInfo(代表构造函数信息)没有返回值。
代码:
using System;
using System.Reflection;
namespace Mgen
{
[AttributeUsage(AttributeTargets.ReturnValue)]
class
MyAttr :
Attribute
{
public
int Data {
get; set; }
}
class
Program
{
static
void Main(string[] args)
{
var method
= typeof(Program).GetMethod("doo");
test(method.ReturnTypeCustomAttributes);
test(method.ReturnParameter);
}
static
void test(ICustomAttributeProvider customAttrProvider)
{
if (customAttrProvider.IsDefined(typeof(MyAttr),
false))
{
var att
= (MyAttr)customAttrProvider.GetCustomAttributes(typeof(MyAttr),
false)[0];
Console.WriteLine(att.Data);
}
}
[return:
MyAttr(Data
= 17)]
public
static
string doo()
{
return
"hehe";
}
}
}
输出两个17. 两种方法都可以获取返回值的自定义特性。
相关文章推荐
- C#提高--------------获取方法返回值的自定义特性(Attribute)
- .net c#获取自定义Attribute
- Asp.net Mvc过滤器获取请求方法自定义特性
- .net c#获取自定义Attribute
- .net c#获取自定义Attribute
- .net c#获取自定义Attribute
- C# 通过反射获取MVC Controller里的类名,方法名,参数列表,返回值类型,Description描述,自定义Attribute
- C# .NET 获取枚举值的自定义属性(特性)信息
- C# .NET 获取枚举值的自定义属性(特性)信息
- C# .NET 获取枚举值的自定义属性(特性/注释/备注)信息
- asp.net中C#获取字符串中汉字的个数的具体实现方法
- ASP.NET MVC WebApi 返回数据类型序列化控制(json,xml) 用javascript在客户端删除某一个cookie键值对 input点击链接另一个页面,各种操作。 C# 往线程里传参数的方法总结 TCP/IP 协议 用C#+Selenium+ChromeDriver 生成我的咕咚跑步路线地图 (转)值得学习百度开源70+项目
- C#.Net中获取程序运行时间的两种方法 (转载:陈进学博客)
- asp.net C#网站项目 获取物理路径、虚拟路径的方法
- Sql2012如何将远程服务器数据库及表、表结构、表数据导入本地数据库 自定义日志记录功能,按日记录,很方便 C#常量和字段以及各种方法的语法总结 类型,对象,线程栈,托管堆在运行时的关系,以及clr如何调用静态方法,实例方法,和虚方法 asp.net webapi 自定义身份验证
- C#(ASP.Net)获取当前路径的方法集合
- paip. C#.NET循环获取不同随机数的方法根据时间
- 用asp.net(c#)获取网页源代码(两种方法)
- C#.net 获取文件路径方法(Winform)
- c# 特性/属性(Attribute) 以及使用反射查看自定义特性