有人能解释为什么IronRuby中的DateTime是Object[]示例代码吗
IronRuby 0.9.1.0 on .NET 2.0.50727.4927
Copyright (c) Microsoft Corporation. All rights reserved.
>>> require 'System'
=> true
>>> t = System::DateTime.Now
=> Thu Dec 03 15:32:42 +05:00 2009
>>> t.is_a?(Time)
=> true
>>> t.is_a?(System::DateTime)
=> true
>>> t.is_a?(System::Object)
=> true
>>> t.is_a?(System::Object[])
=> true
>>> t.is_a?(System::Decimal)
=> false发布于 2009-12-29 12:17:36
对泛型类型使用[]是一种合理的语法,但是您当前看到的行为(在非泛型类型上使用[] )是一个开放的bug:http://ironruby.codeplex.com/WorkItem/View.aspx?WorkItemId=3355
在Ruby中,方括号仅用于对数组进行索引,而不是定义类型数组...因为不需要定义Ruby数组的类型。Ruby允许对任何对象重载任何[],并且Ruby的Class对象对[]没有意义,所以我们定义了[]来表示获取泛型类型。请记住,of方法是要使用的首选方法;[]实际上只是为了方便起见,而且语法与IronPython类似。
发布于 2009-12-03 19:22:21
因为System::Object[]不是真正的数组类型。(t.is_a::DateTime[])也将返回true。
我认为这里发生的事情是,IronRuby将方括号视为空的泛型类型指示符(因为创建泛型类型使用相同的语法,例如,System::Collections::Generic::ListString.new).
正确的方法如下:
IronRuby 0.9.3.0 on .NET 2.0.50727.4927
Copyright (c) Microsoft Corporation. All rights reserved.
>>> t = System::DateTime.new
=> 01/01/0001 00:00:00
>>> t.is_a? System::Array.of(System::Object)
=> falsehttps://stackoverflow.com/questions/1839101
复制相似问题