How to get an attribute of a property in c#?

Going direct to the point,

How can I get Property's attribute, and, the type or the value of this attribute? For example an attribute like this:

[ForeignKey(typeof(SomeObject))]

I wanna know or get its type "SomeObject". I know I can get Properties() and etc, but attributes I have no Ideia.

That is a doubt I have and it is freaking me off. Thanks in advance for helpin!

Jon Skeet
people
quotationmark

Once you've got the PropertyInfo for the property you're interested in, you just call GetCustomAttributes on it:

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

There's also the CustomAttributes property, but that's only available in .NET 4.5 and Windows Store apps.

people

See more on this question at Stackoverflow