From 187e6ae26d88ab0975de6011d00e41a846bcb6fa Mon Sep 17 00:00:00 2001 From: Ignacio Etcheverry Date: Tue, 5 Mar 2019 18:52:19 +0100 Subject: C#: Add marshalling support for IEnumerable and IDictionary Added constructor that takes IEnumerable for Array and IEnumerable for Array. Added constructor that takes IDictionary for Dictionary and IDictionary for Dictionary. --- modules/mono/glue/Managed/Files/Dictionary.cs | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) (limited to 'modules/mono/glue/Managed/Files/Dictionary.cs') diff --git a/modules/mono/glue/Managed/Files/Dictionary.cs b/modules/mono/glue/Managed/Files/Dictionary.cs index b89851f246..af1782b79a 100644 --- a/modules/mono/glue/Managed/Files/Dictionary.cs +++ b/modules/mono/glue/Managed/Files/Dictionary.cs @@ -40,6 +40,14 @@ namespace Godot.Collections safeHandle = new DictionarySafeHandle(godot_icall_Dictionary_Ctor()); } + public Dictionary(IDictionary dictionary) : this() + { + if (dictionary == null) + throw new NullReferenceException($"Parameter '{nameof(dictionary)} cannot be null.'"); + + MarshalUtils.IDictionaryToDictionary(dictionary, GetPtr()); + } + internal Dictionary(DictionarySafeHandle handle) { safeHandle = handle; @@ -255,6 +263,23 @@ namespace Godot.Collections objectDict = new Dictionary(); } + public Dictionary(IDictionary dictionary) + { + objectDict = new Dictionary(); + + if (dictionary == null) + throw new NullReferenceException($"Parameter '{nameof(dictionary)} cannot be null.'"); + + // TODO: Can be optimized + + IntPtr godotDictionaryPtr = GetPtr(); + + foreach (KeyValuePair entry in dictionary) + { + Dictionary.godot_icall_Dictionary_Add(godotDictionaryPtr, entry.Key, entry.Value); + } + } + public Dictionary(Dictionary dictionary) { objectDict = dictionary; -- cgit v1.2.3