如何获取特定MyData.Name值的密钥? 我不需要修改字典
这是给定的情况,我需要将“服务器”匹配为1公升
public class MyBase
{
protected struct MyData
{
public string Name;
}
}
public class MyClass : MyBase
{
private readonly Dictionary<ulong, MyBase.MyData> m_Data = new Dictionary<ulong, MyBase.MyData>();
public MyClass()
{
m_Data.Add(1L, new MyData { Name = "Server" });
}
}
public class MyMulti
{
public static MyClass myClass = new MyClass();
}
public class Test
{
Type MyDataType = typeof(MyBase).GetNestedType("MyData", BindingFlags.Instance | BindingFlags.NonPublic);
object m_Data = typeof(MyClass).GetField("m_Data", BindingFlags.Instance | BindingFlags.NonPublic).GetValue(MyMulti.myClass);
// How can i get the Key for a specific MyData.Name value?
}
答案 0 :(得分:0)
我实际上只是找到了一种简单的方法:
var m_Data = typeof(MyClass).GetField("m_Data", BindingFlags.Instance | BindingFlags.NonPublic).GetValue(MyMulti.myClass) as IDictionary;
foreach (var pair in m_Data.Keys)
{
Console.WriteLine(pair + " = " + m_Data[pair].GetType().GetField("Name").GetValue(m_Data[pair]));
}