如何在haxe中检测属性是否为数字类型?

时间:2013-12-19 17:49:41

标签: haxe

我需要检测对象的属性是否为数字(Int或Float) 我所拥有的是对象引用,属性名称为字符串

这是我的实现,compoent是对象引用,但它不起作用

public function IsNumeric():Bool
    {
        if (Std.is(Type.typeof(Reflect.getProperty(compoent, propertyName)), Int)) return true;
        if (Std.is(Type.typeof(Reflect.getProperty(compoent, propertyName)), Float)) return true;
        return false;
    }

任何人都可以提供帮助吗?

1 个答案:

答案 0 :(得分:4)

它比您尝试的更简单:Std.is( value, type )

class Test {
  static function main(){
    js.Lib.alert( Std.is("string", Int) );
    js.Lib.alert( Std.is(0, Int) );
    js.Lib.alert( Std.is(0.3, Int) );
    js.Lib.alert( Std.is("string", Float) );
    js.Lib.alert( Std.is(0, Float) );
    js.Lib.alert( Std.is(0.3, Float) );
  }
}

请参阅http://try.haxe.org/#6A9Bd