MVC3:获取KeyAttribute
在提供的模型(类)中查找哪个属性具有 KeyAttribute
的最佳方法是什么?
我尝试使用DataAnnotationsModelMetadataProvider
;但 ModelMetadata
不包含有关 KeyAttribute
的信息。
What is the best way to find which property has a KeyAttribute
in provided Model (class)?
I tried with DataAnnotationsModelMetadataProvider
; but ModelMetadata
does not contain information about KeyAttribute
.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
对于名为
MyModel
的模型类,您可以找到具有关键属性的属性,如下所示。For a model class named
MyModel
, you can find the properties with key attributes as follows.从未尝试过,但我认为您可以使用 TypeDescriptor.GetProperties 方法来获取所需的属性。根据 MSDN,它使用指定的属性数组作为过滤器返回指定组件的属性集合。
检查此链接。我希望它有帮助:
http://msdn.microsoft.com/en-us/library/3x9x2kh5 .aspx#Y0
Never tried, but i think you could use TypeDescriptor.GetProperties Method to get the dessired property. According with MSDN, it returns the collection of properties for a specified component using a specified array of attributes as a filter.
Check this link. I hope it helps:
http://msdn.microsoft.com/en-us/library/3x9x2kh5.aspx#Y0