关于获取属性的description等问题

hpinger 2009-06-12 12:16:42
现有一个结构体
public struct Desc
{
private byte val1;
[Description("一个属性")]
public byte des
{
get { return val1; }
set { val1 = value; }
}

///...以下省略
}

请问怎样将des的描述description给弄出来?
我知道可以用反射的方法获取枚举中的description,但同样的方法却不能取得这个结构体的,为什么?
请教高手了。
...全文
382 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
hpinger 2009-06-13
  • 打赏
  • 举报
回复
明白了,用Type就ok
谢谢
hpinger 2009-06-13
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 zgke 的回复:]
GetCustomAttributes();是用来获取 特性的

可以通过 Assembly Type FieldInfo ParameterInfo MemberInfo EventInfo ...获取..

但你必须获取到 属性 成员 或则方法 的Info才能获取到他的特性
[/Quote]

结构体也可以么?(可能这样做的意义不大)
比如
[Description("一个结构体")]
public struct Desc
{
...
}
zgke 2009-06-13
  • 打赏
  • 举报
回复
GetCustomAttributes();是用来获取 特性的

可以通过 Assembly Type FieldInfo ParameterInfo MemberInfo EventInfo ...获取..

但你必须获取到 属性 成员 或则方法 的Info才能获取到他的特性
hpinger 2009-06-13
  • 打赏
  • 举报
回复
收获很大,散分!
不过有谁能总结一下对特性的获取方法?!



ps:新手注意了:Peroperty是属性,Attribute是特性,这样才不容易混淆
hpinger 2009-06-13
  • 打赏
  • 举报
回复
最后整理一下,分享给大家

string str = "";
Desc desc = new Desc();

////不用反射 获取属性的特性
PropertyDescriptor pd = TypeDescriptor.GetProperties(typeof(Desc))["des"];
DescriptionAttribute description = pd == null ? null : pd.Attributes[typeof(DescriptionAttribute)] as DescriptionAttribute;
str = description == null ? "" : description.Description;

////用反射 获取属性的特性
PropertyInfo pi = typeof(Desc).GetProperty("des");
foreach (object obj in pi.GetCustomAttributes(false))
{
if (obj is DescriptionAttribute)
str=(obj as DescriptionAttribute).Description;
}

////不用反射 获取结构体的特性
AttributeCollection attributes = TypeDescriptor.GetAttributes(desc);//or typeof(Desc)
DescriptionAttribute da = (DescriptionAttribute)attributes[typeof(DescriptionAttribute)];
if(attributes.Contains(da))
str = da.Description;

////用反射 获取结构体的特性
Type myType = typeof(Desc);
foreach (object obj in myType.GetCustomAttributes(false))
{
if (obj is DescriptionAttribute)
str = (obj as DescriptionAttribute).Description;
}
xxyping 2009-06-12
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 gomoku 的回复:]
C# codePropertyDescriptor pd=TypeDescriptor.GetProperties(typeof(Desc))["des"];
DescriptionAttribute description=pd==null?null: pd.Attributes[typeof(DescriptionAttribute) ]asDescriptionAttribute;if(description!=null)
{
MessageBox.Show(description.Description);
}
[/Quote]

UP
gomoku 2009-06-12
  • 打赏
  • 举报
回复
There are more than one ways to skin a cat :)

PropertyInfo pi = typeof(Desc).GetProperty("des");
foreach (object obj in pi.GetCustomAttributes(false))
{
if (obj is DescriptionAttribute)
{
MessageBox.Show((obj as DescriptionAttribute).Description);
break;
}
}
gomoku 2009-06-12
  • 打赏
  • 举报
回复

PropertyDescriptor pd = TypeDescriptor.GetProperties(typeof(Desc))["des"];
DescriptionAttribute description = pd == null ? null : pd.Attributes[ typeof(DescriptionAttribute) ] as DescriptionAttribute;
if (description != null)
{
MessageBox.Show(description.Description);
}
zenowolf 2009-06-12
  • 打赏
  • 举报
回复
1.用反射的确可以。。
2.也不知道
3.up一下

110,535

社区成员

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

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

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