为了在发生错误时存储进程状态,我想列出存储在AppDomain中的所有(自定义)数据(通过SetData)。
LocalStore属性是私有(private)的,AppDomain类不可继承。
有什么办法枚举那些数据吗?

最佳答案

        AppDomain domain = AppDomain.CurrentDomain;
        domain.SetData("testKey", "testValue");

        FieldInfo[] fieldInfoArr = domain.GetType().GetFields(BindingFlags.NonPublic | BindingFlags.Instance);
        foreach (FieldInfo fieldInfo in fieldInfoArr)
        {

            if (string.Compare(fieldInfo.Name, "_LocalStore", true) != 0)
                continue;
            Object value = fieldInfo.GetValue(domain);
            if (!(value is Dictionary<string,object[]>))
                return;
            Dictionary<string, object[]> localStore = (Dictionary<string, object[]>)value;
            foreach (var item in localStore)
            {
                Object[] values = (Object[])item.Value;
                foreach (var val in values)
                {
                    if (val == null)
                        continue;
                    Console.WriteLine(item.Key + " " + val.ToString());
                }
            }


        }

关于c# - 列出存储在AppDomain中的所有自定义数据,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/13763605/

10-10 11:26