从字典计数中创建多个新类对象

时间:2018-03-17 12:24:31

标签: c# .net oop collections

int proprtyCount = dictionary.Keys.Count;

 foreach (KeyValuePair<object, object> pair in dictionary)
{
     ClassCustom obj1 =new ClassCustom(pair.Key, pair.Value);
}

我需要使用dictionary.keys.count创建数字对象,并将这些对象传递给某个集合类。

我必须将下面的对象传递给集合(例如,在这种情况下,字典键数为3)

SomeCollection collection =new SomeCollection(obj1,obj2,obj3);

1 个答案:

答案 0 :(得分:0)

为什么不将它分配到列表中?

<form action="<?php echo $this->getUrl('checkout/cart/updatePost') ?>" method="post">
    <?php echo $this->getBlockHtml('formkey'); ?>

让SomeCollection类在其构造函数中初始化类型为int proprtyCount = dictionary.Keys.Count; var classCustomList = new List<ClassCustom>(); foreach (KeyValuePair<object, object> pair in dictionary) { classCustomList.Add(new ClassCustom(pair.Key, pair.Value)); } SomeCollection collection = new SomeCollection(classCustomList); 的列表。

更新:如果是某种集合类,可以尝试做类似于

的事情
classCustomList

类型数组

的重载

例如

int proprtyCount = dictionary.Keys.Count;
var conditions = new PropertyCondition[propertyCount];
int index = 0;
foreach (KeyValuePair<object, object> pair in dictionary)
{
    conditions[i] = new PropertyCondition(pair.Key, pair.Value));
    index++;
}

var conditionEnabledButtons = new AndCondition(conditions);