2011-12-12 23 views
0

在提供的模型(類)中找到哪個屬性具有KeyAttribute的最佳方法是什麼?MVC3:獲取KeyAttribute

我試過DataAnnotationsModelMetadataProvider;但是ModelMetadata不包含有關KeyAttribute的信息。

回答

0

對於名爲MyModel的模型類,您可以找到具有以下關鍵屬性的屬性。

var keyAttributedProps = typeof(MyModel).GetProperties() 
    .Where(p => p.GetCustomAttributes(typeof(KeyAttribute), true).Length == 1);