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

问题描述 投票:1回答:1

我正在使用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,这正是我所需要的,但是类是密封的,所以这是一个死胡同。

我希望我可以随时更改模型,并通过设置器提供DisplayName和Description,但是该模型只有吸气剂,所以这是另一个死胡同。

我在这里用尽所有选项。还有什么可以做的?

c# asp.net custom-attributes asp.net-dynamic-data
1个回答
0
投票

如果通过扩展方法扩展DisplayAttribute,诸如GetGroupName()之类的方法不能解决您的问题?

© www.soinside.com 2019 - 2024. All rights reserved.