Hi Everyone,
I am trying to feed a dictionary that comes out of a python node (type: DesignScript.Builtin.Dictionary) into a zerotouch node that I am developing in C#.
How can I cast or convert this input to a normal C# dictionary that I can then use in my code?
I think you’d need to get the key/value pairs from the DesignScript dictionary and put them in a new Dictionary<string, object>
in your ZT node.
1 Like
Thank you for the reply.
But how do I access the pair in my code?
Using dictionary type as the input type does not work.
you can just reference the Dynamo dictionary type, it’s in the designscriptbuiltin.dll which is part of the dynamocore nuget package.
2 Likes
it also looks like IDictionary
should be converted automatically, or Dictionary
for that matter so a code repro case would be useful.
: base(type)
{
this.primitiveMarshaler = primitiveMarshaler;
}
public override StackValue Marshal(object obj, ProtoCore.Runtime.Context context, Interpreter dsi, ProtoCore.Type type)
{
List<string> keys = null;
List<object> values = null;
if (obj is IDictionary)
{
var dict = (IDictionary) obj;
keys = dict.Keys.Cast<string>().ToList();
values = dict.Values.Cast<object>().ToList();
}
else if (obj is Dictionary)
{
var dict = (Dictionary) obj;
keys = dict.Keys.ToList();
values = dict.Values.ToList();
1 Like
I was able to use System.Collections.IDictionary and it worked fine.
Thank you!
1 Like
I solve that using this methodology.
Your Node Input must be an IDictionary
.
Then, inside in your Node logic you need to convert the IDictionary to a strong typed Dictionary.
This allows you to consume your input data as you wish.
See images below showing how I implement that.
Node Method
Generic extension method to convert IDictionary to the strong typed Dictionary
Dynamo Graph
1 Like