【How to: Create a C/C++ Union by Using Attributes (C#)】
1、you can create what is known as a union in C/C++ by using the StructLayout(LayoutKind.Explicit)
and FieldOffset
attributes.
2、Below the two integer fields, i1
and i2
, share the same memory locations as lg
. This sort of control over struct layout is useful when using platform invocation.
参考:
【Accessing Attributes by Using Reflection (C#)】
1、you can define custom attributes and place them in your source code. you can retrieve the information that was defined with custom attributes. The key method is GetCustomAttributes
, which returns an array of objects that are the run-time equivalents of the source code attributes.
The code is not executed until SampleClass
is queried for attributes. Calling GetCustomAttributes
on SampleClass
causes an Author
object to be constructed and initialized as above. If the class has other attributes, other attribute objects are constructed similarly. GetCustomAttributes
then returns the Author
object and any other attribute objects in an array. You can then iterate over this array, determine what attributes were applied based on the type of each array element, and extract information from the attribute objects.
2、下面是一个 Attribute 的定义,通过 System.AttributeUsage() 定义这个新 Attribute的用法。
参考: