.NET
Microsoft Technologies based on the .NET software framework.
3,779 questions
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
l
let's say we have a class "MyClass", now i want to put all static menbers into a Dictionary ,
use name "a", "b", "c", "d" as key, use "aaa", "bbb", "ccc", ”ddd“ as value.
Can it be done ?
Hello @兰树豪,
Can you try below code:
Type myType = typeof(MyClass);
// Get all static members of MyClass
FieldInfo[] staticFields = myType.GetFields(BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic);
// Display the names of all static members
Dictionary<string,string> map = new Dictionary<string,string>();
foreach (FieldInfo field in staticFields)
{
object value = field.GetValue(null);
map.Add(field.Name, value.ToString());
}
foreach(string key in map.Keys) {
Console.WriteLine("{0}={1}",key, map[key].ToString());
}