[英]Is there a Ruby equivalent for the typeof reserved word in C#?
我有一个C#方法,我需要从一个需要System.Type参数的Ruby中调用它。 在C#中是否有与typeof等效的Ruby? 调用在C#中看起来像这样...
var CustomClasInstance = Container.GetInstance(typeof(ICustomClass))
Object.class
或Object.type
都Object.type
您的需求。
另外,这两个方法是Object.is_a?
和Object.instance_of?
可以使用。 但是,它们不是100%相同。 仅当对象obj
被创建为myClass
类型的对象时,语句obj.instance_of?(myClass)
才返回true。 如果对象obj
是myClass
类,属于从myClass
继承的类或包含myClass
模块的类,则使用obj.is_a?(myClass)
将返回true。
例如:
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
由于您的C#方法需要非常特定的数据类型,因此我建议使用Object.instance_of?
。
除了检查Object#class(Object aka Base类的实例方法类)之外,您还可以
s.is_a? Thing
这将检查s中是否有Thing。
有关如何在Ruby中标识变量类型的参考,请参见http://www.techotopia.com/index.php/Understanding_Ruby_Variables#Identifying_a_Ruby_Variable_Type
如果您有名为s
变量,则可以通过调用来获取其类型
s.class
ICustomClass.to_clr_type
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.