如何在 c# 中获取属性的属性

本文关键字:属性 获取 | 更新日期: 2023-09-27 18:36:39

直奔主题,

如何获取属性的属性,以及此属性的类型或值?例如这样的属性:

[ForeignKey(typeof(SomeObject))]

我想知道或获取其类型"SomeObject"。我知道我可以得到属性()等,但属性我没有Ideia。

这是我的疑问,它吓坏了我。 提前感谢您的帮助!

如何在 c# 中获取属性的属性

一旦你得到了你感兴趣的房产的PropertyInfo,你只需打电话给GetCustomAttributes

ForeignKey[] keys = (ForeignKey[]) 
    property.GetCustomAttributes(typeof(ForeignKeyAttribute), false);

还有 CustomAttributes 属性,但这仅在 .NET 4.5 和 Windows 应用商店应用中可用。