Let's say I have a method, which prints the names and values of some properties of an object:
public void PrintProperties(object o, params string[] propertyNames)
{
foreach (var propertyName in propertyNames)
{
// get the property info,
// then get the property's value,
// print property-name and -value
}
}
// method can be used like this
PrintProperties(user, "FirstName", "LastName", "Email");
Now instead of having to pass a list of strings to the method, I'd like to change that method so that the properties can be specified using lambda expressions (not sure if that's the correct term).
E.g. I'd like to be able to call my method (somehow) like this:
PrintProperties(user, u->u.FirstName, u->u.LastName, u->u.Email);
The goal is, to give the user of the method intellisense support, to prevent typing errors.
(Similar to the ASP.NET MVC helper methods, like TextBoxFor(u=>u.Name)
).
How do I have to define my method, and how can I then get the PropertyInfo
s inside the method?