instanceOf<U> method

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

Implementation

U instanceOf<U>(
  String typeName, {
  List positionalArguments = const [],
  Map<Symbol, dynamic> namedArguments = const {},
  Symbol constructorName = Symbol.empty,
}) {
  ClassMirror? typeMirror = currentMirrorSystem()
      .isolate
      .rootLibrary
      .declarations[Symbol(typeName)] as ClassMirror?;

  typeMirror ??= currentMirrorSystem()
      .libraries
      .values
      .where((lib) => lib.uri.scheme == "package" || lib.uri.scheme == "file")
      .expand((lib) => lib.declarations.values)
      .firstWhere(
        (decl) =>
            decl is ClassMirror &&
            MirrorSystem.getName(decl.simpleName) == typeName,
        orElse: () => throw ArgumentError(
          "Unknown type '$typeName'. Did you forget to import it?",
        ),
      ) as ClassMirror?;

  return typeMirror!
      .newInstance(
        constructorName,
        positionalArguments,
        namedArguments,
      )
      .reflectee as U;
}