【问题标题】:IronRuby needing a .NET Interface as an argumentIronRuby 需要 .NET 接口作为参数
【发布时间】:2014-02-05 16:59:47
【问题描述】:

我正在使用 Revit API 来使用 IronRuby 进行可扩展存储,我认为我的问题是 IronRuby 问题。我正在尝试在 IronRuby 中重现 Jeremy Tannik 的 Revit 博客中的这个 C# 示例:

  // create a field to store a string map
  FieldBuilder fieldBuilder =
    schemaBuilder.AddMapField( "StringMap", typeof( string ), typeof( string ) );

. . .

  // set the value for this entity
  IDictionary<string, string> stringMap = new Dictionary<string, string>();
  stringMap.Add( "key1", "value1" );
  stringMap.Add( "key2", "value2" );
  entity.Set<IDictionary<string, string>>( field, stringMap );

当我尝试在 IronRuby 中做我认为相同的事情时:

# create a field to store a string map
schema_builder.AddMapField( "manually_checked", System::String.to_clr_type, System::Boolean.to_clr_type )

. . .

# set the value for this entity
xDict = Dictionary[System::String,System::Boolean].new
IDictionary_module = xDict.class.ancestors.detect  { |i| i.to_s == 
    "System::Collections::Generic::IDictionary[System::String, System::Boolean]" }
xDict.Add( "blah", true )

# all three of these fail with the same error
x.Set( "manually_checked", xDict )
x.method(:Set).of(Dictionary[System::String,System::Boolean]).call( "manually_checked", xDict )
x.method(:Set).of(IDictionary_module).call( "manually_checked", xDict )

我调用 Set() 的所有三种方式都失败并显示相同的消息:

不支持的类型: System.Collections.Generic.Dictionary`2[[System.String, mscorlib, 版本=4.0.0.0,文化=中性, PublicKeyToken=b77a5c561934e089],[System.Boolean, mscorlib, 版本=4.0.0.0,文化=中性,PublicKeyToken=b77a5c561934e089]] (无效操作异常)

这让我觉得错误信息中的“Dictionary`2”是对xDict.class.ancestors[2]的引用,也就是IDictionary[System::String,System::Boolean]在祖先数组。

所以,我的问题是,是否有 IronRuby 语法允许这样做,或者因为 Ruby 模块和 .NET 接口之间的差异而遇到阻碍?

【问题讨论】:

    标签: interface module clr ironruby


    【解决方案1】:

    您的字典 Add() 使用 IronRuby 字符串作为关键参数,但字典需要 CLR 字符串类型。向 Ruby 字符串添加“to_clr_string”调用,使其符合预期。

    【讨论】:

    • 谢谢。不幸的是,IronRuby 的有效死亡导致我将所有内容都转换为 C#。 C'est la vie。
    猜你喜欢
    • 2018-05-12
    • 1970-01-01
    • 1970-01-01
    • 2021-04-19
    • 1970-01-01
    • 1970-01-01
    • 2018-10-21
    • 1970-01-01
    • 2021-09-30
    相关资源
    最近更新 更多