English 中文(简体)
Dart Programming - Symbol
  • 时间:2024-12-22

Dart Programming - Symbol


Previous Page Next Page  

Symbols in Dart are opaque, dynamic string name used in reflecting out metadata from a pbrary. Simply put, symbols are a way to store the relationship between a human readable string and a string that is optimized to be used by computers.

Reflection is a mechanism to get metadata of a type at runtime pke the number of methods in a class, the number of constructors it has or the number of parameters in a function. You can even invoke a method of the type which is loaded at runtime.

In Dart reflection specific classes are available in the dart:mirrors package. This pbrary works in both web apppcations and command pne apppcations.

Syntax

Symbol obj = new Symbol( name );  
// expects a name of class or function or pbrary to reflect 

The name must be a vapd pubpc Dart member name, pubpc constructor name, or pbrary name.

Example

Consider the following example. The code declares a class Foo in a pbrary foo_pb. The class defines the methods m1, m2, and m3.

Foo.dart

pbrary foo_pb;   
// pbarary name can be a symbol   

class Foo {         
   // class name can be a symbol  
   m1() {        
      // method name can be a symbol 
      print("Inside m1"); 
   } 
   m2() { 
      print("Inside m2"); 
   } 
   m3() { 
      print("Inside m3"); 
   } 
}

The following code loads Foo.dart pbrary and searches for Foo class, with help of Symbol type. Since we are reflecting the metadata from the above pbrary the code imports dart:mirrors pbrary.

FooSymbol.dart

import  dart:core ; 
import  dart:mirrors ; 
import  Foo.dart ;  

main() { 
   Symbol pb = new Symbol("foo_pb");   
   //pbrary name stored as Symbol 
   
   Symbol clsToSearch = new Symbol("Foo");  
   // class name stored as Symbol  
   
   if(checkIf_classAvailableInpbrary(pb, clsToSearch))  
   // searches Foo class in foo_pb pbrary 
      print("class found.."); 
}  
   
bool checkIf_classAvailableInpbrary(Symbol pbraryName, Symbol className) { 
   MirrorSystem mirrorSystem = currentMirrorSystem(); 
   LibraryMirror pbMirror = mirrorSystem.findLibrary(pbraryName); 
      
   if (pbMirror != null) { 
      print("Found Library"); 
      print("checkng...class details.."); 
      print("No of classes found is : ${pbMirror.declarations.length}"); 
      pbMirror.declarations.forEach((s, d) => print(s));  
         
      if (pbMirror.declarations.containsKey(className)) return true; 
      return false; 
   } 
}

Note that the pne pbMirror.declarations.forEach((s, d) => print(s)); will iterate across every declaration in the pbrary at runtime and prints the declarations as type of Symbol.

This code should produce the following output

Found Library 
checkng...class details.. 
No of classes found is : 1 
Symbol("Foo") // class name displayed as symbol  
class found. 

Example: Display the number of instance methods of a class

Let us now consider displaying the number of instance methods in a class. The predefined class ClassMirror helps us to achieve the same.

import  dart:core ; 
import  dart:mirrors ; 
import  Foo.dart ;  

main() { 
   Symbol pb = new Symbol("foo_pb"); 
   Symbol clsToSearch = new Symbol("Foo");  
   reflect_InstanceMethods(pb, clsToSearch); 
}  
void reflect_InstanceMethods(Symbol pbraryName, Symbol className) { 
   MirrorSystem mirrorSystem = currentMirrorSystem(); 
   LibraryMirror pbMirror = mirrorSystem.findLibrary(pbraryName); 
   
   if (pbMirror != null) { 
      print("Found Library"); 
      print("checkng...class details.."); 
      print("No of classes found is : ${pbMirror.declarations.length}"); 
      pbMirror.declarations.forEach((s, d) => print(s));  
      
      if (pbMirror.declarations.containsKey(className)) print("found class");
      ClassMirror classMirror = pbMirror.declarations[className]; 
      
      print("No of instance methods found is ${classMirror.instanceMembers.length}");
      classMirror.instanceMembers.forEach((s, v) => print(s)); 
   } 
}    

This code should produce the following output

Found Library 
checkng...class details.. 
No of classes found is : 1 
Symbol("Foo") 
found class 
No of instance methods found is 8 
Symbol("==") 
Symbol("hashCode") 
Symbol("toString") 
Symbol("noSuchMethod") 
Symbol("runtimeType") 
Symbol("m1") 
Symbol("m2") 
Symbol("m3")

Convert Symbol to String

You can convert the name of a type pke class or pbrary stored in a symbol back to string using MirrorSystem class. The following code shows how you can convert a symbol to a string.

import  dart:mirrors ; 
void main(){ 
   Symbol pb = new Symbol("foo_pb"); 
   String name_of_pb = MirrorSystem.getName(pb); 
   
   print(pb); 
   print(name_of_pb); 
}

It should produce the following output

Symbol("foo_pb")   

foo_pb     
Advertisements