动态创建对象 [英] Dynamically create objects

查看:52
本文介绍了动态创建对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些值的数组.我想要做的是遍历数组元素并创建一个以数组元素命名的对象(特别是到星号服务器的自定义连接管理器).

I have a array with some values. What i want to do is to loop through the array elements and create an object ( a custom connection manager to a asterisk server in particular) named after an array element.

for (int k = 0; k < servers.Count; k++)
           {

   connectionk = new connection(servers[0].tostring());

   }


我想不出一种使用动态名称来命名连接的方法,例如,connection0(其中0是k的值).

有帮助吗?


I cant figure out a way to name the connection with an dynamic name , like connection0 for example (where 0 is the value of k).

Any help ?

推荐答案

使用Dictionary对象.

因此您的代码看起来像

Use a Dictionary object.

So your code will look something like

for (int k = 0; k < servers.Count; k++)
{ 
    Dictionary<string,object> objConnection;
    objConnection.Add(servers[0].tostring(),new connection());
}


为什么要创建这样的连接变量名称.创建一个连接数组/列表以执行此操作. :-D

Why do you want to create connection variable names like this. Create an array/list of connection to do this. :-D

List<Connection> lcon = new List<Connection>();
for (int k = 0; k < servers.Count; k++)
{
   lcon.add(new connection(servers[0].tostring()));
}


现在在lcon中找到所有对象. :rose:


Now find all the objects in lcon. :rose:


这篇关于动态创建对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆