How to find the target property of the UIHInt attribute?

I have the following UIHInt based approach:

[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; } } 

The goal is to assign a SelectList to a model property with one value to be selected from the list, for example:

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

I now have work with some very Golberg equipment and my own metadata provider, but when I find IMetadataAware.OnMetadataCreated , I feel that I can simplify this. Right now, I am adding SelectListName to the metadata, and then jumping through a few hoops in a) Get the SelectList into a kind of global dictionary and b) select the selection list from this dictionary when rendering the drop-down list.

I would like to add SelectList itself to the model metadata in the attribute, that is, the metadata local to the property to which the attribute applies, but how can I access this property or its containing type?

+6
source share
1 answer

Sample code for accessing Pocos attributes. There is a version with one or more attributes to view

method call example

 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; } 
0
source

All Articles