I am looking for an effective way to automatically format data fields in essence - ideally using attributes.
We need to create a PDF file from the data model. We want to ensure consistency in the delivered product, so we strive to apply some formatting rules to certain data fields (dates, phone numbers, zip codes, etc.). Of course, I could write custom attributes and formatting code, but I would prefer not to reinvent the wheel. I see many promises using DataAnnotations (especially the DisplayFormat attribute), but I cannot find any built-in classes that work with these attributes.
How to do this in a context other than the UI (for example, not MVC)?
Here is an example of what we are looking for:
public class MyDataModel { [PhoneNumber] public string PhoneNumber { get; set; } public void FormatData() {
I am also open to solutions that create a โpresentationโ of data, and not to update the original object, namely:
MyDataModel formatted = original.FormatData();
Whatever requires the least amount of code, is ideal.
source share