按名称获取结构中的变量
Get Variables in struct by name
我想使用结构中变量的名称从结构中的变量中获取值。函数应使用字符串 return 结构中具有此名称的变量的值。
在此示例中,“GetStingfromStruct”函数应 return“asdf”。 (当前代码只是一些测试和 returns "System.String string1")。
如果这个问题解决了,我还有一个问题。有没有办法检查结构是否包含具有字符串名称的变量。 (为了避免错误)
private void SetStruct()
{
Mystruct mystruct = new Mystruct();
mystruct.string1="asdf";
mystruct.string2="ghjkl";
mystruct.string3="qwert";
}
private sting GetStingfromStruct(string variableName)
{
return mystruct.GetType().GetField("string1")
}
public struct Mystruct
{
public string string1;
public string string2;
public string string3;
}
您在 set struct 方法中声明了一个 struct 实例,然后您试图在无法访问该实例的 get struct 方法中访问该实例。这是行不通的。你为什么要尝试为这个结构使用 getter 和 setter?为什么在可以使用字典的情况下尝试使用结构?
字典可以这样使用:
var myDict = new Dictionary<string, string>
{
{ "key1", "value1" },
{ "key2", "value2" }
};
你可以使用反射:
在一般情况中,对于任意结构和任意字段,您可以将
using System.Linq;
using System.Reflection;
...
// returns field value by variableName
// or null if field is not found
private static string GetStringFromStruct<T>(T source, string variableName)
where T : struct =>
typeof(T)
.GetFields(BindingFlags.NonPublic | BindingFlags.Public |
BindingFlags.Instance | BindingFlags.Static)
.Where(field => field.Name == variableName)
.Select(field => field.GetValue(field.IsStatic ? null : source))
.FirstOrDefault()
?.ToString();
那你就可以把它当作
string result = GetStringFromStruct(mystruct, "string1");
如果您只想检查 mystruct
,而不想使用 Dictionary<string, string>
:
private sting GetStringfromStruct(string variableName)
{
var field = mystruct.GetType().GetField(variableName);
if (field == null)
return null; // variableName has not found
return field.GetValue(mystruct)?.ToString();
}
要做的最小更改是将实例 (mystruct
) 传递给 GetValue
方法:
private sting GetStingfromStruct(string variableName)
{
return (string)mystruct.GetType().GetField(variableName).GetValue(mystruct);
}
您还应该添加检查以确保字符串值实际上是字段名称等。
但我会附和其他评论和答案,除非您出于某种原因被迫使用结构,否则这不是一个好的设计。
我想使用结构中变量的名称从结构中的变量中获取值。函数应使用字符串 return 结构中具有此名称的变量的值。
在此示例中,“GetStingfromStruct”函数应 return“asdf”。 (当前代码只是一些测试和 returns "System.String string1")。
如果这个问题解决了,我还有一个问题。有没有办法检查结构是否包含具有字符串名称的变量。 (为了避免错误)
private void SetStruct()
{
Mystruct mystruct = new Mystruct();
mystruct.string1="asdf";
mystruct.string2="ghjkl";
mystruct.string3="qwert";
}
private sting GetStingfromStruct(string variableName)
{
return mystruct.GetType().GetField("string1")
}
public struct Mystruct
{
public string string1;
public string string2;
public string string3;
}
您在 set struct 方法中声明了一个 struct 实例,然后您试图在无法访问该实例的 get struct 方法中访问该实例。这是行不通的。你为什么要尝试为这个结构使用 getter 和 setter?为什么在可以使用字典的情况下尝试使用结构?
字典可以这样使用:
var myDict = new Dictionary<string, string>
{
{ "key1", "value1" },
{ "key2", "value2" }
};
你可以使用反射:
在一般情况中,对于任意结构和任意字段,您可以将
using System.Linq;
using System.Reflection;
...
// returns field value by variableName
// or null if field is not found
private static string GetStringFromStruct<T>(T source, string variableName)
where T : struct =>
typeof(T)
.GetFields(BindingFlags.NonPublic | BindingFlags.Public |
BindingFlags.Instance | BindingFlags.Static)
.Where(field => field.Name == variableName)
.Select(field => field.GetValue(field.IsStatic ? null : source))
.FirstOrDefault()
?.ToString();
那你就可以把它当作
string result = GetStringFromStruct(mystruct, "string1");
如果您只想检查 mystruct
,而不想使用 Dictionary<string, string>
:
private sting GetStringfromStruct(string variableName)
{
var field = mystruct.GetType().GetField(variableName);
if (field == null)
return null; // variableName has not found
return field.GetValue(mystruct)?.ToString();
}
要做的最小更改是将实例 (mystruct
) 传递给 GetValue
方法:
private sting GetStingfromStruct(string variableName)
{
return (string)mystruct.GetType().GetField(variableName).GetValue(mystruct);
}
您还应该添加检查以确保字符串值实际上是字段名称等。
但我会附和其他评论和答案,除非您出于某种原因被迫使用结构,否则这不是一个好的设计。