关于C#特性

u012522824 2014-02-09 07:49:10
using System;
using System.Reflection;
using System.Diagnostics;

//attaching Help attribute to entire assembly
[assembly: Help("This Assembly demonstrates custom attributes creation and their run-time query.")]
//our custom attribute class
public class HelpAttribute : Attribute
{
public HelpAttribute(String Description_in)
{
//
// TODO: Add constructor logic here
this.description = Description_in;
//
}
protected String description;
public String Description
{
get
{
return this.description;
}
}
}
//attaching Help attribute to our AnyClass
[Help("This is a do-nothing Class.")]
public class AnyClass
{
//attaching Help attribute to our AnyMethod
[Help("This is a do-nothing Method.")]
public void AnyMethod()
{
}
//attaching Help attribute to our AnyInt Field
[Help("This is any Integer.")]
public int AnyInt;
}
class QueryApp
{
public static void Main()
{
Type type = typeof(AnyClass);
HelpAttribute helpAttribute;
//Querying Assembly Attributes
String assemblyName;
Process process = Process.GetCurrentProcess();
assemblyName = process.ProcessName + ".exe";
Assembly assembly = Assembly.LoadFrom(assemblyName);
foreach (Attribute attribute in assembly.GetCustomAttributes(true))
{
helpAttribute = attribute as HelpAttribute;
if (null != helpAttribute)
{
Console.WriteLine("Description of {0}:\n{1}",
assemblyName, helpAttribute.Description);
}
}

Console.WriteLine("按任意键结束...");
Console.ReadKey();
}
}

上面是从网上看到用来学习的代码,输入代码后按照说明应该输出以下结果:
Description of QueryAttribute.exe:
This Assembly demonstrates custom attributes creation and
their run-time query.
Press any key to continue
但是我实际编译运行时什么结果也没有,请大家帮忙看看问题出在哪?谢谢大家!

...全文
205 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
anny_li 2014-02-10
  • 打赏
  • 举报
回复
引用 2 楼 junshanhudazhaxi 的回复:
我用你的代码是会有信息输出的。源代码
我觉得源代码的继承不对或者自定义属性有问题,因为反射出来的属性没有与之相匹配的!
u012522824 2014-02-10
  • 打赏
  • 举报
回复
引用 2 楼 junshanhudazhaxi 的回复:
我用你的代码是会有信息输出的。源代码
给于热心肠奖2点。
引用 3 楼 BenBenBears 的回复:
不要调试,直接运行就会有结果。
剩下的给予技术老大的报酬,谢谢了!
u012522824 2014-02-10
  • 打赏
  • 举报
回复
好棒,学到了东西!谢谢了!
junshanhudazhaxi 2014-02-09
  • 打赏
  • 举报
回复
我用你的代码是会有信息输出的。源代码
anny_li 2014-02-09
  • 打赏
  • 举报
回复
你把这个运行一下看看,我试了一下,OK的! //attaching Help attribute to entire assembly [assembly: Help("This Assembly demonstrates custom attributes creation and their run-time query.")] //our custom attribute class public class HelpAttribute : Attribute { public HelpAttribute(String Description_in) { // // TODO: Add constructor logic here this.description = Description_in; // } protected String description; public String Description { get { return this.description; } } } //attaching Help attribute to our AnyClass [Help("This is a do-nothing Class.")] public class AnyClass { //attaching Help attribute to our AnyMethod [Help("This is a do-nothing Method.")] public void AnyMethod() { } //attaching Help attribute to our AnyInt Field [Help("This is any Integer.")] public int AnyInt; } class QueryApp { public static void Main() { Type type = typeof(AnyClass); HelpAttribute HelpAttr; //Querying Class Attributes foreach (Attribute attr in type.GetCustomAttributes(true)) { HelpAttr = attr as HelpAttribute; if (null != HelpAttr) { Console.WriteLine("Description of AnyClass:\n{0}", HelpAttr.Description); } } //Querying Class-Method Attributes foreach (MethodInfo method in type.GetMethods()) { foreach (Attribute attr in method.GetCustomAttributes(true)) { HelpAttr = attr as HelpAttribute; if (null != HelpAttr) { Console.WriteLine("Description of {0}:\n{1}", method.Name, HelpAttr.Description); } } } //Querying Class-Field (only public) Attributes foreach (FieldInfo field in type.GetFields()) { foreach (Attribute attr in field.GetCustomAttributes(true)) { HelpAttr = attr as HelpAttribute; if (null != HelpAttr) { Console.WriteLine("Description of {0}:\n{1}", field.Name, HelpAttr.Description); } } } } }
largeskymengsk 2014-02-09
  • 打赏
  • 举报
回复
应该是先获取类型:assembly.GetTypes
BenBenBears 2014-02-09
  • 打赏
  • 举报
回复
http://blog.csdn.net/taonylu/article/details/4828783 这篇C#自定义属性里面也提到在程序运行时获取属性信息。 而“启动调试”和“开始执行(不调试)”的区别在于后者是直接执行编译后的程序,前者是加载到调试器(Debugger)上执行。
BenBenBears 2014-02-09
  • 打赏
  • 举报
回复
不要调试,直接运行就会有结果。

110,536

社区成员

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

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

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