IronRuby需要.NET接口作为参数

时间:2014-02-05 16:59:47

标签: interface module clr ironruby

我正在使用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,   Version = 4.0.0.0,Culture = neutral,PublicKeyToken = b77a5c561934e089]]   (InvalidOperationException异常)

这让我觉得错误消息中的“Dictionary`2”是对xDict.class.ancestors [2]的引用,它是IDictionary [System :: String,System :: Boolean]的位置。祖先阵列。

所以,我的问题是,是否有一个IronRuby语法允许这个,或者由于Ruby模块和.NET接口之间的差异而对showstopper产生了影响?

1 个答案:

答案 0 :(得分:0)

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