Shipping configuration parameters along a library

  • Thread starter Thread starter Joannes Vermorel
  • Start date Start date
J

Joannes Vermorel

I am currently implementing a scientific library in C#. The algorithms
include some system dependent tuning parameters (lets say memory vs. speed
parameters for example). Since, those parameters are quite numerous, it
would be cumbersome for the client of the library to be forced to provide
explicit values for all those parameters. Also, default parameters should be
provided by the library itself. But I would rather not "hard code" such
parameters. I think, a good solution would be somehow to ship a (XML)
configuration file along the library. This way, the default performance of
the library could still be tuned after compilation.

To do this, there are a lot of dirty home-made solutions. But I think, this
issue should have some classical solutions (using System.Resource or
System.Configuration ???). Does someone have a convenient solution for this
problem ?

Thank you for the help,
Joannes Vermorel
 
Place your configuration settings into their own class, and you can use
XMLSerializer (described in Visual Studio Help) to read and write an object
of the class to an XML file.

Give your class a constructor that initialises the default settings. That
way, you can easily use these if the XML file doesn't exist.

Here's a simple example of the class to be serialised. (In production code,
you'd probably want to use properties instead of public variables.)

[Serializable]
public class MySettings
{
public bool bFavourSpeedOverSize;
public bool bOptimiseForSomeProcessor;

public MySettings()
{
this.SetDefaults();
}

public void SetDefaults()
{
bFavourSpeedOverSize = true;
bOptimiseForSomeProcessor = false;
}
}

P.
 
Back
Top