Hi, I'm posting this question which I previously posted in the Winforms
group (without any luck) in the hope that someone can point me in the
right direction re. serializing properties to the application configuration
file (app.config). Hopefully the moderater will let this question through.
What I need to be able to do, is serialize/deserialize certain properties
for components (of varying type of component) from/to the application
configuration file (app.config). This needs to operate similarly to dynamic
properties, where serialization handles both generation of code and addition
of configuration data to the app.config file.
Unfortunately, I cannot use the existing dynamic properties support as the
types I'm attempting to serialize are complex types (they contain aggregate
class instances, references etc.). Also, the system I'm developing is
intended to support external configuration by an extensible configurator
application, which makes dynamic properties unsuitable as I cannot detect
the type of object referred to by the dynamic property (my system embeds
type information)
What I've written so far, uses type converters to generate the code by
returning an InstanceDescriptor. The InstanceDescriptor I return describes
an invocation to a static method on a configuration component. This is
similar to the code generated for dynamic properties where a call to
AppSettings is generated. My type converter also adds a reference to the
property's object to a list of objects to be serialized. I then intercept
the SerializationComplete event from the serialization manager, and then
serialize the list of objects to the app.config file.
So, what I end up with in InitializeComponent is something like this:
private void InitializeComponent()
{
// [snipped code]
this.readerControl1 = new
SensorID.SmartEas.ReaderControl(this.components);
//
// readerControl1
//
this.readerControl1.ConnectionSettings =
((SensorID.SmartEas.ReaderConnectionSettings)(SensorID.Config.ConfigManager.
GetSection("test connection")));
// [snipped code]
}
As can be seen, this directs the ConnectionSettings property to be retrieved
using my configuration manager, which then deserializes the property from
the configuration file, which might look like this:
<configuration>
<SensorID.Configuration>
<SensorID.ReaderConnection Name="test connection">
<Settings xsi:type="SensorID.Reader.IntermecRal">
<Parity>None</Parity>
<BaudRate>115200</BaudRate>
<NoStopBits>1</NoStopBits>
<NoBits>8</NoBits>
<PortNo>1</PortNo>
<AttributesList>
<AttributeEntry>
<EntryName>IT500_READ_TRIES</EntryName>
<EntryValue>3</EntryValue>
</AttributeEntry>
</AttributesList>
</Settings>
</SensorID.ReaderConnection>
</SensorID.Configuration>
</configuration>
Now, though, I'm having a whole bunch of problems, which indicate that I'm
probably taking the wrong tack in attempting to implement this system:
1. There is no support for undo/redo -- once my property settings are
serialized to the app.config file, they are stored immediately to disk and
cannot be undone.
2. I have potential conflict with the way the IDE handles dynamic
propertes - once my settings are written to app.config file, if the user is
also setting dynamic properties and then closes the IDE and confirms they
want to save the copy of app.config automatically opened (for dynamic
properties) in the IDE, then I can lose my settings as they are overwritten
by the new app.config file, which does not contain my settings.
3. I need to figure out where the app.config file should be stored - at the
moment I'm using the current working directory, which is not necessarily (in
fact it isn't) the directory where the project is stored.
4. Unlike dynamic properties, I don't know how to add app.config to the
project and to make my system store data in an open (unsaved) copy of
app.config within the IDE.
5. Deserialization within the IDE from code does not work correctly - due to
the use within my configuration component (ConfigManager in the example) of
the ConfigurationSettings class, my attempt to retrieve the configuration
data fails. This is because the ConfigurationSettings class automatically
attempts to retrieve the config file for the host app (named host app.exe +
.config). At design time, this means the configuration settings are read
for the .NET IDE itself (devenv.exe.config) and of course it therefore
fails. I suppose what I probably need to do, is detect whether in design
mode or not, and if in design mode read the XML settings from disk myself
(don't use the ConfigurationSettings) class.
I know there are a lot of issues/questions there but I'm really hoping that
someone can give me some pointers here on what tack I should be taking to do
this - I feel like I'm drowning!
Thanks in advance.
Phil
===================================
This list is hosted by DevelopMentor� http://www.develop.com
You may be interested in Guerrilla .NET, 24 March 2003, in London and Boston
http://www.develop.com/courses/gdotnet
View archives and manage your subscription(s) at http://discuss.develop.com