List all custom data stored in AppDomain
Asked Answered
G

3

8

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 ?

Germen answered 7/12, 2012 at 12:59 Comment(2)
why do not just store all keys information (previously set with SetData) in some collection and after query GetData fro every key in that collection ?Riddance
I was looking for a solution, where the process doesn't need to use a specific implementation. Since I don't think it's possible, extension method for AppDomain which is storing keys passed. Tks for your reply. If you have another clue, don't hesitate.Germen
H
7
        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());
                }
            }


        }
Hagans answered 7/12, 2012 at 15:41 Comment(0)
A
3

Based on Frank59's answer but a bit more concise:

var appDomain = AppDomain.CurrentDomain;
var flags = BindingFlags.NonPublic | BindingFlags.Instance;
var fieldInfo = appDomain.GetType().GetField("_LocalStore", flags);
if (fieldInfo == null)
    return;
var localStore = fieldInfo.GetValue(appDomain) as Dictionary<string, object[]>;
if (localStore == null)
    return;
foreach (var key in localStore.Keys)
{
    var nonNullValues = localStore[key].Where(v => v != null);
    Console.WriteLine(key + ": " + string.Join(", ", nonNullValues));
}
Althing answered 7/3, 2013 at 17:35 Comment(0)
Y
1

Same solution, but as an F# extension method. May not need the null check. https://gist.github.com/ctaggart/30555d3faf94b4d0ff98

type AppDomain with
    member x.LocalStore
        with get() =
            let f = x.GetType().GetField("_LocalStore", BindingFlags.NonPublic ||| BindingFlags.Instance)
            if f = null then Dictionary<string, obj[]>()
            else f.GetValue x :?> Dictionary<string, obj[]>

let printAppDomainObjectCache() =
    for KeyValue(k,v) in AppDomain.CurrentDomain.LocalStore do
        printfn "%s" k
Yelena answered 29/10, 2014 at 17:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.