马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?立即注册
×
你可以界说自界说特性并将其放入源代码中这一究竟,在没有检索该信息并对其举行操纵的方法的情况下将没有任何代价。 通过利用反射,可以检索通过自界说特性界说的信息。 告急方法是 GetCustomAttributes,它返回对象数组,这些对象在运行时等效于源代码特性。 此方法有很多重载版本。
特性规范,比方:- [Author("P. Ackerman", Version = 1.1)]
- class SampleClass { }
复制代码 在概念上等效于以下代码:- var anonymousAuthorObject = new Author("P. Ackerman")
- {
- Version = 1.1
- };
复制代码 但是,在为特性查询 SampleClass 之前,代码将不会实行。 对 SampleClass 调用 GetCustomAttributes 会导致构造并初始化一个 Author 对象。 如果该类具有其他特性,则将以雷同方式构造其他特性对象。 然后 GetCustomAttributes 会以数组情势返回 Author 对象和任何其他特性对象。 之后你便可以循环访问此数组,根据每个数组元素的范例确定所应用的特性,并从特性对象中提取信息。
下面是完备的示例。 界说自界说特性、将其应用于多个实体,并通过反射对其举行检索。- // Multiuse attribute.
- [System.AttributeUsage(System.AttributeTargets.Class |
- System.AttributeTargets.Struct,
- AllowMultiple = true) // Multiuse attribute.
- ]
- public class AuthorAttribute : System.Attribute
- {
- string Name;
- public double Version;
- public AuthorAttribute(string name)
- {
- Name = name;
- // Default value.
- Version = 1.0;
- }
- public string GetName() => Name;
- }
- // Class with the Author attribute.
- [Author("P. Ackerman")]
- public class FirstClass
- {
- // ...
- }
- // Class without the Author attribute.
- public class SecondClass
- {
- // ...
- }
- // Class with multiple Author attributes.
- [Author("P. Ackerman"), Author("R. Koch", Version = 2.0)]
- public class ThirdClass
- {
- // ...
- }
- class TestAuthorAttribute
- {
- public static void Test()
- {
- PrintAuthorInfo(typeof(FirstClass));
- PrintAuthorInfo(typeof(SecondClass));
- PrintAuthorInfo(typeof(ThirdClass));
- }
- private static void PrintAuthorInfo(System.Type t)
- {
- System.Console.WriteLine($"Author information for {t}");
- // Using reflection.
- System.Attribute[] attrs = System.Attribute.GetCustomAttributes(t); // Reflection.
- // Displaying output.
- foreach (System.Attribute attr in attrs)
- {
- if (attr is AuthorAttribute a)
- {
- System.Console.WriteLine($" {a.GetName()}, version {a.Version:f}");
- }
- }
- }
- }
- /* Output:
- Author information for FirstClass
- P. Ackerman, version 1.00
- Author information for SecondClass
- Author information for ThirdClass
- R. Koch, version 2.00
- P. Ackerman, version 1.00
- */
复制代码 怎样利用特性创建 C/C++ 连合
通过利用特性,可自界说结构在内存中的结构方式。 比方,可利用 StructLayout(LayoutKind.Explicit) 和 FieldOffset 特性在 C/C++ 中创建所谓的连合。
在此代码段中,TestUnion 的全部字段均从内存中的同一位置开始。- [System.Runtime.InteropServices.StructLayout(LayoutKind.Explicit)]
- struct TestUnion
- {
- [System.Runtime.InteropServices.FieldOffset(0)]
- public int i;
- [System.Runtime.InteropServices.FieldOffset(0)]
- public double d;
- [System.Runtime.InteropServices.FieldOffset(0)]
- public char c;
- [System.Runtime.InteropServices.FieldOffset(0)]
- public byte b;
- }
复制代码 以下代码是另一个示例,此中的字段从差别的显式设置位置开始。- [System.Runtime.InteropServices.StructLayout(LayoutKind.Explicit)]
- struct TestExplicit
- {
- [System.Runtime.InteropServices.FieldOffset(0)]
- public long lg;
- [System.Runtime.InteropServices.FieldOffset(0)]
- public int i1;
- [System.Runtime.InteropServices.FieldOffset(4)]
- public int i2;
- [System.Runtime.InteropServices.FieldOffset(8)]
- public double d;
- [System.Runtime.InteropServices.FieldOffset(12)]
- public char c;
- [System.Runtime.InteropServices.FieldOffset(14)]
- public byte b;
- }
复制代码 组合的两个整数字段 i1 和 i2 与 lg 共享雷同的内存位置。 lg 利用前 8 个字节,或 i1 利用前 4 个字节且 i2 利用后 4 个字节。 利用平台调用时,这种对结构结构的控制很有效。
|