求获取特定Attribute的值

niss 2012-10-10 05:41:53
[WebService(Namespace = "http://1.2.3/",Name="12344")]
public class WebService1:System.Web.Services.WebService
{
...

private string getName()
{
//怎么得到这个类的WebServiceAttribute中的Name值?
}
}

求高手,
我只知道一个GetCustomAttributes,问题是他返回的是数组
string s = "";
foreach (object attr in this.GetType().GetCustomAttributes(typeof(WebServiceAttribute), true))
{
if (attr is WebServiceAttribute)
{
s += ((WebServiceAttribute)attr).Name + ";";
}
}
return s;
这样会不会出现多个值啊
有没有其他方法啊
...全文
167 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
niss 2012-10-10
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 的回复:]

你说的这方面没怎么用过,
GetCustomAttribute(Name) 应该有个这样的方法
或者说你GetCustomAttributes返回的集合提供一个根据名字的索引[name] 来取出你所要的值
可以试试
[/Quote]
非常感谢,直接使用Attribute的静态方法GetCustomAttribute(type t,type attrType)就可以了
PaulyJiang 2012-10-10
  • 打赏
  • 举报
回复
你说的这方面没怎么用过,
GetCustomAttribute(Name) 应该有个这样的方法
或者说你GetCustomAttributes返回的集合提供一个根据名字的索引[name] 来取出你所要的值
可以试试

110,825

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧