Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/10.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
asp.net c#我需要从另一个字符串中存储的变量名访问变量的值_C#_Asp.net - Fatal编程技术网

asp.net c#我需要从另一个字符串中存储的变量名访问变量的值

asp.net c#我需要从另一个字符串中存储的变量名访问变量的值,c#,asp.net,C#,Asp.net,请注意:-我已经回答了几个链接,但它们只指向控件。我可以轻松地使用对象访问对象,没有任何问题。问题在于在运行时访问变量。一个是指向变量的,但我发现它非常困难和僵硬。所以我想知道任何简单易行的方法 我有以下要求:- 场景,例如:- 1. TextBox_mobile is a control object in aspx page 2. mobile is a variable stored in c# .cs file 3. I have a 135+ such controls in a

请注意:-我已经回答了几个链接,但它们只指向控件。我可以轻松地使用对象访问对象,没有任何问题。问题在于在运行时访问变量。一个是指向变量的,但我发现它非常困难和僵硬。所以我想知道任何简单易行的方法

我有以下要求:-

场景,例如:-

 1. TextBox_mobile is a control object in aspx page
 2. mobile is a variable stored in c# .cs file
 3. I have a 135+ such controls in aspx page and want to store them in variables in .cs file on say submit button.
 4. I have stored in a table having two fields control_objects and against it variable names
 5. So when submitt is fired (click event) I want to run the assigning task with running a process to retriew control names and
variables names and assinging the values of control objects to appropriate variables.
例如,要想在实践中更进一步:-

// create a variable to store textbox control
string mobile = "" 
// Text_mobile is a TextBox in aspx page
// A temporary variable to store variable in loop from table 
string var_mobile = "mobile"; // Currently I am hard coding
// now I wish to use this var_mobile to make automatically assign the value into main variable
<<var_mobile>> = TextBox_mobile.Text.Trim();
//and backend it should be actually storing same in earlier mobile variabl
mobile = TextBox_mobile.Text.Trim();
//创建一个变量来存储textbox控件
字符串mobile=“”
//Text_mobile是aspx页面中的文本框
//用于在表的循环中存储变量的临时变量
字符串var_mobile=“mobile”//目前我正在硬编码
//现在,我希望使用这个var_mobile来自动将值分配到主变量中
=TextBox_mobile.Text.Trim();
//后端应该在早期的移动设备中存储相同的数据
mobile=TextBox_mobile.Text.Trim();
因为我有很多对象和变量,以后还要处理变量,所以我希望每次都在循环逻辑中完成,而不是在单个基础上分配它们

在Asp.net C#中是否有这种可能?

在C#中,这和所有需要代码与程序集中声明的类型及其成员的数据交互的事情都可以通过使用来实现

要使用包含字段名称的字符串获取字段,可以使用方法,然后调用返回的

例如,如果您有一个名为
MyClass
的类这样声明

class MyClass
{
    public int myField = 0;
}
您可以使用以下代码设置
myField

MyClass myClass = new MyClass();
string fieldName = "myField";
int valueToSet = 10;

typeof(MyClass).GetField(fieldName).SetValue(myClass, valueToSet);

根据Kacper的快速回答,我能够用他的点对点解决问题

CommonClass.cs

public class CommonClass
{
     public string mobile = "";
     public CommonClass()
     {
         //
         // TODO: Add constructor logic here
         //
      }
}
.cs文件中的我的代码:-

    string mobile = "";

    CommonClass obj_class = new CommonClass();
    string fieldname = "mobile";
    typeof(CommonClass).GetField(fieldname).SetValue(obj_class, TextBox_mobile.Text);

    mobile = obj_class.mobile.Trim();

这回答了你的问题吗@ElmoDev001仅适用于控制对象。我对此没有异议。我想远程访问变量并从适当的控制对象分配值。这非常有效。你确实找到了我需要的东西。非常感谢。我会把我的代码放在更详细的地方,这样它可以帮助很多其他人。我很高兴听到这一点;)如果是这样,那么你可以接受我的回答作为你问题的答案