How do you add a string to a c# IList<string> from IronRuby?

前提是你 提交于 2019-12-13 04:59:43

问题


I get the following exception when I try to use a ruby script to modify a list of c# strings.

Unhandled Exception: System.ArgumentException: The value "Scott" is not of type "System.String" and cannot be used in this generic collection.

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}

回答1:


Add to_clr_string to the ruby code:

names.map! { |item| item.capitalize.to_clr_string }


来源:https://stackoverflow.com/questions/5341111/how-do-you-add-a-string-to-a-c-sharp-iliststring-from-ironruby

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!