gpt4 book ai didi

C# 数据注释,不能扩展特定属性 - 有哪些可能性?

转载 作者:行者123 更新时间:2023-12-03 22:29:48 24 4
gpt4 key购买 nike

我正在使用 ASP.NET 动态数据构建遗留应用程序。按照惯例,这些模型都是只读的,可以通过属性设置显示名称或描述。

这很好用,但是,现在我需要查询两个不同的源(资源文件和其他一些源)以获取显示名称。

之前的代码是干净的,因为我们只查询了资源:

[Display(ResourceType = typeof(Resources.m_Res), Name = "M_RES_MIMETYPE_ID", Description = "M_RES_MIMETYPE_ID_DESCR")]

这完全没问题,并且按预期工作。但是,现在我必须首先从其他文件中获取显示名称和描述,如果其他一切都失败了,我必须回退到资源。

我必须以这种方式创建两个不同的自定义属性:
    public class MGADisplayName : DisplayNameAttribute
{
private readonly string propertyName;
public string Name { get; set; }
public Type TableName { get; set; }
public Type ResourceType { get; set; }

public MGADisplayName([CallerMemberName] string PropertyName = null)
{
propertyName = PropertyName;
}

public override string DisplayName
{
get
{
var key = (TableName.Name + ":" + (propertyName ?? Name)).ToLower();
if (/* SOME QUERYING */)
{
return QUERY[key];
}
else
{
string property = Resources.m_Res.ResourceManager.GetString(Name);
if (property != null)
{
return property;
}
else
{
return Name;
}

}
}
}
}

这种工作,我想暂时还可以,但下一个问题指日可待:我需要对 Display.GroupName 做同样的事情。

现在,据我所知,没有 GroupNameAttribute 可以扩展,所以我在这里有点摸不着头脑。

我希望我可以扩展 DisplayAttribute,这正是我需要的,但是这个类是密封的,所以这是一个死胡同。

我希望我可以即时更改模型并通过 setter 提供 DisplayName 和 Description,但是该模型只有 getter,所以这是另一个死胡同。

我在这里慢慢没有选择了。这里还能做什么?

最佳答案

虽然 DisplayAttribute类是密封的,可以通过ResourceType定制属性(property):

Gets or sets the type that contains the resources for the ShortName, Name, Prompt, and Description properties.



有两件事要提。首先,它还处理 GroupName .第二个在 中提到备注 部分:

If this value is not null, the string properties are assumed to be the names of public static properties that return the actual string value.



很快,提供的类型可以是任何 公众 具有 的类/结构公共(public)静态字符串 每个字符串键的属性。

这完全符合 PublicResXFileCodeGenerator生成类型化的资源类。但重要的是它可以是任意类型,您可以利用这一事实。

例如:
public static class SR
{
// Assuming you have file SR.resx under same namespace
static readonly ResourceManager ResourceManager = new ResourceManager(typeof(SR));

static string GetString([CallerMemberName]string propertyName = null)
{
// Custom logic goes here
return ResourceManager.GetString(propertyName) ?? propertyName;
}

// Properties
public static string M_RES_MIMETYPE_ID => GetString();
public static string M_RES_MIMETYPE_ID_DESCR => GetString();
public static string M_RES_MIMETYPE_ID_GROUP => GetString();
// ...
}

用法:
[Display(
ResourceType = typeof(SR),
Name = nameof(SR.M_RES_MIMETYPE_ID),
Description = nameof(SR.M_RES_MIMETYPE_ID_DESCR),
GroupName = nameof(SR.M_RES_MIMETYPE_ID_GROUP)
)]

测试:
var displayInfo = new DisplayAttribute
{
ResourceType = typeof(SR),
Name = nameof(SR.M_RES_MIMETYPE_ID),
Description = nameof(SR.M_RES_MIMETYPE_ID_DESCR),
GroupName = nameof(SR.M_RES_MIMETYPE_ID_GROUP)
};
// These are used by ASP.NET for retrieving actual values
var name = displayInfo.GetName();
var description = displayInfo.GetDescription();
var groupName = displayInfo.GetGroupName();

所以,这就是解决方案。

现在,唯一的问题是需要手动添加 string每个资源键的属性。它可以通过推出您自己的单个文件生成器或 T4 模板生成器来解决,但我认为如何做到这一点超出了本文的范围。

关于C# 数据注释,不能扩展特定属性 - 有哪些可能性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61479072/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com