views:

47

answers:

3

Suppose I have a class with several properties that correspond to user-defined parameters, such as:

bool StepUpHedge { get; set; }
bool PullOnJump { get; set; }
bool PullOnCross { get; set; }
double MaxStockSpread { get; set; }
double MaxHedgeSpread { get; set; }

(These are just examples, not real code, and anyway what they mean isn't important.)

Now, let's say there are in fact dozens or even hundreds of these user-defined parameters. To deal with saving/loading and (best of all) copying all or some of them from one instance of the class to another can be quite cumbersome.

Obviously, I don't want to just make a single Dictionary<Parameter, object> to store them all (type safety? boxing? what's that?), but it would be nice to be able to enumerate over them (without reflection), which is causing me to wonder...

What if I made several dictionaries:

public enum BoolParameter { /*...*/ }
public enum DoubleParameter { /*...*/ }
public enum IntParameter { /*...*/ }
Dictionary<BoolParameter, bool> BoolParameters { get; set; }
Dictionary<DoubleParameter, double> DoubleParameters { get; set; }
Dictionary<IntParameter, int> IntParameters { get; set; }

I know this seems very iffy, and there are cleaner ways to deal with properties in general (for instance, by subclassing them into logical groups); but what I can't seem to wrap my head around is how else to provide the full desired functionality in an easily maintainable way.

The desired functionality is: to be able to display all user-defined parameters for a selected instance, select which properties to copy, and copy the values of only those selected properties from one instance to another. By using an enumerable interface for these properties, this becomes quite simple to do -- and adding a new parameter is as simple as adding it to the dictionary. Otherwise, without using reflection, I'm having a hard time seeing it.

Come to think of it, maybe I should be using a strongly typed DataSet for all this...

+2  A: 

I've never actually done this, but I would imagine you can iterate over the properties of a class using Reflection's GetProperties().

which gives you an array of PropertyInfo objects to work with.

http://msdn.microsoft.com/en-us/library/system.reflection.propertyinfo.aspx

Edit: just noticed the 'without reflection' bit. why without reflection? if you don't have any hard restriction, this is probably better than a complicated multi-dictionary solution..

Oren Mazor
A: 

If there are really hundreds of these properties, I think it is not practical anyway to implement each of them.

Assumed that these properties do not have anything specific, I would actually also consider a more general way to handle this, and what you suggest is not so bad - even if it is unconventional. But it is also unconventional to have hundreds of properties.

Stefan Steinegger
A: 

If I can fully understand what do you mean,i think it's better to use a collection of parameter definition instead. Something like this:

public class ParameterInfo : IClonabe
{
    public string ParameterName { get; set; }
    public Type ParameterType { get; set; }
    public object Value { get; set; }
}

And another class:

public class UserParameters : IClonabe
{
    public IEnumerable<ParameterInfo> Parameters { get; set; }
}

using this model is acceptable, because parameters are not permanent. you can implement IClonable interface for easy copying from your parameters.

Mehdi Golchin