H
Helmut Giese
Hello out there,
I don't know how to pass a 'type' as a parameter and therefore
currently have this code to serialize components to XML:
TypeConverter cvt;
string str;
foreach (PropertyItem pItem in props) {
switch (pItem.DisplayName) {
case "Anchor":
cvt = TypeDescriptor.GetConverter(typeof(AnchorStyles));
str = cvt.ConvertToInvariantString(pItem.Value);
break;
case "Cursor":
cvt = TypeDescriptor.GetConverter(typeof(Cursor));
str = cvt.ConvertToInvariantString(pItem.Value);
break;
case "Font":
cvt = TypeDescriptor.GetConverter(typeof(Font));
str = cvt.ConvertToInvariantString(pItem.Value);
break;
<etc. etc. etc.>
Reading it back is done like this
// reader: XMLReader
// self: the control created via its type read from XML
propName = reader.Name;
switch (propName) {
case "Anchor":
cvt = TypeDescriptor.GetConverter(typeof(AnchorStyles));
self.Anchor =
(AnchorStyles)cvt.ConvertFromInvariantString(str);
break;
case "Cursor":
cvt = TypeDescriptor.GetConverter(typeof(Cursor));
self.Cursor =
(Cursor)cvt.ConvertFromInvariantString(str);
break;
case "Font":
cvt = TypeDescriptor.GetConverter(typeof(Font));
self.Font = (Font)cvt.ConvertFromInvariantString(str);
break;
<etc. etc. etc.>
I hate code like this - endlessly repeating the same thing over and
over. However, I don't know how to do it otherwise, so I just lived
with it.
Now, however, I am integrating components from National Instrument's
Measurement Studio and they come with loads of properties of their
own. I could of course repeat the same code pattern another 50 times,
but I'd be much happier if someone could tell me how to put this code
into a function passing the type as parameter (and "somehow" handling
the type cast when reading).
Any insight into this magic will be greatly appreciated.
Best regards
Helmut Giese
I don't know how to pass a 'type' as a parameter and therefore
currently have this code to serialize components to XML:
TypeConverter cvt;
string str;
foreach (PropertyItem pItem in props) {
switch (pItem.DisplayName) {
case "Anchor":
cvt = TypeDescriptor.GetConverter(typeof(AnchorStyles));
str = cvt.ConvertToInvariantString(pItem.Value);
break;
case "Cursor":
cvt = TypeDescriptor.GetConverter(typeof(Cursor));
str = cvt.ConvertToInvariantString(pItem.Value);
break;
case "Font":
cvt = TypeDescriptor.GetConverter(typeof(Font));
str = cvt.ConvertToInvariantString(pItem.Value);
break;
<etc. etc. etc.>
Reading it back is done like this
// reader: XMLReader
// self: the control created via its type read from XML
propName = reader.Name;
switch (propName) {
case "Anchor":
cvt = TypeDescriptor.GetConverter(typeof(AnchorStyles));
self.Anchor =
(AnchorStyles)cvt.ConvertFromInvariantString(str);
break;
case "Cursor":
cvt = TypeDescriptor.GetConverter(typeof(Cursor));
self.Cursor =
(Cursor)cvt.ConvertFromInvariantString(str);
break;
case "Font":
cvt = TypeDescriptor.GetConverter(typeof(Font));
self.Font = (Font)cvt.ConvertFromInvariantString(str);
break;
<etc. etc. etc.>
I hate code like this - endlessly repeating the same thing over and
over. However, I don't know how to do it otherwise, so I just lived
with it.
Now, however, I am integrating components from National Instrument's
Measurement Studio and they come with loads of properties of their
own. I could of course repeat the same code pattern another 50 times,
but I'd be much happier if someone could tell me how to put this code
into a function passing the type as parameter (and "somehow" handling
the type cast when reading).
Any insight into this magic will be greatly appreciated.
Best regards
Helmut Giese