c# - Is there a way to pass an argument to the is operator? -
i trying find alternative following can take advantage of is
operator.
public bool isoftype(type type) { return this._item.gettype() == type; }
something similar following, not compile.
public bool isoftype(type type) { return this._item type; }
i think you're looking type.isassignablefrom
:
public bool isoftype(type type) { return _item != null && type.isassignablefrom(_item.gettype()); }
or if can make method generic, that's simpler can use is
type parameter:
public bool isoftype<t>() { return _item t; }
edit: noted in wim's answer, there's type.isinstanceof
makes non-generic method simpler:
public bool isoftype(type type) { return type.instanceof(_item); }
Comments
Post a Comment