Assuming we have a class with metadata, we hope to retrieve the attribute name and value through reflection. For example, this class has a attribute called
and is associated with a attribute. Name
Author
We can use to retrieve the attribute information of the
attributes). typeof(Book).GetProperties()
Book
Once the attribute is found, we can obtain the attribute name from the GetCustomAttributes()
instance and obtain the attribute value by the attribute itself. The following code realizes the type of scanning, identifies the attributes of Author
attributes, and generate a dictionary containing the attribute details:
Author
This method can retrieve attribute names and values with attributes with specific attributes during runtime. PropertyInfo
The above is the detailed content of How Can I Retrieve Attribute Properties and Values Using Reflection in C#?. For more information, please follow other related articles on the PHP Chinese website!