Persisting Custom Control Properties in C# 2.0

I recently found myself in a bit of a pickle trying to persist some properties in a custom control I am developing at work.  My confusion was down to several properties persisting fine while other properties just would not maintain any values when I switched between source and design views in my test application.
I was ready to throw in the towel and put this down to a quirk with ASP.Net Custom Controls and try out some really complicated method to force the properties to persist when I stumbled upon something interesting.

If I set my property to be a string everything persisted, with no extra effort on my part for example:

public string Foo
{
...
}

works fine, while:

public double Foo
{
...
}

won’t persist.  A bit of research led me to believe that the .net framework takes care of strings for you.
I then noticed that all the properties I had that were not strings but were persisting just fine had a typeConverter applied to them, either custom converters I had written, or converter from the .Net framework.
for example:

[typeConverter(typeOf(ColorConverter))]
public colour Foo
{
...
}

worked just fine… so I tried the following with the double sample above:

[typeConverter(typeOf(DoubleConverter))]
public double Foo
{
...
}

This resulted in the property persisting fine.

So, the reason for this post is two fold really, it may provide a clue to anyone else struggling to persist their properties as I was, but also it opens this up for comment as to whether this is the right way of going about this or whether there is indeed a better way.
Are there any performance hits to using a typeConverter in this way?

Advertisements

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: