I have a partial class generated from a web service. I add ModelMetadata
to this class like so:
// Automatically generated code
public partial class MyClass
{
public string FIELD_ONE ...
public DateTime? FIELD_TWO ...
}
// My code
[MetadataType(typeof(MyMetaDataClass))]
public partial class MyClass
{
}
public class MyMetaDataClass
{
[Display(Name="Field One")]
public string FIELD_ONE;
[Display(Name="Field Two")]
public DateTime? FIELD_TWO;
}
Now I would like to get all properties of MyClass
which have metadata Display Attributes, ie, FIELD_ONE
and FIELD_TWO
.
I've tried things like
typeof(MyClass).GetProperties().Where(p => Attribute.IsDefined(typeof(DisplayAttribute))
But of course the attribute is not on the properties of MyClass
. I've also tried:
ModelMetadata mmd = ModelMetadata.FromLambdaExpression(m => pi.GetValue(m, null), Html.ViewData);
But I get the error "Templates can be used only with field access, property access, single-dimension array index, or single-parameter custom indexer expressions."
If I could get from MyClass
to MyMetadataClass
I might then be able to get the property names of that and get the properties of the same name from MyClass
.
Is this possible, or am I barking up the wrong tree?