开发者

Class design for unknown number of type parameters

开发者 https://www.devze.com 2023-01-29 14:50 出处:网络
I need to design a class that can hold an arbitrary number of types, although I do not know the type and number of these values at design time. For example - I could initialize the class with 2 ints a

I need to design a class that can hold an arbitrary number of types, although I do not know the type and number of these values at design time. For example - I could initialize the class with 2 ints and a long or just 1 int value. I also would like to avoid boxing as far as possible (using an 'object' type). Each type would also have a name, so an internal dictionary that holds the values could look like

Dictionary<string,object> nameValues;//Boxing!

and the constructor could look like

MyClass(params object[] values) { ... }

I could construct an instance like this

MyClass mc = new MyClass("intVal",3,"doubleVal",3.5,"dateTimeVal",DateTime.Now);

Any suggestions on a more efficient des开发者_如何学Cign that would avoid boxing as far as possible.

Edit: It should be possible to specify a 'string' value as a parameter also. I had initially mentioned only Value types but that's not the case anymore!


Personally I wouldn't be scared of a little boxing; unless you've profiled and proven that it is an issue, this is probably the least of your issues. But how about an anon-type:

var mc = new { intVal = 3, doubleVal = 3.5, dateTimeVal = DateTime.Now };

The only problem is that outside of this method the only way to get these values back out is via reflection. Which isn't necessarily a problem. But in many ways the Dictionary<string,object> is (while more overweight) much simpler.


try this

Dictionary<string, ValueType> x = new Dictionary<string, ValueType>();
x.Add("1",1);
x.Add("2", 23.33m);
x.Add("3", new MyStruct());
x.Add("4", new object()); // compile-time error


If you know in advance the possible types, you can use separate Dictionary for each type then you don't need boxing when reading the values back.

For example, using your existing code structure:

class MyClass
{
    private Dictionary<string, int> intValues = new Dictionary<string, int>();
    private Dictionary<string, double> doubleValues = new Dictionary<string, double>();
    private Dictionary<string, DateTime> dateTimeValues = new Dictionary<string, DateTime>();

    public MyClass(params object[] values)
    {
        if (values.Length % 2 != 0)
            throw new ArgumentException("invalid values!", "values");
        for (int i = 0; i < values.Length; i += 2)
        {
            string key = values[i].ToString();
            object value = values[i + 1];
            if (value is int)
                intValues.Add(key, (int)value);
            else if (value is double)
                doubleValues.Add(key, (double)value);
            else if (value is DateTime)
                dateTimeValues.Add(key, (DateTime)value);
        }
    }
}


may be something like below

public class ValueHolder
{
    public ValueType[] Values { get; set; }
    public string[] NonValueType { get; set; }
}

public class SomeValueType<T> where T:ValueHolder
{
    Dictionary<string, T> tempDict = new Dictionary<string, T>();


    public SomeValueType(T val,string keyName)
    {
        if(!tempDict.Keys.Contains(keyName))
             tempDict.Add(keyName, val);
    }

}

uses

        ValueType[] arr = new ValueType[] { 1, 1.1, DateTime.Now };
        ValueHolder vh = new ValueHolder();
        vh.Values = arr;
        vh.NonValueType = new string[] { "temp", "s" };
        SomeValueType<ValueHolder> temp = new SomeValueType<ValueHolder>(vh, "key");
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号