Reputation: 46591
I have to set some properties at run time, but the issue I run into is that some properties have 5 settings while another may have 3 or 4. Is there a way I can create a generic class that could hold any number of settings for a property without having to write multiple classes?
One example is that I have a property called Color
which the only setting is the Color
, but in another scenario, I have a property called Mask
which has a setting for MaskType
and the Mask
Upvotes: 1
Views: 114
Reputation: 16628
It's a classic, and the solution really depends on how you wish to access your properties.
In ASP.NET the cache uses something like:
public class Cache
{
private Dictionary<string, object> CachedValues;
public object this [string arg]
{
get
{
return CachedValues[arg];
}
}
}
Or you can use Tuple<T1, T2, T3...>
Or you can use dynamic
and anonymous types
Upvotes: 2
Reputation: 7105
I think it is always better to write individual classes that will represent your individual objects. In my opinion you will only create more problems trying to write a generic class. Perhaps have a look at the Factory or Adapter patterns to decide on which of the classes to use in different scenarios.
Upvotes: 1
Reputation: 1847
You might like to have a look around the ExpandoObject. It allows you to add properties and their values dynamically, though it does require .NET 4 or upwards.
Upvotes: 2