0

We are using EF4 database first approach to create all the entities as found in the context class. I'm now trying to add a display name attribute to one of the objects' properties as follows:

[MetadataType(typeof(OpportunityMetaData))]
public partial class Opportunity : EntityObject
{

}

public class OpportunityMetaData
{
    [Display(Name = "Worked By")]
    public int WorkedById { get; set; }
}

Then on a test page, using reflection, I'm trying to get an output that says "Worked By", as follows:

var attrType = typeof(DisplayNameAttribute);
var property = typeof(Opportunity).GetProperty("WorkedById");
Response.Write(((DisplayNameAttribute)property.GetCustomAttributes(attrType, false).FirstOrDefault()).DisplayName);

But this just gives Object Reference not set to an instance of an object. Alternatively, if I just Response.Write the property, it writes out "WorkedById" and not "Worked By".

Any help would be appreciated.

James
  • 15
  • 6

1 Answers1

0

Its DisplayAttribute, not DisplayNameAttribute. Name is just a property on it.

Malcolm O'Hare
  • 4,879
  • 3
  • 33
  • 53
  • Got it thanks. Turns out we actually needed to first use MetadataTypeAttribute as `typeof(Opportunity).GetCustomAttributes(typeof(MetadataTypeAttribute), true))[0]` then get the resulting MetadataTypeAttributes property and get it's `DisplayAttribute` as you correctly point out. – James Dec 10 '12 at 20:37