Skip to content

Commit 904ae0c

Browse files
committed
TypeConversion - Improve ToNative type checking when converting from Object to CefValue
Previously you had to used a fixed type, now the type checking is more flexible. Move the Dictionary check to before the collection check as a dictionary is technically a collection Resolves #2625
1 parent e7b192e commit 904ae0c

File tree

2 files changed

+33
-16
lines changed

2 files changed

+33
-16
lines changed

CefSharp.Core/Internals/TypeConversion.h

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -150,31 +150,34 @@ namespace CefSharp
150150
{
151151
cefValue->SetDouble(Convert::ToDouble(value));
152152
}
153-
else if (type == List<Object^>::typeid)
153+
else if (System::Collections::IDictionary::typeid->IsAssignableFrom(type))
154154
{
155-
auto list = safe_cast<List<Object^>^>(value);
156-
auto cefList = CefListValue::Create();
157-
for (int i = 0; i < list->Count; i++)
158-
{
159-
auto value = list[i];
160-
SerializeV8Object(cefList, i, value);
161-
}
162-
cefValue->SetList(cefList);
163-
}
164-
else if (type == Dictionary<String^, Object^>::typeid)
165-
{
166-
auto dictionary = safe_cast<Dictionary<String^, Object^>^>(value);
155+
auto dictionary = (System::Collections::IDictionary^) value;
167156
auto cefDictionary = CefDictionaryValue::Create();
168157

169-
for each (KeyValuePair<String^, Object^>^ entry in dictionary)
158+
for each (System::Collections::DictionaryEntry entry in dictionary)
170159
{
171-
auto key = StringUtils::ToNative(entry->Key);
172-
auto value = entry->Value;
160+
auto key = StringUtils::ToNative(Convert::ToString(entry.Key));
161+
auto value = entry.Value;
173162
SerializeV8Object(cefDictionary, key, value);
174163
}
175164

176165
cefValue->SetDictionary(cefDictionary);
177166
}
167+
else if (System::Collections::IEnumerable::typeid->IsAssignableFrom(type))
168+
{
169+
auto enumerable = (System::Collections::IEnumerable^) value;
170+
auto cefList = CefListValue::Create();
171+
172+
int i = 0;
173+
for each (Object^ arrObj in enumerable)
174+
{
175+
SerializeV8Object(cefList, i, arrObj);
176+
177+
i++;
178+
}
179+
cefValue->SetList(cefList);
180+
}
178181

179182
return cefValue;
180183
}

CefSharp.Example/Handlers/BrowserProcessHandler.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.
44

55
using System;
6+
using System.Collections.Generic;
67
using System.Diagnostics;
78
using System.Threading.Tasks;
89
using CefSharp.SchemeHandler;
@@ -73,6 +74,19 @@ void IBrowserProcessHandler.OnContextInitialized()
7374
//The default is true, you can change to false to disable
7475
context.SetPreference("webkit.webprefs.plugins_enabled", true, out errorMessage);
7576

77+
//string error;
78+
//var dicts = new List<string> { "en-GB", "en-US" };
79+
//var success = context.SetPreference("spellcheck.dictionaries", dicts, out error);
80+
81+
//The no-proxy-server flag is set in CefExample.cs class, you'll have to remove that before you can test
82+
//this code out
83+
//var v = new Dictionary<string, string>
84+
//{
85+
// ["mode"] = "fixed_servers",
86+
// ["server"] = "scheme://host:port"
87+
//};
88+
//success = context.SetPreference("proxy", v, out errorMessage);
89+
7690
//It's possible to register a scheme handler for the default http and https schemes
7791
//In this example we register the FolderSchemeHandlerFactory for https://cefsharp.example
7892
//Best to include the domain name, so only requests for that domain are forwarded to your scheme handler

0 commit comments

Comments
 (0)