列出存储在 AppDomain 中的所有自定义数据 [英] List all custom data stored in AppDomain

查看:15
本文介绍了列出存储在 AppDomain 中的所有自定义数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

In order to store the state of processes when an error occured, I would like to list all (custom) data stored in AppDomain (by SetData). The LocalStore property is private and AppDomain class not inheritable. Is there any way to enumerate those data ?

推荐答案

        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());
                }
            }


        }

这篇关于列出存储在 AppDomain 中的所有自定义数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆