The name and value of the attribute and value of the reflection extraction
A common task in reflection is to retrieve attribute information associated with attributes from the attributes of the class. Consider the following example:
Here, attributes are applied to
attributes. Our goal is to use reflexes to obtain attribute names and values ("Author", "Authorname").<code class="language-csharp">public class Book { [Author("AuthorName")] public string Name { get; private set; } }</code>
For this, please follow the steps below: Author
Name
instance array of the class attribute.
typeof(Book).GetProperties()
type. PropertyInfo
PropertyInfo
GetCustomAttributes()
Author
The example of this kind of implementation is provided below: Author
By calling this function, you can get a dictionary that maps the attribute name to the author's name, thereby providing a complete list of attribute information associated with attributes. PropertyInfo
The above is the detailed content of How Can I Extract Attribute Names and Values from Class Properties Using Reflection?. For more information, please follow other related articles on the PHP Chinese website!