在提供的模型(類)中找到哪個屬性具有KeyAttribute
的最佳方法是什麼?MVC3:獲取KeyAttribute
我試過DataAnnotationsModelMetadataProvider
;但是ModelMetadata
不包含有關KeyAttribute
的信息。
在提供的模型(類)中找到哪個屬性具有KeyAttribute
的最佳方法是什麼?MVC3:獲取KeyAttribute
我試過DataAnnotationsModelMetadataProvider
;但是ModelMetadata
不包含有關KeyAttribute
的信息。
對於名爲MyModel
的模型類,您可以找到具有以下關鍵屬性的屬性。
var keyAttributedProps = typeof(MyModel).GetProperties()
.Where(p => p.GetCustomAttributes(typeof(KeyAttribute), true).Length == 1);
從來沒有嘗試過,但我認爲你可以使用TypeDescriptor.GetProperties方法來獲得dessired屬性。根據MSDN,它使用指定的屬性數組作爲過濾器返回指定組件的屬性集合。
選中此鏈接。我希望它有幫助: http://msdn.microsoft.com/en-us/library/3x9x2kh5.aspx#Y0