How to get values stored in Scripting.Dictionary instance.
-
Hi, I am using a vender-provided COM object in a C# Windows Forms application to extract proprietary data format from an AD attribute. The data is stored in an associative array. The problem I am having is retrieving the items that the object returns as its parameters. For example, upon binding to an AD container object, the object creates an instance of type Scripting.Dictionary within COM object, and exposes this instance as a property called "Parameters." I've tried casting to a ListDictionary, HybridDictionary, you name it. Nothing seems to work except IEnumerator. The problem I'm having is how to retrieve the items for each key in the Scripting.Dictionary collection. I'm able to retrieve the names of the keys, but now I simply want to get the values for each key, called "Items". I've wrapped the Scripting.Dictionary class into a .NET assembly using the TLBIMP libary and included it in my project. Here's a snippet of my code so far: // AD path to bind to string ADsPath = "LDAP://CN=Some List,OU=Distribution Lists,DC=DomainA,DC=CorpA,DC=com"; // instantiate the vender COM object AGQueryClass ag = new AGQueryClass(); // bind to the AD path ag.Set(ADsPath); //cast ag.Parameters to Scripting.Dictionary instance Scripting.DictionaryClass sd = (Scripting.DictionaryClass) ag.Parameters; // get collection enumerator IEnumerator IEn = sd.GetEnumerator(); // array to hold keys Array ary = Array.CreateInstance(typeof(object), sd.Count); IEn.Reset(); IEn.MoveNext(); // populate the array with key names for (int i=0; i
-
Hi, I am using a vender-provided COM object in a C# Windows Forms application to extract proprietary data format from an AD attribute. The data is stored in an associative array. The problem I am having is retrieving the items that the object returns as its parameters. For example, upon binding to an AD container object, the object creates an instance of type Scripting.Dictionary within COM object, and exposes this instance as a property called "Parameters." I've tried casting to a ListDictionary, HybridDictionary, you name it. Nothing seems to work except IEnumerator. The problem I'm having is how to retrieve the items for each key in the Scripting.Dictionary collection. I'm able to retrieve the names of the keys, but now I simply want to get the values for each key, called "Items". I've wrapped the Scripting.Dictionary class into a .NET assembly using the TLBIMP libary and included it in my project. Here's a snippet of my code so far: // AD path to bind to string ADsPath = "LDAP://CN=Some List,OU=Distribution Lists,DC=DomainA,DC=CorpA,DC=com"; // instantiate the vender COM object AGQueryClass ag = new AGQueryClass(); // bind to the AD path ag.Set(ADsPath); //cast ag.Parameters to Scripting.Dictionary instance Scripting.DictionaryClass sd = (Scripting.DictionaryClass) ag.Parameters; // get collection enumerator IEnumerator IEn = sd.GetEnumerator(); // array to hold keys Array ary = Array.CreateInstance(typeof(object), sd.Count); IEn.Reset(); IEn.MoveNext(); // populate the array with key names for (int i=0; i
Try this:
using System;
using System.Collections;
using System.Collections.Specialized;public class ScriptingUtils
{
public static IDictionary ConvertDictionary(Scripting.IDictionary dict)
{
HybridDictionary ret = new HybridDictionary(dict.Count);foreach(object k in dict) { // Can't pass k as ref, because it's read-only object key = k; // Can't pass a ref to an indexer // Use the accessor method instead object value = dict.get\_Item(ref key); ret.Add(key, value); } return ret; }
}
You can then change your code to:
using System;
using System.Collections;
...
// AD path to bind to
string ADsPath = "LDAP://CN=Some List,OU=Distribution Lists,DC=DomainA,DC=CorpA,DC=com";// instantiate the vender COM object AGQueryClass ag = new AGQueryClass(); // bind to the AD path ag.Set(ADsPath); IDictionary dict = ScriptingUtils.ConvertDictionary( (Scripting.IDictionary)ag.Parameters); object\[\] keys = new object\[dict.Count\]; object\[\] values = new object\[dict.Count\]; int index = 0; foreach(DictionaryEntry entry in dict) { // Do something with the entry here... keys\[index\] = entry.Key; values\[index\] = entry.Value; index++; }
...
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
-
Try this:
using System;
using System.Collections;
using System.Collections.Specialized;public class ScriptingUtils
{
public static IDictionary ConvertDictionary(Scripting.IDictionary dict)
{
HybridDictionary ret = new HybridDictionary(dict.Count);foreach(object k in dict) { // Can't pass k as ref, because it's read-only object key = k; // Can't pass a ref to an indexer // Use the accessor method instead object value = dict.get\_Item(ref key); ret.Add(key, value); } return ret; }
}
You can then change your code to:
using System;
using System.Collections;
...
// AD path to bind to
string ADsPath = "LDAP://CN=Some List,OU=Distribution Lists,DC=DomainA,DC=CorpA,DC=com";// instantiate the vender COM object AGQueryClass ag = new AGQueryClass(); // bind to the AD path ag.Set(ADsPath); IDictionary dict = ScriptingUtils.ConvertDictionary( (Scripting.IDictionary)ag.Parameters); object\[\] keys = new object\[dict.Count\]; object\[\] values = new object\[dict.Count\]; int index = 0; foreach(DictionaryEntry entry in dict) { // Do something with the entry here... keys\[index\] = entry.Key; values\[index\] = entry.Value; index++; }
...
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
Yes, that did it! Thank you Richard.