. - C#, .Net Framework" /> Topicala Page Index Token

A Journal about the experiences I have developing little applications in C#, Perl, Html and Javascript and talking about things new things that I use. Always Geeky; Always Nerdy; Always poor Grammer!

I am a Software Analyst Developer working in Southport, England but living in Liverpool. I develop mainly in C# and ASP.Net. I have been developing comercial software for several years now. I maintain this site (hosted at SwitchMedia UK) as a way of exploring new technologies (such as AJAX) and just generally talking about techie geek issues. This site is developed through a host of Perl scripts and a liberal use of Javascript. I enjoy experimenting with new technologies and anything that I make I host here.

Quick Search

Web www.kinlan.co.uk

Tuesday, October 03, 2006

Assigining a TypeConverter to a class you don't own

I ran into problems with the XNA Beta1, where by I had a class that had a Vector2 struct in.  The problem with the XNA Vector2 struct is that there is no TypeConverter for it at the moment.  This means that there is no support in the designer. 

I initially solved this problem by assigning a type converter to the public property on my class that used the Vector2. (see below)

public class A
{
private Vector2 v;

[TypeConverter(typeof(Vector2Converter))]
public Vector2 Vec
{
get{ return v; }
set{ v= value}
}
}

However, VS2005, when serializing out to code from the designer, it will not obey the TypeConverter on the property, but will try to use the Type's Conveter eventhough the designer will use the TypeConverter on your class to edit the control... clear as mud? :)


This leads to ugly code in the InitializeCode method that tries to look in the resource file to get the object out.  I didn't like this solution because it looks ugly and is hard to maintain.


I solved this problem by forcing the TypeConverter on to the Vector2.  In my classes constructor I called the following code I have created.

public static void Register<T, TC>() where TC: TypeConverter
{
Attribute[] attr = new Attribute[1];
TypeConverterAttribute vConv = new TypeConverterAttribute(typeof(TC));
attr[0] = vConv;
TypeDescriptor.AddAttributes(typeof(T), attr);
}

The above code adds the TypeConverter (TC) to the Type (T) so that the designer can serialize to code correctly using my own TypeConverter.  The main thing it is doing is calling: TypeDescriptor.AddAttributes


The solution then allows the Designer to serialize to code and it gets rid of having to serialize the object to a resource file.  The solution would look like the code below.

public class A { 
private Vector2 v;

//Enables Designer support, so it can be edited
[TypeConverter(typeof(Vector2Converter))]
public Vector2 Vec {
get{ return v; }
set{ v= value}
}

public A()
{
//Enables designer serialization to code
ConverterRegistration.Register<Vector2 , Vector2Converter>();
}
}


Comments: [Add New]

public static void Register 〈T, TC〉() where TC : TypeConverter
{
TypeConverterAttribute attr = new TypeConverterAttribute(typeof (TC));

if(!TypeDescriptor.GetAttributes(typeof(T)).Contains(attr))
TypeDescriptor.AddAttributes(typeof(T), attr);
}
IMHO it is cleaner this way.

By Anonymous Anonymous, at Sunday, February 11, 2007 9:34:00 PM

Doesn't "Contains(attr)" always return "false" ? Since attr is a new instance...

By Anonymous Anonymous, at Wednesday, March 18, 2009 9:55:00 AM