How do you add a string to a c# IList from

2019-08-29 07:20发布

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条回答
Lonely孤独者°
2楼-- · 2019-08-29 07:49

Add to_clr_string to the ruby code:

names.map! { |item| item.capitalize.to_clr_string }
查看更多
登录 后发表回答