Background:
I have a custom class, which represents a Data Base Table, each property corresponding to a table column. The properties can be classified in three ways.
Example: Take for example a Person object.
- MetaProperties: (Columns that are needed by the program)
- Person_ID: used in table for indexing etc…
- UserDefinedType: (UDT), complex class handling write-permission on the table.
- Timestamp: needed to handle the UDT in C# DataTables
- RealProperties: (actual traits that describe the real Person)
- FullName
- DateOfBirth
- PlaceOfBirth
- EyeColor
- etc… (many more)
-
RawDataProperties: (these columns hold raw data from external sources)
- Phys_EyeColor: the eye-color, as directly imported from the physical traits database, may be in unknown format, may have conflicting value with entry from other db, or any other data quality issue…
- HR_FullName: full name as given in HR file
- Web_FullName: full name as taken from a web form
- Web_EyeColor: eye color as taken from web form
- etc…
public class Person
{#region MetaProperties public int Person_ID { get; set; } public UserDefinedType UDT { get; set; } public DateTime timestamp { get; set; } #endregion #region RealProperties public string FullName { get; set; } public DateTime DateOfBirth { get; set; } public string PlaceOfBirth { get; set; } public Color EyeColor { get; set; } //... #endregion #region RawDataProperties public string Phys_EyeColor { get; set; } public string Phys_BodyHeight { get; set; } public string Web_FullName { get; set; } public string Web_EyeColor { get; set; } public string HR_FullName { get; set; } //... #endregion}
Question: How can I programmatically differentiate between these three types of properties in my Person class? The goal is to be able to iterate through properties of a certain type using System.Reflection or some other organisational construct. Pseudocode:
foreach(Property prop in Person.GetPropertiesOfType("RealProperty"){
... doSmth(prop);
}
I’m thinking about writing custom Attributes, and hanging them on to the properties, sort of like taggin.
But since I know nothing about custom Attributes, I would like to ask if I’m going down the proper path, or if there are any other better ways of doing this.
Note: the shown example may may not be the best in terms of program design, and I am well aware that inheritance or splitting up the class otherwise could solve this problem. But that is not my question – I want to know if properties in a class can be tagged or somehow differentiated between using custom categories.
You can do this with custom attributes.
Then, you can do this with each property or field:
Then you can access it with something like