gpt4 book ai didi

c# - 自定义 DisplayFormatAttribute 只设置一次

转载 作者:IT王子 更新时间:2023-10-29 04:31:37 26 4
gpt4 key购买 nike

我通过以下代码在资源的 DisplayFormat 中设置 NullDisplayText

public class LocalizedDisplayFormatAttribute : DisplayFormatAttribute
{

private readonly PropertyInfo _propertyInfo;


public LocalizedDisplayFormatAttribute(string resourceKey, Type resourceType)
: base()
{
this._propertyInfo = resourceType.GetProperty(resourceKey, BindingFlags.Static | BindingFlags.Public);
if (this._propertyInfo == null)
{
return;
}

base.NullDisplayText = (string)this._propertyInfo.GetValue(this._propertyInfo.DeclaringType, null);
}


public new string NullDisplayText
{
get
{
return base.NullDisplayText;
}

set
{
base.NullDisplayText = value;
}
}
}

我使用的默认文化是“en-US”,一旦我将文化更改为 es-AR 并加载页面,它就可以正常工作,但是当我将文化改回 en-US 时,字段不会转换回来。

我通过以下方式改变文化

protected void Application_AcquireRequestState(object sender, EventArgs e)
{
try
{
HttpCookie cookie = HttpContext.Current.Request.Cookies.Get("CurrentCulture");
string culutureCode = cookie != null && !string.IsNullOrEmpty(cookie.Value) ? cookie.Value : "en";
CultureInfo ci = new CultureInfo(culutureCode);
System.Threading.Thread.CurrentThread.CurrentUICulture = ci;
System.Threading.Thread.CurrentThread.CurrentCulture =
CultureInfo.CreateSpecificCulture(ci.Name);
}
catch
{
}
}

我在 ViewModel 中使用 DisplayFormat 属性作为

  public class AlarmCodeDetailsViewModel
{
/// <summary>
/// Gets or sets the alarm code ID
/// </summary>
public int AlarmCodeID { get; set; }

/// <summary>
/// Gets or sets the alarm code
/// </summary>
[LocalizedDisplayName("Label_AlarmCode")]
[LocalizedDisplayFormatAttribute("Warning_NullDisplayText", typeof(Properties.Resources), HtmlEncode = false)]
public string Code { get; set; }

/// <summary>
/// Gets or sets the Description
/// </summary>
[LocalizedDisplayName("Label_Description")]
[LocalizedDisplayFormatAttribute("Warning_NullDisplayText", typeof(Properties.Resources), HtmlEncode = false)]
public string Description { get; set; }

/// <summary>
/// Gets or sets the Notes
/// </summary>
[LocalizedDisplayName("Label_Notes")]
[LocalizedDisplayFormatAttribute("Warning_NullDisplayText", typeof(Properties.Resources), HtmlEncode = false)]
public string Notes { get; set; }
}

最佳答案

这里有一些问题的见解。

Mvc 使用 TypeDescriptor 的一种形式(AssociatedMetadataTypeTypeDescriptionProvider(type).GetTypeDescriptor(type))来读取模型的属性。 TypeDescriptors 缓存有关特性和特性的信息。因此,就 TypeDescriptor api 而言,您的 LocalizedDisplayFormatAttribute 属性仅被实例化一次,这意味着资源信息仅被读取一次(在构造时)。请参阅答案底部以获取引用。

不起作用的解决方案

  1. 眨眼 react 是每次通过 getter 访问时,只从 LocalizedDisplayFormatAttribute NullDisplayText 中提取最新的资源信息。不幸的是 DisplayFormatAttribute NullDisplayText 不是虚拟的,您正在使用 new 关键字隐藏该属性。这从多态调度的角度来看是行不通的(Mvc 将 getter 作为 DisplayFormatAttribute 而不是 LocalizedDisplayFormatAttribute 调用,因此您的阴影属性永远不会被调用)

  2. 我尝试了 TypeDescriptor.Refresh() 重载 https://msdn.microsoft.com/en-us/library/z1ztz056(v=vs.110).aspx运气不好

我所知道的其余选项在某种程度上并不那么方便或不令人惊奇。可能不推荐。

  1. 一些成功刷新 AssociatedMetadataTypeTypeDescriptionProvider TypeDescriptors 的方法。我对这些不太熟悉,所以完全可以有一个。我只是目前没有看到。
  2. 修改或创建您自己的 ModelMetadataProvider。一切都是开源的,所以这是可能的,但我不确定我是否会推荐它,除非万不得已。
  3. 您可以使用 TypeDescriptor api 来强制重新实例化您的属性,无论何时它被拉出。参见 https://stackoverflow.com/a/12143653/897291 .
  4. 直接在 MVC 中为所需的属性建模(作为模型属性,而不是属性)。可以是全新的属性,或者您可以在原始属性中具有某种逻辑,当 null 返回其他内容时。虽然处理起来很尴尬。

没什么了不起的,我知道。也许这会给其他人足够的洞察力来想出更好的东西?

要自己验证这一点,请参阅 https://aspnetwebstack.codeplex.com/SourceControl/latest#src/System.Web.Mvc/DataAnnotationsModelMetadataProvider.cs CreateMetaData方法调用SetFromDataTypeAndDisplayAttributes方法设置result.NullDisplayText = displayFormatAttribute.NullDisplayText;

DataAnnotationsModelMetadataProvider 扩展了 AssociatedMetadataProvider,后者负责传递属性。参见 https://aspnetwebstack.codeplex.com/SourceControl/latest#src/System.Web.Mvc/AssociatedMetadataProvider.cs GetMetadataForProperty 方法为例。

关于c# - 自定义 DisplayFormatAttribute 只设置一次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30326257/

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