Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/313.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在实例化对象时以编程方式使用字符串作为对象名_C#_Oop - Fatal编程技术网

C# 在实例化对象时以编程方式使用字符串作为对象名

C# 在实例化对象时以编程方式使用字符串作为对象名,c#,oop,C#,Oop,这是一个人为的例子,但假设我已经声明了对象: CustomObj fooObj; CustomObj barObj; CustomObj bazObj; 我有一个字符串数组: string[] stringarray = new string[] {"foo","bar","baz"}; 如何使用字符串数组以编程方式访问和实例化这些对象,并使用类似于foreach的东西进行迭代: foreach (string i in stringarray) { `i`Obj = new Cus

这是一个人为的例子,但假设我已经声明了对象:

CustomObj fooObj;
CustomObj barObj;
CustomObj bazObj;
我有一个字符串数组:

string[] stringarray = new string[] {"foo","bar","baz"};
如何使用字符串数组以编程方式访问和实例化这些对象,并使用类似于foreach的东西进行迭代:

foreach (string i in stringarray) {
    `i`Obj = new CustomObj(i);
}

希望我想表达的想法是清楚的。这在C#中可能吗?

您需要清楚地了解对象和变量之间的区别。对象本身没有名称。变量名在编译时决定。除非通过反射,否则不能通过执行时间确定的名称访问变量

听起来你真的只是想要一本
字典

Dictionary map=newdictionary();
foreach(stringArray中的字符串名称)
{
映射[名称]=新CustomObj(名称);
}
然后可以使用字典的索引器访问对象

如果您真的试图在执行时根据变量的名称设置变量的值,则必须使用反射(请参阅)。请注意,这不适用于局部变量。

您不能

您可以将它们放入字典:

Dictionary<String, CustomObj> objs = new Dictionary<String, CustomObj>();

foreach (string i in stringarray)
{
    objs[i] = new CustomObj(i);
}

然后,你可以通过反射来达到它们。让我知道你是否想走这条路。

如果变量是类成员变量,则可以使用反射,但对于非常专业的应用程序以外的任何应用程序,反射速度都非常慢。我想如果你详细说明你想做什么,我们可以提供更好的建议。很少有像现在这样访问变量的情况。

一个选项是完全动态的路由,根据,在该路由中,您在字符串中指定一个代码块,然后从程序中编译/运行它

另一个选项,灵活性较低,但更简单的是via——您要求创建一个新对象——它不会分配给动态变量,但这是必需的吗?

您可以使用find函数:

    public static Control FindControl(string controlId, Control container)
    {
        if (container.ID == controlId)
            return container;

        foreach (Control control in container.Controls)
        {
            Control c = FindControl(controlId, control);
            if (c != null)
                return c;
        }
        return null;
    }
然后你会得到你的控制权,基于这样的索引: TextBox firstname=(TextBox)FindControl(string.Concat(“TextBox”,index.ToString()),this);
我希望这能有所帮助。

@jotte:非常感谢您的帮助。我用过它,它很管用!除了需要按container.Name更改container.ID之外

然后,您只需要使用以下内容(此示例用于复选框,但任何类型的变量都可以使用):

string Test=“cbACn”+i.ToString(); 复选框cbTest=(复选框)FindControl(测试,GBACSAT); if(cbTest!=null) { cbTest.Checked=true;
}

使用此.Controls.Find(control_name,true)[0]…记住将其转换为

也许,您是想在运行时将对象分配给实例变量,还是只想动态创建对象?我认为此响应应该获得“直观客户真正想要什么”奖。回答得好,如果“CustomObj”没有构造函数怎么办?@Jnr:那么这个问题就不适用了,因为OP处于有构造函数的情况下。基本上,“如何构造实例”和“如何跟踪集合中的实例”完全是正交关系。ID不是控件的属性。您可以使用Name属性:“if(container.Name==controlId)”
public class SomeClass
{
    private CustomObj fooObj;
    private CustomObj barObj;
    private CustomObj bazObj;
}
    public static Control FindControl(string controlId, Control container)
    {
        if (container.ID == controlId)
            return container;

        foreach (Control control in container.Controls)
        {
            Control c = FindControl(controlId, control);
            if (c != null)
                return c;
        }
        return null;
    }