instanceOf<U> method

U instanceOf<U>(
  1. String typeName, {
  2. List positionalArguments = const [],
  3. required Map<Symbol, dynamic> namedArguments,
  4. Symbol? constructorName,
})
inherited

Implementation

U instanceOf<U>(String typeName,
    {List positionalArguments = const [],
    required Map<Symbol, dynamic> namedArguments,
    Symbol? constructorName}) {
  ClassMirror typeMirror =
      currentMirrorSystem().isolate.rootLibrary.declarations[Symbol(typeName)] as ClassMirror;
  // ignore: unnecessary_null_comparison
  if (typeMirror == null) {
    typeMirror = currentMirrorSystem()
        .libraries
        .values
        .where((lib) => lib.uri.scheme == "package" || lib.uri.scheme == "file")
        .expand((lib) => lib.declarations.values)
        .firstWhere(
            (decl) =>
                decl is DeclarationMirror && MirrorSystem.getName(decl.simpleName) == typeName,
            orElse: () => throw ArgumentError(
                "Unknown type '$typeName'. Did you forget to import it?")) as ClassMirror;
  }

  return typeMirror
      .newInstance(constructorName ?? const Symbol(""), positionalArguments, namedArguments)
      .reflectee as U;
}