Windows Develop Bookmark and Share   
 index > Windows Forms Designer > delegate property in property designer?
 

delegate property in property designer?

I have a custom control that exposes a delegate property and I want to be able to edit the property through the property browser. Almost everything works, I have created a custom TypeConverter with 2 custom properties (object and method name) and when I change them, the property changes appropriately (in memory). However, when the designer tries to generate the designer code for the property, it fails with the following error:

Code generation for property XYZ failed. Error was: Type xyz in Assembly 'abc' is not marked as serializable.

The designer simply tries to serialize the property since delegates implement ISerializable and it doesn't even call the "ConvertTo" method of the TypeConverter with an InstanceDescriptor target type.

I guess one solution would be to serialize the component, but it's a type derived from a ComboBox and I really don't want to be deserializing at run time. (when I tried serializing the component, the property designer did something like this: obj.DelegateProperty = resources.GetObject... which reads the serialized object from the resource file).

Does anyone have any ideas how I can fix this? I know it's possible since the property browser can handle events properly, but at this point I'm out of ideas. Any suggestions will be really appreciated.
Rado R  Tuesday, July 10, 2007 9:19 PM
Ok, I kind of solved my problem, but with a workaround. Now I don't try to generate the delegate property directly, but I use a delegate as a sub-property of another property ... which is sort of what I needed in the first place anyway.

Now, you'd have the same problem if you wanted to generate the code from a TypeConverter for the property because you'll have to generate the delegate somehow. So instead, I do the code generation in a CodeDomSerializer by adding a DesignerSerializer attribute to the class containing the delegate. With CodeDomSerializer, you can generate all sorts of code your heart desires so the rest is easy.

However, if anyone knows how to change the Serializer of a delegate, please tell me.

Cheers!
Rado R  Thursday, July 12, 2007 2:29 PM

Rado,

PropertyGrid handles events via adifferent PropertyTab, so Isuspect the code generation in the EventsPropertyTab is quite different from the "normal" Properties Tab.

I think I'dtry usinga MulticastDelegate property, rather thanthedelegate you've defined.

RightPaddock  Wednesday, July 11, 2007 5:11 AM
Thanks for the reply.

I am actually using a MultiCast delegate. The delegate type is declared as:
public delegate string xyz();
which creates a multicast delegate.
However, the property browser doesn't handle delegates so I had to define my own TypeConverter to be able to change the delegate through the property browser.

Does anyone know how I can prevent the property browser to use delegate's serialization and call the ConvertTo method with a InstanceDescriptor return type instead?


Rado R  Wednesday, July 11, 2007 1:40 PM
Ok, I kind of solved my problem, but with a workaround. Now I don't try to generate the delegate property directly, but I use a delegate as a sub-property of another property ... which is sort of what I needed in the first place anyway.

Now, you'd have the same problem if you wanted to generate the code from a TypeConverter for the property because you'll have to generate the delegate somehow. So instead, I do the code generation in a CodeDomSerializer by adding a DesignerSerializer attribute to the class containing the delegate. With CodeDomSerializer, you can generate all sorts of code your heart desires so the rest is easy.

However, if anyone knows how to change the Serializer of a delegate, please tell me.

Cheers!
Rado R  Thursday, July 12, 2007 2:29 PM

You can use google to search for other answers

Custom Search

More Threads

• codedom - execute functions
• Visual Designer crash - could not find KB article
• Receiving Form Designer Error: "Type 'mpx.Controller' does not have a constructor with parameters of types DesignerHost."
• Create custom Controls for Win Forms book, sample, etc
• Adding References in the Design Time automatically
• UserControl's child container's contents not being serialized
• .net 2.0 DesignerSurface
• Multi select drop down and data binding
• Subscribe to UserControl's ParentForm events
• table layout panel bug /issue the controls swap at runtime.