none
Getting type information for the PropertyGrid from partial classes RRS feed

  • Question

  • Hi,

    Using VS2010, VB.Net, Entity Framework 5, Winforms...

    In order to get type information from an entity which is split among multiple files, I've read much about using the TypeDescriptionProvider attribute, creating a TypeDescriptionProvider class along with an accompanying class which inherits CustomTypeDescriptor. Then I read other articles and posts in which an implementation of ICustomTypeDescriptor is used. I'm confused. Which objects are truly required and which are not? Why is one method used over some other? Can someone please lay this out in a simple, step by step approach?

    I am generating DbContext classes from a template. The template decorates the classes with DataAnnotations attributes. Now I need attributes related to type information. Since one cannot do partial properties, I need some method of looking up this type metadata for PropertyGrids, etc. The MetaDataType attribute (along with associated metadata classes) doesn't appear to be an option because the PropertyGrid can't communicate with the metadata class.

    Therefore, I need to implement some other solution. TypeDescriptors seems to be the way to go but, again, I'm confused. I can't find a resource which states the obvious. None of them describe the forest; they describe the trees.

    Simply put, I need the ability to supply metadata for the properties of my entities such that they're visible to the PropertyGrid without hand-editing my generated entities.

    Thanks for any help.

    Wednesday, March 6, 2013 6:17 PM

Answers

  • How about something like this?

    Imports System.ComponentModel.DataAnnotations
    <MetadataType(GetType(Equipment.EquipmentMetadata))>
    Public Class Equipment
        Public Sub New()
            TypeDescriptor.AddProvider(New AssociatedMetadataTypeTypeDescriptionProvider(GetType(Equipment)), GetType(Equipment))
        End Sub
        Protected Friend Structure EquipmentMetadata
            <DisplayName("Equipment Number")>
            <Description("The number which uniquely identifies the asset.")>
            Public Property EquipmentNumber As String
        End Structure
    End Class

    The above class is a partial of class Equipment generated by the DbContext generator.

    Does anyone work with this stuff? I'd very much like to get your feedback, if so.

    • Marked as answer by KorLibrary Tuesday, March 12, 2013 2:39 PM
    Wednesday, March 6, 2013 9:12 PM

All replies

  • How about something like this?

    Imports System.ComponentModel.DataAnnotations
    <MetadataType(GetType(Equipment.EquipmentMetadata))>
    Public Class Equipment
        Public Sub New()
            TypeDescriptor.AddProvider(New AssociatedMetadataTypeTypeDescriptionProvider(GetType(Equipment)), GetType(Equipment))
        End Sub
        Protected Friend Structure EquipmentMetadata
            <DisplayName("Equipment Number")>
            <Description("The number which uniquely identifies the asset.")>
            Public Property EquipmentNumber As String
        End Structure
    End Class

    The above class is a partial of class Equipment generated by the DbContext generator.

    Does anyone work with this stuff? I'd very much like to get your feedback, if so.

    • Marked as answer by KorLibrary Tuesday, March 12, 2013 2:39 PM
    Wednesday, March 6, 2013 9:12 PM
  • Hi Kor,

    Welcome to the MSDN Forum.

    Based on your description, it seems that you are trying to firgure out the difference between TypeDescriptionProvider and TypeDescriptor, because they all can provide the decriptions.

    To simple, the description of the Type TypeDescriptionProvider on MSDN said: http://msdn.microsoft.com/en-us/library/system.componentmodel.typedescriptionprovider.aspx

    Provides supplemental metadata to the TypeDescriptor.

    For more, try here: http://stackoverflow.com/questions/749542/icustomtypedescriptor-typedescriptionprovider-typeconverter-and-uitypeeditor  

    ICustomTypeDescriptor

    • Implementing this interface in a class totaly overrides the native properties of a class and replaces them with the PropertyDescriptors returned by ICustomTypeDescriptor.GetProperties()

    TypeDescriptionProvider

    • Used to extend on the existing properties of a class
    • A TypeDescriptionProvider is appended to a class through a TypeDescriptionProvider attribute
    • The GetTypeDescriptor() method of the TypeDescriptionProvider returns an ICustomTypeDescriptor to be appended to the existing properties of the type.
    • A PropertyGrid will show both the properties of the class found through Reflection, and the properties added to the class through the TypeDescriptionProvider
    • can also be added be TypeDescriptor.AddProvider
    • can be applied per-type as well as per-instance, making it possible to apply to lists without having to implement ITypedList

    I hope this will be helpful.

    Best regards,


    Mike Feng
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Thursday, March 7, 2013 9:22 AM
    Moderator
  • No, I don't think I'm trying to figure out the difference between TypeDescriptionProvider and TypeDescriptor.

    I'm trying to figure out, as described in my original post, to extend a classes metadata across multiple files.

    You've offered me more trees.

    Tuesday, March 12, 2013 2:39 PM