none
DescriptionAttribute metatag added to attributes RRS feed

  • Question

  • How do you add a DescriptionAttribute to a property that is automaticly generated by Linq designer?
    Ive tried the following ('Name' is the name of the property - sorry about any confusion):

                TypeDescriptor.AddAttributes(Name, new DescriptionAttribute("Test of descriptionattribute on Name"));

                AttributeCollection attributes = TypeDescriptor.GetProperties(this)["Name"].Attributes;

                /* Prints the description by retrieving the DescriptionAttribute
                 * from the AttributeCollection. */
                DescriptionAttribute myAttribute = (DescriptionAttribute)attributes[typeof(DescriptionAttribute)];
                System.Console.WriteLine(myAttribute.Description);

    I want to add a description of what kind of information my property is holding (more saying then the actual nameing, obviously).

    This obviously isn't the correct way of doing it, but the msdn documentation only gives an example of how I can read the DescriptionAttribute. Anyone who knows how I can do this without falling back on building the database in sql instead?
    Thank you!
    Tuesday, August 18, 2009 10:44 AM

Answers

  • The attributes are for the most part read only. Unfortunately the DBML does not give the ability to add additional attributes on generated class properties. It was supported in the early betas but was removed by RTM.

    This is the main reason why Dynamic Data, Ria Services, MVC 2, etc. all use the buddy class with the MetadataType attribute to add attributes to the generated properties. The attributes couldn't be added in these cases at runtime because they are consumed at compile time rather than at runtime. You may want to look at their implementation as a prototype for your needs.

    Personally, I find the buddy class implementation a hack around limitations in the designers, but we're stuck with it at the moment. Alternatively, you could add extensions to the DBML my manually changing the XML adding your own namespace extensions and doing the code generation yourself, but I suspect that is more work than you are wanting to do.

    Jim
    http://www.LinqInAction.net - "LINQ In Action", The book is now available. Don't wait for the movie
    • Marked as answer by Yichun_Feng Tuesday, August 25, 2009 1:22 AM
    Thursday, August 20, 2009 9:21 PM
    Moderator