开发者

Is there a Ruby equivalent for the typeof reserved word in C#?

I have a C# method that I need to call from a piece of Ruby that requires a System.Type argument. Is there a Rub开发者_如何转开发y equivalent to typeof in C#? The call will look something like this in C# ...

var CustomClasInstance = Container.GetInstance(typeof(ICustomClass))


Either Object.class or Object.type should do what you need.

Also, the two methods Object.is_a? and Object.instance_of? can be used. However, they are not 100% identical. The statement obj.instance_of?(myClass) will return true only if object obj was created as an object of type myClass. Using obj.is_a?(myClass) will return true if the object obj is of class myClass, is of a class that has inherited from myClass, or has the module myClass included in it.

For example:

x = 1
x.class                   => Fixnum
x.instance_of? Integer    => false
x.instance_of? Numeric    => false
x.instance_of? Fixnum     => true
x.is_a? Integer           => true
x.is_a? Numeric           => true
x.is_a? Fixnum            => true

Since your C# method requires a very specific data type, I would recommend using Object.instance_of?.


In addition to checking Object#class (the instance method class on the Object aka Base class), you could

s.is_a? Thing

this will check to see if s has Thing anywhere in its ancestry.


For a reference on how to identify variable types in Ruby, see http://www.techotopia.com/index.php/Understanding_Ruby_Variables#Identifying_a_Ruby_Variable_Type

If you have variable named s, you can retrieve the type of it by invoking

s.class


ICustomClass.to_clr_type
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜