0

i have this model

  public class AppInfo
  {
    [Required(ErrorMessage = "Campo obrigatório")]
    [NonEditable]
    public string nick { get; set; }
    [Required(ErrorMessage = "Campo obrigatório")]
    [NonEditable]
    public string version { get; set; }
    public string description { get; set; }
    public bool invalidated { get; set; }
    public System.DateTime releasedAt { get; set; }
    public System.DateTime createdAt { get; set; }
    public System.DateTime updatedAt { get; set; }
  }

and i want to create a NonEditable attribute for a PATCH request, what can i do inside the Attribute to do what i want ? I have no ideia what to do

Attribute class:

  [AttributeUsage(AttributeTargets.All)]
  public class NonEditableAttribute : Attribute
  {
    private string name;
    private string action;

    public NonEditableAttribute([CallerMemberName] string name = null)
    {
      this.name = name;
    }
  }
}
Gabriel Guedes
  • 481
  • 5
  • 15
  • The question is weird as it is. I don't understand what does an attribute has to do with a property being editable or not. The only thing that control that is the property setter which is `public`. You should have `public string version { get; private set; }` instead. – Franck Jan 06 '20 at 14:50
  • Due to the fact, that you write about a PATCH request, I would think, you maybe want to ignore this property when it's being serialized. Or if you like to make it not settable, you have to work with either a `{ get; private set; }` or `{ get; }` property and set in the constructor, but in that case your code that creates an instance of that class has also to support that immutable pattern. – Oliver Jan 06 '20 at 15:01
  • An attribute can't help you in that case, cause it only gives you some kind of meta information that can be attached to a property. If and how this attribute type and value is used depends on the class that uses this instance. But making a property read-only is definitive not a job for an attribute. – Oliver Jan 06 '20 at 15:04
  • oh, i understand, i didnt know it, im a begginer at dotnet hehe thanks for the help – Gabriel Guedes Jan 06 '20 at 15:58

0 Answers0