locked
TypeDescriptor.AddProvider doesnt work for object instance RRS feed

  • Question

  • Hi 



    I am trying to write a CustomTypeDescriptionProvider for a Model in my MVC application.



    I have done the implementation as described here http://msdn.microsoft.com/en-us/magazine/cc163804[dot]aspx

    the GetProperties method gets called when i register the CustomTypeDescriptor using 

     [TypeDescriptionProvider(typeof(TitleTypeDescriptionProvider))]
        public class CustomDataRecursive2 : CustomTypeDescripto



    or 

    TypeDescriptor.AddProvider(t, typeof(CustomData));

    however my requirement is to have a type descriptor for each instance.

    When i use the below method below it doesnt work...

     TypeDescriptor.AddProvider(new CustomData.TitleTypeDescriptionProvider(), vacancy.CustomData);



    The TitleTypeDescriptionProvider.GetTypeDescriptor is never called.

    Am I missing something ? 

    • Edited by pat1rr1 Monday, November 4, 2013 3:08 PM typo
    Monday, November 4, 2013 2:45 PM

Answers

  • Hi Pnvpratik,

    I’m sorry to misunderstand you.

    The AddProvider(TypeDescriptionProvider, Object) method adds a type description provider that supplies type information for a single instance of a component. If a provider is added by this method, the provider's CreateInstance method will not be called because the instance already exists.—MSDN says that.

    You could refer to this page to see more information. http://msdn.microsoft.com/en-us/library/d1seatc8(v=vs.110).aspx.

    TypeDescriptor class was the implementation of Decorator Pattern. If the implement of Microsoft doesn’t match your requirement, I think you should write your own Decorator Pattern. http://www.codeproject.com/Articles/479635/UnderstandingplusandplusImplementingplusDecoratorp. The page shows the implementing of Decorator Pattern.

    Best Regards,


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Thursday, November 7, 2013 4:53 AM

All replies

  • Hi Pnvpratik,

    I write a sample code for you, please refer to this page to see the detail code. http://sdrv.ms/17Jhaae.

    I hope to give you inspiration.

    Best Regards,


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Tuesday, November 5, 2013 5:52 AM
  • Hi Hetro 

    Thanks for the reply ,  the sample you have shown is adding class as a data source to the propertygrid.

    however i am trying to use the class in a MVC 4 model.  

    From what i understand in MVC if you want to ensure that the GetProperties() gets invoked when the data is getting binded , we have to attach a custom TypeDescriptionProvider to the list of type description provider. 

    My problem is that when i add the type description provider using the TypeDescriptionProvider attribute or by using 

     [TypeDescriptionProvider(typeof(TitleTypeDescriptionProvider))]

    or

    TypeDescriptor.AddProvider(t, typeof(CustomData)); 

    it works properly..

    but it doesnt work if I  try to associate a type descriptor to a instance by using a different overload of TypeDescriptor.AddProvider as shown below:

      TypeDescriptor.AddProvider(new CustomData.TitleTypeDescriptionProvider(), vacancy.CustomData);

    * Note how the proivder is only being added to the single instance of the object.

    I would like to be able to attach a type descriptor to  a instance of a object.  How can I achieve that ?


    Tuesday, November 5, 2013 1:36 PM
  • Hi Pnvpratik,

    I’m sorry to misunderstand you.

    The AddProvider(TypeDescriptionProvider, Object) method adds a type description provider that supplies type information for a single instance of a component. If a provider is added by this method, the provider's CreateInstance method will not be called because the instance already exists.—MSDN says that.

    You could refer to this page to see more information. http://msdn.microsoft.com/en-us/library/d1seatc8(v=vs.110).aspx.

    TypeDescriptor class was the implementation of Decorator Pattern. If the implement of Microsoft doesn’t match your requirement, I think you should write your own Decorator Pattern. http://www.codeproject.com/Articles/479635/UnderstandingplusandplusImplementingplusDecoratorp. The page shows the implementing of Decorator Pattern.

    Best Regards,


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Thursday, November 7, 2013 4:53 AM
  • Hi 

    This is where I am confused , what does the statement below mean ?

    "the provider's CreateInstance method will not be called because the instance already exists."


    Does it mean that since create  instance is not called , my custom TypeDescription provider will not be called when MVC is  trying to get the properties inside the  class ?

    Also are you suggesting that i implement the  decorator pattern on  the TypeDescriptor class ?

    Could you please elaborate  ?

     


    • Edited by pat1rr1 Wednesday, November 13, 2013 2:23 PM elaborate my question
    Wednesday, November 13, 2013 2:22 PM
  • hi,
    >This is where I am confused , what does the statement below mean ?
    Per my understanding,if the instance is exist, you could not let the provider's createinstance method been called.The two versions of AddProvider method produce results with different scopes. The AddProvider method that takes an Object parameter affects only that single instance of the component specified. In contrast, the other overload, which

    takes a Type parameter, affects all instances of the component described by that type.May be you should use the other overload method.
    >Also are you suggesting that i implement the  decorator pattern on  the TypeDescriptor class ?
    If Microsoft's implementation does not meet your requirements, I think you can consider yourself to write decorated patterns.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Thursday, November 14, 2013 1:37 AM
  • It's been a long while since you wrote this... Have you ever found a solution?

    I'm stuck with the same problem in .NET 4.5 and DataGridView in WinForm


    BKQc

    Tuesday, August 16, 2016 7:47 PM