You can pass a property as an Expression(Of Func(Of MyModel, String))
and by that receive an expression in the method that you can analyze and evaluate:
Public Shared Function DisplayFor(Of TModel, TValue)(model As TModel, expr As Expression(Of Func(Of TModel, TValue))) As String
' Retrieve the value dynamically
Dim compExpr = expr.Compile()
Dim value = compExpr.DynamicInvoke(model)
Dim retVal As String
If value Is Nothing Then
retVal = String.Empty
Else
retVal = value.ToString()
End If
' Analyze expression body
Dim memberAccExpr = DirectCast(expr.Body,
System.Linq.Expressions.MemberAccessExpression)
Dim attr = memberAccExpr.Member.GetCustomAttributes(typeof(MyDisplayAttribute), false).Cast(Of MyDisplayAttribute)().FirstOrDefault();
Return retVal
End Function
Call the method like this:
DisplayFor(myModelVar, Function(m) m.MyProperty)
I hope this sample gives you a rough outline on how to handle this. Please note that especially the analysis of the expression body is simplified. In real world code there would be various checks to make sure that the expression matches your expectations.