Attributes and Named/Optional constructor parameters not working

前端 未结 3 1883
耶瑟儿~
耶瑟儿~ 2021-01-01 12:09

I have custom attribute defined like so:

  [AttributeUsage(AttributeTargets.Field)]
  public class EnumDisplayAttribute : Attribute
  {
    public string Des         


        
3条回答
  •  [愿得一人]
    2021-01-01 12:49

    Optional parameters are not really optional, the method signature has all arguments in it and attributes are special (existed before optional parameters and have different rules when applied as an attribute (eg consider who calls the attribute constructor)). I imagine however that support will be added in the future.

    For now, if you wish to achieve the optional effect try the following:

    [AttributeUsage(AttributeTargets.Field)]
    public class EnumDisplayAttribute : Attribute
    {
      public string Description { get; set; }
      public string Code { get; set; }
    
    }
    

    And apply as so:

    [EnumDisplay(Description = null, Code = "C")]
    private object _aField;
    

提交回复
热议问题