name变量使用字符串.NET [英] Name Variable using string .NET

查看:214
本文介绍了name变量使用字符串.NET的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我工作在.NET中的反序列化类,我必须开发一种能够对我有存储在一个字符串变量名。

I'm working on a deserialization class in .NET, I have to develop a method that provides to me with a variable name that is stored in a string.

我有一个字符串,如:

string string_name = "this_is_going_to_be_var_name";

我现在能做些什么让我的code动态声明一个名为this_is_going_to_be_var_name变量?

Now what can I do so that my code dynamically declares a variable named this_is_going_to_be_var_name?

因此​​,要清楚的事情了:会有一个反序列化类,将声明的同名变量作为输入提供与其父类型按高等程度程序员/用户的愿望串

So to clear things up: There will be a deserialization class that will declare variables of the same names as strings provided as input with their PARENT TYPES as per wish of the Higher Level Programmer/User.

例如:在使用Javascript / jQuery的,当我取JSON通过请求,跨preTER声明的同名变量(S)/阵列(S)和值分配给他们。如果{VAR_NAME:var_value}是一个JSON字符串,跨preTER将创建一个名为变量VAR_NAME,将分配var_value来了,如json_data_object.var_name

推荐答案

没有你不行。 C#中的变量都是静态声明。

No you can't. C# variables are all statically declared.

你能做的最好的事情就是创建变量名的字典和使用的键代替。

The best thing you can do is create a dictionary and use keys instead of variable names.

// Replace object with your own type
Dictionary<string, object> myDictionary = new Dictionary<string, object>();
myDictionary.Add("this_is_going_to_be_var_name", value_of_the_variable);
// ...
// This is equivalent to foo($this_is_going_to_be_var_name) in PHP
foo(myDictionary["this_is_going_to_be_var_name"]); 

这篇关于name变量使用字符串.NET的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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