I am trying to serialize few custom settings from configuration file. Below are my settings:
<FavouriteTools>
<favouriteTool PluginPlaceHolder="FavoriteTool1" PlugInName="Brightness"
PlugInType="2D" PlugInAssemblyName="Micro.DigiMic.Plugins.AreaBasedOnBrightness.AreaBasedOnBrightnessPlugin"
GUID="0000c03a-b891-4ebd-87d7-5fbc19073a1a" />
<favouriteTool PluginPlaceHolder="FavoriteTool2" PlugInName="CircleArea"
PlugInType="2D" PlugInAssemblyName="Micro.DigiMic.Plugins.CircleAreaPlugin.CircleAreaPlugin"
GUID="0000c06a-b891-4ebd-87d7-5fbc19073a1a" />
<favouriteTool PluginPlaceHolder="FavoriteTool3" PlugInName="Contour Area"
PlugInType="2D" PlugInAssemblyName="Micro.DigiMic.Plugins.ContourAreaPlugin.ContourAreaPlugin"
GUID="0000c11a-b891-4ebd-87d7-5fbc19073a1a" />
But I get error during serializing them. Here are my classes.
//FavouriteTools - Root Node
[Serializable]
[XmlType("FavoriteTools")]
[ConfigurationCollection(typeof(FavouriteTool), AddItemName = "favouriteTool", CollectionType = ConfigurationElementCollectionType.BasicMap)]
public class FavouriteToolsCollection
: ConfigurationElementCollection
{
//Few plublic methods here
}
//favouriteTool - Child node
[Serializable]
[XmlType("favouriteTool")]
public class FavouriteTool : ConfigurationElement
{
/// <summary>
/// Gets or sets value of Plugin PlaceHolder.
/// </summary>
/// <value>Text Color.</value>
[ConfigurationProperty("PluginPlaceHolder", IsRequired = false)]
public string PlugPlaceHolder
{
get { return (string)base["PluginPlaceHolder"]; }
set { base["PluginPlaceHolder"] = value; }
}
//Few more properties like above
}
I am trying to serialize below class, but gets exception on below like
XmlSerializer xmlinf = new XmlSerializer(data.GetType());
`data` is `ExportUser`
[Serializable]
public class ExportUser
{
public bool IsMetric { get; set; }
[XmlArray("FavoriteTools")]
[XmlArrayItem("favouriteTool")]
public FavouriteToolsCollection FavoriteTools { get; set; }
}
I get this error - There was an error reflecting type 'ExportUser'
and in inner exception the error says - There was an error reflecting type 'FavoriteTools'
.
Is there anything missing?
Update:
After seeing the inner exception, the error is
{"You must implement a default accessor on System.Configuration.ConfigurationLockCollection because it inherits from ICollection."}
There was an error reflecting type 'Zeiss.Micro.DigiMic.Application.FavouriteTool'.
But I do have a default accessor in FavouriteToolsCollection
class:
public FavouriteTool this[int index]
{
get
{
return (FavouriteTool)BaseGet(index);
}
set
{
if (this.BaseGet(index) != null)
{
this.BaseRemoveAt(index);
}
this.BaseAdd(index, value);
}
}
What more is missing?
OK. I just encountered this. And solved it using a wrapper object.
What I did was to create a simple wrapper object for the thing I want to serialize. I my case it is a ConfigurationCollection. I serialize that and create a couple adapters to map from the serializable object to the actual object and vice versa.