【问题标题】:How do you add a string to a c# IList<string> from IronRuby?如何从 IronRuby 向 c# IList<string> 添加字符串?
【发布时间】:2011-03-17 15:19:06
【问题描述】:

当我尝试使用 ruby​​ 脚本修改 c# 字符串列表时,出现以下异常。

未处理的异常:System.ArgumentException:值“Scott”不是“System.String”类型,不能在此通用集合中使用。

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);
}

红宝石

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

【问题讨论】:

    标签: c# ironruby dynamic-language-runtime


    【解决方案1】:

    to_clr_string 添加到 ruby​​ 代码中:

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

    【讨论】:

    猜你喜欢
    • 2021-09-27
    • 2012-10-04
    • 1970-01-01
    • 2023-03-17
    • 1970-01-01
    • 2023-03-13
    • 2013-12-21
    • 1970-01-01
    • 2012-04-24
    相关资源
    最近更新 更多