C# 字符串作为变量名
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9183041/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
String as variable name
提问by hradecek
is it possible in C# to use a String like a variable name ?
I have got a:
在 C# 中是否可以像变量名一样使用字符串?
我有一个:
String x = "matrix1_2";
Microsoft.VisualBasic.PowerPacks.RectangleShape y = ???;
??? - there should be the name of variable...matrix1_2
???- 应该有变量名...matrix1_2
采纳答案by Denis Biondic
No, you can't, and it makes no sense honestly to have a feature like that.
不,你不能,老实说,拥有这样的功能是没有意义的。
If you need to dynamically assign some data with key and value, you could use an dictionary:
如果您需要使用键和值动态分配一些数据,您可以使用字典:
Dictionary<string, RectangleShape> shapes = new Dictionary<string, RectangleShape>();
shapes.Add("matrix1_2", new RectangleShape( ... ));
Then you can simply read the "variable" like
然后你可以简单地阅读“变量”,如
shapes["matrix1_2"]
回答by Oded
This is not possible.
这不可能。
You can't have dynamic variable names in C#, VB.NET or any .NET language. There is no support for such a feature.
您不能在 C#、VB.NET 或任何 .NET 语言中使用动态变量名称。不支持此类功能。
回答by Marc Gravell
No it is not. If "matrix1_2" is a local variable, then you can't do it as the variable might not even existafter the compiler is through, if it is actually an instance field, then reflection may help:
不它不是。如果“matrix1_2”是一个局部变量,那么你不能这样做,因为编译器通过后该变量甚至可能不存在,如果它实际上是一个实例字段,那么反射可能会有所帮助:
object value = obj.GetType().GetField(fieldName).GetValue(obj);
回答by chrs
Seems like a bad idea. Try with enums or own datatypes /classes
似乎是个坏主意。尝试使用枚举或自己的数据类型/类
回答by Brandon Moore
You could with reflection, but I suspect in this case theere is a better design you could implement that would be better than using reflection. If you provide a little more info probably one of us could help you with that.
您可以使用反射,但我怀疑在这种情况下,您可以实现比使用反射更好的设计。如果您提供更多信息,我们中的一个人可能会帮助您。
Declare a dictionary variable:
声明一个字典变量:
Dictionary<string, RectangleShape> rectangleDictionary = new Dictionary<string, RectangleShape>();
Then, where you would normallay write "matrix1_2 = somevalue;", instead write:
然后,你通常会写“matrix1_2 = somevalue;”,而不是写:
rectangleDictionary.add("matrix1_2", somevalue)
Then you'll be able to work with the variable name:
然后您将能够使用变量名称:
rectangleDictionary["matrix1_2"] = someothervalue;
rectangleDictionary["matrix1_2"].someproperty = something;
Microsoft.VisualBasic.PowerPacks.RectangleShape y = rectangleDictionary["matrix1_2"];
回答by Justin
Does "matrix1_2" exist already? In other words, when you say y = ??? are you trying to access an existing variable or create one.
“matrix1_2”是否已经存在?换句话说,当你说 y = ??? 您是要访问现有变量还是创建一个变量。
To my knowledge, there is no way to create a new "named" instance at run-time. However, you can retrieve the names of existing fields and select them using System.Reflection.
据我所知,没有办法在运行时创建一个新的“命名”实例。但是,您可以检索现有字段的名称并使用 System.Reflection 选择它们。
String x = "matrix1_2";
Microsoft.VisualBasic.PowerPacks.RectangleShape y;
Type type = typeof(MyType); // Get type pointer
FieldInfo[] fields = type.GetFields();
foreach (var field in fields)
{
if (field.Name == "matrix1_2")
{
y = field;
}
}
MyTypeabove is the name of whatever class matrix1_2lives in.
MyType上面是任何类matrix1_2所在的类的名称。
回答by elliott.io
You can get a field value from a dynamic type in this manner (using C# in a Silverlight 5 project).
您可以通过这种方式从动态类型获取字段值(在 Silverlight 5 项目中使用 C#)。
Type itemType = item.GetType();
try
{
PropertyInfo field = itemType.GetProperty(fieldName);
object val = field.GetValue(item, null);
}
catch (Exception ex)
{
// field doesn't exist, do something else
}
*Where "item" is a dynamic type generated at runtime (but doesn't have to be) and "fieldName" is a string for the property name you are looking for.
*其中“item”是在运行时生成的动态类型(但不一定是),“fieldName”是您要查找的属性名称的字符串。

