C#中的保留字类型是否有Ruby等价物?

Chr*_*ris 33 c# ruby

我有一个C#方法,我需要从一个需要System.Type参数的Ruby调用.在C#中是否有类似于Ruby的Ruby?在C#中调用看起来像这样......

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

bta*_*bta 56

无论是Object.classObject.type应该做你所需要的.

此外,这两种方法Object.is_a?,并Object.instance_of?可以使用.但是,它们并非100%相同.obj.instance_of?(myClass)仅当对象obj被创建为类型对象时,该语句才会返回true myClass.使用obj.is_a?(myClass)该对象将返回true obj是一流的myClass,是从继承的类的myClass,或者具有该模块myClass包含在里面.

例如:

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
Run Code Online (Sandbox Code Playgroud)

由于您的C#方法需要非常具体的数据类型,我建议使用Object.instance_of?.


Rya*_*cox 40

除了检查Object#类(Object aka Base类上的实例方法类)之外,您还可以

s.is_a? Thing
Run Code Online (Sandbox Code Playgroud)

这将检查s在其祖先的任何地方是否有东西.


jsa*_*nen 5

有关如何在Ruby中识别变量类型的参考,请参阅 http://www.techotopia.com/index.php/Understanding_Ruby_Variables#Identifying_a_Ruby_Variable_Type

如果您有变量named s,则可以通过调用来检索它的类型

s.class
Run Code Online (Sandbox Code Playgroud)

  • 看起来s.Class更像CustomClassInstance.GetType()而不是真的喜欢typeof(CustomClass)......我错了吗? (2认同)