Well, in C # we put attributes as follows:
[Required("Este campo é obrigatório")]
public string Nome {get; set;}
What I would like to know is whether there is a way to dynamically add attributes to instantiated objects. Like the following hypothetical code:
var pessoa = new Pessoa();
pessoa.Nome.AdicionarAtributo(Required());
Context
The context is as follows: I'm creating a Universal Windows project, using Prism and applying MVVM. However for each field of a model I'm creating a field in the ViewModel, then in the Save () method, I create an instance of the model and assign the values of the ViewModel. The attributes are currently in the ViewModel fields, but I wanted to do something different. My ViewModel would basically look like this:
public class ClienteViewModel: ValidatableBindableBase
{
private Cliente _modelo;
public Cliente Modelo
{
get => _modelo;
set => SetProperty(ref _modelo, value);
}
public ClienteViewModel()
{
Modelo.Campo.AdicionarAtributo(Required());
[...]
}
[...]
public void Salvar()
{
if(ValidateProperties()){
_serviço.Salvar(_modelo);
}
}
}
My goal is to clean up my ViewModel, if anyone knows an even better way to do this, it will be very welcome.