加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

entity-framework – 如何找到UIHInt属性的target属性?

发布时间:2020-12-16 03:45:03 所属栏目:asp.Net 来源:网络整理
导读:我有以下UIHInt基于attibute: [AttributeUsage(AttributeTargets.Property)]public class DropDownListAttribute : UIHintAttribute,IMetadataAware{ public DropDownListAttribute(string selectListName) : base(KnownUiHints.DropDown,KnownPresentation
我有以下UIHInt基于attibute:

[AttributeUsage(AttributeTargets.Property)]
public class DropDownListAttribute : UIHintAttribute,IMetadataAware
{
    public DropDownListAttribute(string selectListName)
        : base(KnownUiHints.DropDown,KnownPresentationLayers.Mvc,selectListName)
    {
        SelectListName = selectListName;
    }

    public string SelectListName { get; set; }

    public void OnMetadataCreated(ModelMetadata metadata)
    {
        metadata.AdditionalValues[KnowMetadataKeys.SelectListName] = SelectListName;
    }
}

它的目的是将SelectList分配给要从列表中选择的单值视图模型属性,如下所示:

public class DemoModel: ViewModel
{
    [Required]
    [DropDownList("LanguageSelect")]
    [Display(Name = "Language")]
    public int? LanguageId { get; set; }

    public SelectList LanguageSelect { get; set; }
}

我现在正在使用一些非常Golbergian机器和我自己的元数据提供程序,但是发现了IMetadataAware.OnMetadataCreated,我觉得我可以简化这个.现在我将SelectListName添加到元数据中,然后跳过一些箍到a)将SelectList转换为一种全局字典,以及b)在呈现下拉列表时从该字典中获取选择列表.

我想将SelectList本身添加到属性中的模型元数据,即属性适用的属性的本地元数据,但是如何访问该属性或它包含类型?

解决方法

用于访问Pocos上的属性的示例代码.
有一个或多个属性版本可供查看

示例调用方法

var MutliAttributeList = MyStatic.GetPocoMultiAttribute<MyAttribute>(typeof(poco),"AttrName");


 public static UAttribute GetPocoAttribute<TPoco,UAttribute>(string attributeName) {
        var result = typeof (TPoco).GetCustomAttributes(true)
                                   .Where(attribute => attribute.GetType()
                                                                .Name == attributeName)
                                   .Cast<UAttribute>()
                                   .FirstOrDefault();
        return result;
    }

public static IEnumerable<UAttribute> GetPocoMultiAttribute<UAttribute>(Type pocoType,string attributeName) {
        var result = pocoType.GetCustomAttributes(true)
                             .Where(attribute => attribute.GetType()
                                                          .Name == attributeName).Cast<UAttribute>().ToList();
        return result;
    }

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读