未知数量类型参数的类设计

发布于 2024-10-06 05:22:57 字数 525 浏览 4 评论 0原文

我需要设计一个可以容纳任意数量类型的类,尽管我在设计时不知道这些值的类型和数量。例如 - 我可以使用 2 个 int 和一个 long 或仅 1 个 int 值来初始化该类。我还想尽可能避免装箱(使用“对象”类型)。每种类型也有一个名称,因此保存值的内部字典可能看起来像这样

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

,构造函数可能看起来像这样

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

我可以构造一个像这样的实例

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

关于更有效的设计的任何建议,以尽可能避免装箱。

编辑:也应该可以指定“字符串”值作为参数。我最初只提到过值类型,但现在情况不再是这样了!

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 design 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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

淡淡的优雅 2024-10-13 05:22:57

就我个人而言,我不会害怕一点拳击;我不会害怕。除非您已经描述并证明这是一个问题,否则这可能是您遇到的最不重要的问题。但是匿名类型怎么样:

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

唯一的问题是在此方法之外获取这些值的唯一方法是通过反射。这不一定是一个问题。但在很多方面,Dictionary(虽然更重)要简单得多。

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.

别低头,皇冠会掉 2024-10-13 05:22:57

试试这个

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

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
陌生 2024-10-13 05:22:57

如果您事先知道可能的类型,则可以为每种类型使用单独的字典,然后在读回值时不需要装箱。

例如,使用您现有的代码结构:

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);
        }
    }
}

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);
        }
    }
}
许你一世情深 2024-10-13 05:22:57

可能类似于下面的

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);
    }

}

用途

        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");

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");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文