英文:
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("Mapping the JToken to the App ID")]
CreateMap<JToken, RAFApplicationID>().ForAllMembers(opt =>
{
//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:
/// <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 <see cref="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);
}
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论