Assuming you have a view model with some metadata:
public class MyViewModel
{
[DisplayName("Bar")]
public string Foo { get; set; }
}
you could retrieve this metadata like this:
ModelMetadata metadata = ModelMetadata.FromLambdaExpression<MyViewModel, string>(
x => x.Foo,
new ViewDataDictionary<MyViewModel>()
);
Assert.AreEqual("Bar", metadata.DisplayName);
UPDATE:
As requested in the comments section here's how to obtain the metadata if only the type is known at runtime:
var type = typeof(MyViewModel);
var metadata = ModelMetadataProviders.Current.GetMetadataForType(null, type);
and if you want to get the metadata for a child property just specify the name of the property:
var type = typeof(MyViewModel);
var metadata = ModelMetadataProviders.Current.GetMetadataForProperty(null, type, "Foo");