Tekkaman

导航

 

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 lgThis sort of control over struct layout is useful when using platform invocation.

  

参考:

1、https://docs.microsoft.com/zh-cn/dotnet/csharp/programming-guide/concepts/attributes/how-to-create-a-c-cpp-union-by-using-attributes

 

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的用法。

  

 

参考:

1、https://docs.microsoft.com/zh-cn/dotnet/csharp/programming-guide/concepts/attributes/accessing-attributes-by-using-reflection

posted on 2019-07-19 14:37  Tekkaman  阅读(349)  评论(0编辑  收藏  举报