Was this helpful?
This is in continuation to Reflection in .NET - Part 2
Querying the fields for their attached attributes
Consider a library app that has the entities like Book, Student, etc. Now you can use attributes to associate the names of the database tables and columns with the .NET classes and their member fields. For this purpose, define a class named DatabaseAttribute derived from the System.Attribute class.
Make the Table property of DatabaseAttribute class a named parameter and set its default value to Book so that the attribute's user doesn't need to key in this value most of the time as most of the member fields in this app belong to the Book entity. Now if the user wants to provide some value other than Book, she can use the named parameter like this: Table="Student"
Whenever an attribute is used, we want that the user must specify the column name corresponding to the target member field. Therefore, make the Column property as the positional parameter.
We will use reflection to query the fields of Student type about its attached attributes.
Similarly, you can query the fields of Book type about its attached attributes.
Querying the classes for their attached attributes
Mark the Alpha class with the Serializable and Obsolete attributes.
Note that the reflection has returned only the regular custom ObsoleteAttribute. It has not returned the pseudo-custom SerializableAttribute.
For Part 4, click this link: Reflection in .NET - Part 4
|posted||Apr 19, 2016|
|active||Apr 20, 2016|
|•||Reflection in .NET - Part 5|
|•||Reflection in .NET - Part 4|
|•||Reflection in .NET - Part 2|
|•||Reflection in .NET - Part 1|
|•||.NET Core project option: "Produce outputs on build"|
|•||Fastest way to serialize a POCO to JSON and deserialize JSON to a POCO|
|•||Serialization in .NET - Part 4|
|•||Reading appsettings.json in a .NET Core console application|
|•||Serialization in .NET - Part 3|
|•||Serialization in .NET - Part 2|