如何找到UIHInt属性的target属性?

Pro*_*ofK 6 entity-framework data-annotations entity-framework-5 asp.net-mvc-uihint

我有以下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;
    }
}
Run Code Online (Sandbox Code Playgroud)

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

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

    public SelectList LanguageSelect { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

我现在正在使用一些很棒的Golbergian机器和我自己的元数据提供商,但是IMetadataAware.OnMetadataCreated我发现,我觉得我可以简化这个.现在我添加SelectListName到元数据,然后跳过一些箍到a)将SelectList变成一种全局字典,b)在渲染下拉列表时从该字典中获取选择列表.

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

phi*_*ady 0

访问 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;
    }
Run Code Online (Sandbox Code Playgroud)