This doesn't seem to work when Program.Title is not a string, a Guid for instance.
–
user2320724Nov 14 '13 at 3:29

I like this answer, but I would use two generic types so that you aren't constrained to one type as in the problem mentioned by @user2320724. Signature might look like public static string DbField<T1, T2>(this T obj, Expression<Func<T1, T2>> value)
–
Adam VeneziaJan 20 '14 at 22:36

Looks good...the only thing I would look for next is to somehow make this statement dynamic so it can be used by many different properties of that object: GetProperty("Title")
–
kabuceyFeb 24 '10 at 22:12

It is, but ultimately it's going to be a roundabout way, since you will get the Type instance from calling GetType on your instance that exposes the property, and then work on that(more often than not).

In this specific case, your extension method isn't going to be able to get the attribute information because all you are passing to it is a string.

Ultimately, what you need is something to get the PropertyInfo for the property from. Other answers are referring to the Type, what they lack is, this is not the only way to get the attribute information at the PropertyInfo which you want.

You can do that by passing a Type instance with a string, presumably, with the property name, so you can call GetProperty on the Type.

Another way of doing this since C# 3.0 has been to have a method that takes an Expression<T> and then use the parts of the Expression to get at the PropertyInfo. In this case, you would take an Expression<Func<string>> or something where TResult is string.

Once you have the PropertyInfo, you can call GetCustomAttributes on it, and look for your attribute.

The advantage to the expression approach is that Expression<T> derives from LambdaExpression, which you can call Compile on, and then call to get the actual value, if you need it.

No, it's not possible. The reason for this is that it's the value, and not the property itself that would be sent into any custom extension method that would fetch this information. Once you get into that extension method, there's no reliable way to trace back to the property itself.

It might be possible for enum values, but as far as properties on POCO's, it's not going to work.

In order to get the attribute value, you need the type that the attribute applies to. Your extension method is only getting a string value (the value of Title), so you would not be able to get the actual instance that the string came from, and so you can't get the original type that the Title property belongs to. This will make it impossible to get the attribute value from your extension method.

@NerdFury: Completely wrong, since the attribute is on the property, you need the PropertyInfo, not the Type. Also, using the Type is not the only way to get the PropertyInfo, there are other ways since .NET 3.5/C# 3.0 to do it.
–
casperOne♦Feb 24 '10 at 21:48

fair enough, I should have been more careful with my words. So yes, he needs the PropertyInfo to check for custom attributes, but he still can't get that from an extension method on object and call it on a string. There is no way to get the code written above to give the attribute value. I agree with you, that through Expression Trees, he could do it, but the code would not look like what he wanted his API to look like.
–
NerdFuryFeb 24 '10 at 21:56