如何取得一个类的描述文本?

Sunnuy 2007-03-19 09:55:38
[Description("日程安排")]
public partial class test : System.Web.UI.UserControl

怎样取得这个Description中的内容?
...全文
193 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
hertcloud 2007-03-19
  • 打赏
  • 举报
回复
use Reflection, for example:

using System;
using System.ComponentModel;
using System.Reflection;

class TestDesc
{
[Description("Number")]
public int Count
{
get {return _count;}
set {_count= value;}
}

int _count;

public static void Main()
{
Type t = typeof(TestDesc);
MemberInfo[] mis = t.GetMember("Count");

foreach (Attribute a in mis[0].GetCustomAttributes(typeof(DescriptionAttribute),false))
{
Console.WriteLine(((DescriptionAttribute)a).Description);
}
}
}
hy_lihuan 2007-03-19
  • 打赏
  • 举报
回复
学习了
cpp2017 2007-03-19
  • 打赏
  • 举报
回复
Type t = typeof(test ); System.ComponentModel.DescriptionAttribute[] at =(System.ComponentModel.DescriptionAttribute[]) t.GetCustomAttributes(typeof(System.ComponentModel.DescriptionAttribute), false); Response.Write(at[0].Description);
sanlng 2007-03-19
  • 打赏
  • 举报
回复
反射:http://hi.baidu.com/sanlng/blog/item/7202b758b83c4ede9c8204e6.html
特性:http://hi.baidu.com/sanlng/blog/item/9cd58c01aef593061c95832f.html
两篇基础的文章,希望对楼主有帮助
hegang888888888 2007-03-19
  • 打赏
  • 举报
回复
namespace attri
{
[AttributeUsage(AttributeTargets.Class)]
public class myAttribute : Attribute
{
private string name;
private string age;

public string Name
{
get
{
return name;
}
set
{
name = value;
}
}

public string Age
{
set
{
age = value;
}
get
{
return age;
}
}
public myAttribute(string name)
{
this.name = name;

//
// TODO: 在此处添加构造函数逻辑
//
}
}

[attri.my("hegang",Age="28")]
public class AttributeTest
{
public AttributeTest()
{

}
}
}



使用:

Type t = typeof(AttributeTest);
attri.myAttribute[] mt = (attri.myAttribute[])Attribute.GetCustomAttributes(t, typeof(attri.myAttribute));
Response.Write(mt[0].Name + " " + mt[0].Age);
hegang888888888 2007-03-19
  • 打赏
  • 举报
回复
用反射

62,046

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

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