zoukankan      html  css  js  c++  java
  • 使用反射访问属性【MSDN】

    如果没有检索自定义属性的信息和对其进行操作的方法,则定义自定义属性并将其放置在源代码中就没有意义。C# 具有一个反射系统,可用来检索用自定义属性定义的信息。主要方法是 GetCustomAttributes,它返回对象数组,这些对象在运行时等效于源代码属性。此方法具有多个重载版本。

    属性规范,如:

    Code

    在概念上等效于:

    Code

    但是,直到查询 SampleClass 以获取属性时才会执行此代码。对 SampleClass 调用 GetCustomAttributes 会导致按上述方式构造并初始化一个 Author 对象。如果类还有其他属性,则其他属性对象的以类似方式构造。然后 GetCustomAttributes 返回 Author 对象和数组中的任何其他属性对象。之后就可以对此数组进行迭代,确定根据每个数组元素的类型所应用的属性,并从属性对象中提取信息。

    示例

    下面是一个完整的示例。定义一个自定义属性,将其应用于若干实体并通过反射进行检索。

    Code

    输出

    Author information for FirstClass

    H. Ackerman, version 1.00

    Author information for SecondClass

    Author information for ThirdClass

    H. Ackerman, version 1.00

    M. Knott, version 2.00

  • 相关阅读:
    一键编译go文件命令.bat
    安装go语言,配置环境及IDE,只需3步
    Mysql show Status常用参数详解
    LFS:kernel panic VFS: Unable to mount root fs
    LFS: Interface eth0 doesn't exist
    转:Xshell显示找不到匹配的outgoing encryption算法怎么办
    NTP-ntpdate:no server suitable for synchronization found
    linux/module.h: No such file or directory 内核模块编译过程
    ASCII码表完整版
    [Firmware Warn]: GHES: Failed to read error status block address for hardware error source
  • 原文地址:https://www.cnblogs.com/shineqiujuan/p/1414507.html
Copyright © 2011-2022 走看看