How to check if a mixin object class included in Dart?

How to check if mixin object class is included? For example:

class AClass extends Object with MyMixin {}
class BClass extends Object              {}

classIncludesMixin(new AClass(), 'MyMixin'); // => true
classIncludesMixin(new BClass(), 'MyMixin'); // => false

What should be in this method classIncludesMixin()for it to work?

+4
source share
1 answer

You can simply use type checking o is MyMixin(which will also be true for inheritance and implementation).

If you really need to check out the mixin clause, you should use dart: mirror :

bool classIncludesMixin(o, String mixinName) {
  var c = reflect(o).type;
  while (c != null) {
    var m = c.mixin;
    if (c != m && m.simpleName == new Symbol(mixinName)) return true;
    c = c.superclass;
  }
  return false;
}
+6
source

All Articles