EDIT: The referenced duplicate is not an option as it was answered 5 years ago (and my reference 6 years ago) and I was hoping there was something more recent that did this rather than hand crafted work around code. Nor is the referenced duplicate a fully declarative solution.
I have a number of string properties off a class that are only allowed to be a maximum length. Coming from a Delphi background you were able to define a custom type as:
type TString10 = string[10];
type TString50 = string[50];
and then use that as part of a property definition:
property TString10 MyString
property TString10 MyOtherString
property TString50 YetOtherString
property TString50 AndTheLastOne
There appears to be no declarative equivalent in C# that I am aware of? So it appears my only options are:
- Write manual property setters or getters (that truncate to the specified length)
- Call a validation method that checks the properties to ensure their length is as needed
Or are there other approaches to enforcing a length on a string property declaritively?