为AutoMapper添加描述属性

huangapple go评论61阅读模式
英文:

Add Description Attribute to AutoMapper

问题

我想要将一个简单的DescriptionAttribute添加到CreateMap配置中:

public class AppAutoMapper : Profile
{
    public AppAutoMapper()
    {
        [Description("Mapping the JToken to the App ID")]
        CreateMap<JToken, RAFApplicationID>().ForAllMembers(opt =>
        {
            //MAPPING TOKEN TO APP ID MODEL
        });
    }
}

但是它显示了一个错误:

Error CS7014 Attributes are not valid in this context.

据我理解,DescriptionAttribute 可以在任何地方使用:

/// <summary>
/// Specifies a description for a property or event.
/// </summary>
[AttributeUsage(AttributeTargets.All)]
public class DescriptionAttribute : Attribute
{
    /// <summary>
    /// Specifies the default value for the <see cref='System.ComponentModel.DescriptionAttribute'/>,
    /// which is an empty string (""). This <see langword='static'/> field is read-only.
    /// </summary>
    public static readonly DescriptionAttribute Default = new DescriptionAttribute();

    public DescriptionAttribute() : this(string.Empty)
    {
    }

    /// <summary>
    /// Initializes a new instance of the <see cref='System.ComponentModel.DescriptionAttribute'/> class.
    /// </summary>
    public DescriptionAttribute(string description)
    {
        DescriptionValue = description;
    }

    /// <summary>
    /// Gets the description stored in this attribute.
    /// </summary>
    public virtual string Description => DescriptionValue;

    /// <summary>
    /// Read/Write property that directly modifies the string stored in the description
    /// attribute. The default implementation of the "Description" property
    /// simply returns this value.
    /// </summary>
    protected string DescriptionValue { get; set; }

    public override bool Equals([NotNullWhen(true)] object? obj) =>
        obj is DescriptionAttribute other && other.Description == Description;

    public override int GetHashCode() => Description?.GetHashCode() ?? 0;

    public override bool IsDefaultAttribute() => Equals(Default);
}

那为什么这个不起作用呢?

英文:

I wanted to add a simple DescriptionAttribute to the CreateMap configuration:

public class AppAutoMapper : Profile
    {
        public AppAutoMapper()
        {
            [Description(&quot;Mapping the JToken to the App ID&quot;)]
            CreateMap&lt;JToken, RAFApplicationID&gt;().ForAllMembers(opt =&gt;
            {
                //MAPPING TOKEN TO APP ID MODEL
            });
        }
    }

but it's showing an error:

Error	CS7014	Attributes are not valid in this context.

As I undertand it, the DescriptionAttribute can be used anywhere:

/// &lt;summary&gt;
/// Specifies a description for a property or event.
/// &lt;/summary&gt;
[AttributeUsage(AttributeTargets.All)]
public class DescriptionAttribute : Attribute
{
    /// &lt;summary&gt;
    /// Specifies the default value for the &lt;see cref=&#39;System.ComponentModel.DescriptionAttribute&#39;/&gt;,
    /// which is an empty string (&quot;&quot;). This &lt;see langword=&#39;static&#39;/&gt; field is read-only.
    /// &lt;/summary&gt;
    public static readonly DescriptionAttribute Default = new DescriptionAttribute();

    public DescriptionAttribute() : this(string.Empty)
    {
    }

    /// &lt;summary&gt;
    /// Initializes a new instance of the &lt;see cref=&#39;System.ComponentModel.DescriptionAttribute&#39;/&gt; class.
    /// &lt;/summary&gt;
    public DescriptionAttribute(string description)
    {
        DescriptionValue = description;
    }

    /// &lt;summary&gt;
    /// Gets the description stored in this attribute.
    /// &lt;/summary&gt;
    public virtual string Description =&gt; DescriptionValue;

    /// &lt;summary&gt;
    /// Read/Write property that directly modifies the string stored in the description
    /// attribute. The default implementation of the &lt;see cref=&quot;Description&quot;/&gt; property
    /// simply returns this value.
    /// &lt;/summary&gt;
    protected string DescriptionValue { get; set; }

    public override bool Equals([NotNullWhen(true)] object? obj) =&gt;
        obj is DescriptionAttribute other &amp;&amp; other.Description == Description;

    public override int GetHashCode() =&gt; Description?.GetHashCode() ?? 0;

    public override bool IsDefaultAttribute() =&gt; Equals(Default);
}

so why isn't this one working?

答案1

得分: 1

AttributeTargets.All 表示它允许在以下所有地方使用该属性:

  • 程序集 (Assembly)
  • 模块 (Module)
  • 类 (Class)
  • 结构体 (Struct)
  • 枚举 (Enum)
  • 构造函数 (Constructor)
  • 方法 (Method)
  • 属性 (Property)
  • 字段 (Field)
  • 事件 (Event)
  • 接口 (Interface)
  • 参数 (Parameter)
  • 委托 (Delegate)
  • 返回值 (Return value)
  • 泛型参数 (Generic parameter)

在你尝试将其应用于构造函数内的语句时,其中没有匹配的情况。

英文:

AttributeTargets.All means it allows using the attribute on all of these:

  • Assembly
  • Module
  • Class
  • Struct
  • Enum
  • Constructor
  • Method
  • Property
  • Field
  • Event
  • Interface
  • Parameter
  • Delegate
  • Return value
  • Generic parameter

None of these match your case where you are trying to apply it to a statement within a constructor.

huangapple
  • 本文由 发表于 2023年6月16日 09:40:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/76486481.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定