简体   繁体   中英

How to Convert a C# Dictionary in the code to a Powershell object?

Can any one help me? I have C# code that builds a dictionary and I need to convert that dictionary into PowerShell object. The object will then be pass it to a PowerShell script.

My Dictionary

Dictionary<string, Dictionary<string, string>>

Current versions (v3/v4) should be able to use the Dictionary directly but I don't remember how well v2 handled generics. Anyway, here is some pseudo-code (not guaranteed to compile) to convert to a hashtable of hashtables:

var hashtable = new Hashtable();
foreach (var kvPair in dict) {
    var hashtableInner = new Hashtable();
    foreach (var kvPairInner in kvPair.Value) {
        hashtableInner.Add(kvPairInner.Key, kvPairInner.Value);
    }

    hashtable.Add(kvPair.Key, hashtableInner);
}

# Pass hashtable to PowerShell as even v2 understands .NET hashtables

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM