§ ITPOW >> 文档 >> C#

C# 中的特性(Attributes)-4

作者:子非鱼 来源:子非鱼的 blog 日期:2009-1-31

我们将在接下来的内容中在我们的 Main 方法中加入 attribute 查询代码。

查询程序集的Attributes

在接下来的代码中,我们先得到当前的进程名称,然后用Assembly类中的LoadForm()方法加载程序集,再有用GetCustomAttributes()方法得到被绑定至当前程序集的自定义attributes,接下来用foreach语句遍历所有attributes并试图把每个attribute转型为Help attribute(即将转型的对象使用as关键字有一个优点,就是当转型不合法时,我们将不需担心会抛出异常,代之以空值(null)作为结果),接下来的一行就是检查转型是否有效,及是不是为空,跟着就显示Help attribute的“Description”属性。

class QueryApp
{
    public static void Main()
    {
        HelpAttribute HelpAttr;
       
        //Querying Assembly Attributes
        String assemblyName;
        Process p = Process.GetCurrentProcess();
        assemblyName = p.ProcessName + ".exe";
        Assembly a = Assembly.LoadFrom(assemblyName);
       
        foreach (Attribute attr in a.GetCustomAttributes(true))
        {
            HelpAttr = attr as HelpAttribute;
            if (null != HelpAttr)
            {
                Console.WriteLine("Description of {0}:\n{1}", assemblyName,HelpAttr.Description);
            }
        }
    }
}

程序输出如下:

Description of QueryAttribute.exe:
This Assembly demonstrates custom attributes creation and their run-time query.
Press any key to continue

查询类、方法、类成员的 Attributes

下面的代码中,我们惟一不熟悉的就是Main()方法中的第一行。

Type type = typeof(AnyClass);

它用typeof操作符得到了一个与我们AnyClass类相关联的Type型对象。剩下的查询类attributes代码就与上面的例子是相似的,应该不要解释了吧(我是这么想的)。

为查询方法和类成员的attributes,首先我们得到所有在类中存在的方法和成员,然后我们查询与它们相关的所有attributes,这就跟我们查询类attributes一样的方式。

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);
                }
            }
        }
    }
}

The output of the following program is.

Description of AnyClass:
This is a do-nothing Class.
Description of AnyMethod:
This is a do-nothing Method.
Description of AnyInt:
This is any Integer.
Press any key to continue
相关文章