RE: problems with storing a custom type in application settings



Hi Michael,

As mentioned in the FAQ article, one means is implementing a TypeConverter
for the custom type. Here is a simple example:

=========================
using System;
using System.Collections.Generic;
using System.Text;
using System.Configuration;
using System.ComponentModel;



namespace TypeLib
{
[TypeConverter(typeof(MySettingTypeConverter))]
[SettingsSerializeAs( SettingsSerializeAs.String)]
public class MySettingType
{
private string _name;
private string _path;
private bool _enabled;

public string Name
{
get { return _name; }
set { _name = value; }
}

public string Path
{
get { return _path; }
set { _path = value; }
}

public bool Enabled
{
get { return _enabled; }
set { _enabled = value; }
}

public MySettingType()
{ }

public MySettingType(string n, string p, bool e)
{
_name = n; _path = p; _enabled = e;
}

}

public class MySettingTypeConverter :TypeConverter
{
public override bool CanConvertFrom(ITypeDescriptorContext context,
Type sourceType)
{
if(sourceType == typeof(string)) return true;

return base.CanConvertFrom(context, sourceType);
}



public override object ConvertFrom(ITypeDescriptorContext context,
System.Globalization.CultureInfo culture, object value)
{
if (value.GetType() == typeof(string))
{
MySettingType mst = new MySettingType();

string str = value as string;
if (str != null)
{
str = str.Trim();

string[] props = str.Split(new char[] { ',' },
StringSplitOptions.None);
mst.Name = props[0];
mst.Path = props[1];
mst.Enabled = bool.Parse(props[2]);
}

return mst;
}
else return base.ConvertFrom(context, culture, value);
}

public override object ConvertTo(ITypeDescriptorContext context,
System.Globalization.CultureInfo culture, object value, Type
destinationType)
{
if (destinationType == typeof(string))
{
string str = string.Empty;

MySettingType mst = value as MySettingType;
if (mst != null)
{
str = string.Format("{0},{1},{2}", mst.Name, mst.Path,
mst.Enabled.ToString());
}
return str;
}
else return base.ConvertTo(context, culture, value,
destinationType);

}
}
}

===========================

the ConvertTo ,From method make you able to edit the type in designer.
Also, for complex type, you can consider using xml serialization in the
typeconverter.


Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead


This posting is provided "AS IS" with no warranties, and confers no rights.

--------------------
From: =?Utf-8?B?bWljaGFlbCBzb3JlbnM=?= <m_j_sorens@xxxxxxxxxxxxxxxx>
References: <03DCCB07-E2BD-46E1-9661-108F2A0B6534@xxxxxxxxxxxxx>
<zGOnaK2SIHA.6904@xxxxxxxxxxxxxxxxxxxxxx>
Subject: RE: problems with storing a custom type in application settings
Date: Wed, 2 Jan 2008 15:15:02 -0800

Additional comments:
On the chance that you will tell me that the flakiness I have observed is
caused by the lack of TypeConverter/XmlSerializable, I tried to read up on
those topics. Frankly, the documentation on both of these topics is not at
all conducive to the simple task I am trying to accomplish. That includes
both the links you provided plus my own searches through MSDN. What I need
is
an example or straightforward explanation of how to put a custom type into
an
application setting and I have yet to find it.


.



Relevant Pages


Loading