Is there any way to dynamically add attribute to instantiated object in C #?

2

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.

    
asked by anonymous 26.05.2018 / 17:04

3 answers

2

When the instantiated object can not, attributes exist in types and their members, not in instances. It's usually what you need, and you almost always have no reason to put it dynamically, it gives you more work, you're more likely to go wrong without a real gain.

In fact, it does not make sense at all. And if it does for your case you should use a dynamic language and not C #. I think it's polluting one type, not the opposite.

If you think you're doing a lot of legwork, I welcome the world of complex architectures. Or start making simpler architectures or live with it.

Perhaps, a better solution would be to create a code generator. In some cases it is well suited.

    
26.05.2018 / 17:43
1

I have not tried this yet, I've seen people say that it's not possible, but some saying that it's possible:)

There is

TypeDescriptor.AddAttributes(Object, Attribute[])

You also have this example with FastDeepCloner

public class test{
public string Name{ get; set; }
}

var prop = now DeepCloner.GetFastDeepClonerProperties(typeof(test)).First();
prop.Attributes.Add(new JsonIgnoreAttribute());
// now test and se if exist 
prop = now DeepCloner.GetFastDeepClonerProperties(typeof(test)).First();
bool containAttr = prop.ContainAttribute<JsonIgnoreAttribute>()
// or 
JsonIgnoreAttribute myAttr = prop.GetCustomAttribute<JsonIgnoreAttribute>();

Source: link

    
26.05.2018 / 19:00
1

To clean up the ViewModel, I'll give you the NuGet hint PropertyChanged

public class ClienteViewModel: ValidatableBindableBase, INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    public Cliente Modelo {get; set;}

    public ClienteViewModel()
    {
        Modelo.Campo.AdicionarAtributo(Required());
        [...]
    }

    [...]
    public void Salvar()
    {
        if(ValidateProperties()){
            _serviço.Salvar(Modelo);
        }
    }
}    
    
26.05.2018 / 19:09