Come si fa ad aggiungere una stringa a un C # IList da IronRuby?
-
27-10-2019 - |
Domanda
ottengo la seguente eccezione quando si tenta di utilizzare uno script Ruby per modificare un elenco di stringhe di C #.
Eccezione non gestita: System.ArgumentException:. Il valore "Scott" non è di tipo "System.String" e non può essere utilizzato in questa raccolta generica
c #
IList<string> names = new List<string>();
names.Add("scott");
names.Add("jason");
ScriptRuntime runtime = IronRuby.Ruby.CreateRuntime();
ScriptEngine engine = runtime.GetEngine("IronRuby");
ScriptScope scope = engine.CreateScope();
scope.SetVariable("names", names);
engine.ExecuteFile("test.rb", scope);
foreach (var name in names)
{
Console.WriteLine(name);
}
ruby ??
names.map! { |item| item.capitalize}
Soluzione
Add to_clr_string
al codice Ruby:
names.map! { |item| item.capitalize.to_clr_string }
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow