从字符串/符号创建对象的实例而不使用Dart中的Class?

时间:2014-03-28 04:49:17

标签: json reflection dart dart-mirrors

我知道可以通过此链接中显示的符号创建实例:

Create an instance of an object from a String in Dart?

但是这对我不起作用,因为我想做的是创建一个没有类的实例。

导致此问题是因为我有一个带有内部列表的类:

class MyNestedClass {
  String name;
}

class MyClass {
  int i, j;
  String greeting;
  List<MyNestedClass> myNestedClassList;
}

我想将地图转换为此类:

   {
        "greeting": "hello, there",
        "i": 3,
        "j": 5,
        "myNestedClassList": [
           {
             "name": "someName1"
           },{
             "name": "someName2"
           }
        ]
    }

现在我正在做这样的事情:

 static void jsonToObject(String jsonString, Object object) {
    Map jsonMap = JSON.decode(jsonString); //Convert the String to a map
    mapToObject(jsonMap, object); //Convert the map to a Object
  }

  static void mapToObject(Map jsonMap, Object object) {
    InstanceMirror im = reflect(object); //get the InstanceMirror of the object
    ClassMirror cm = im.type; //get the classMirror of the object

    jsonMap.forEach((fieldNameStr, fieldValue) { // For each element in the jsonMap
      var fieldName = new Symbol(fieldNameStr); // convert the fieldName in the Map to String

      if (isPrimitive(fieldValue)) { // if fieldValue is primitive (num, string, or bool
              im.setField(fieldName, fieldValue); //set the value of the field using InstanceMirror
      } else if (fieldValue is List) { // else if the fieldValue is a list
          ClassMirror listCm = (cm.declarations[fieldName] as VariableMirror).type; //get the class mirror of the list
          var listReflectee = listCm.newInstance(const Symbol(''), []).reflectee; //create an instance of the field

          for(var element in fieldValue) { //for each element in the list
            if(!isPrimitive(element)) { // if the element in the list is a map (i.e not num, string or bool)
               var listType = listCm.typeArguments[0]; //get the TypeMirror of the list (i.e MyNestedClass from List<MyNestedClass>)

               //This is the line that doesn't work correctly
               //It should be something like:
               //
               //     ClassMirror.fromSymbol(listType.simpleName).newInstance(const Symbol(''), []);
               //
               var listObject = (listType as ClassMirror).newInstance(const Symbol(''), []); //create an instance of the specified listType

               mapToObject(element, listObject); //convert the element to Object
            }
            listReflectee.add(element); //add the element to the list
          };
      } else { //else (the field value is a map
        ClassMirror fieldCm = (cm.declarations[fieldName] as VariableMirror).type; // get the field ClassMirror from the parent declarations
        var reflectee = fieldCm.newInstance(const Symbol(''), []).reflectee; //create an instance of the field
        mapToObject(fieldValue, reflectee); // convert the fieldValue, which is a map, to an object
        im.setField(fieldName, reflectee); // set the value of the object previously converted to the corresponding field
      }
    });
  }

正如您所看到的那样,实际上不起作用的行是:

var listType = listCm.typeArguments[0]; //get the TypeMirror of the list (i.e MyNestedClass from List<MyNestedClass>)
var listObject = (listType as ClassMirror).newInstance(const Symbol(''), []); //create an instance of the specified listType

因为他们正在localClassMirror而不是MyNestedClass上创建实例。我正在寻找类似于以下的方法:

 ClassMirror.fromSymbol(listType.simpleName).newInstance(const Symbol(''), []);

您可以在下一个网址中看到完整的源代码:

DSON Source Code

1 个答案:

答案 0 :(得分:1)

如果您拥有该类的完全限定名称,则应该能够使用libraryMirror找到该类型,然后它应该类似于您创建实例的链接问题。 我自己还没有这样做,也没有手头的代码。

另见:how to invoke class form dart library string or file

另一种方法是在应用程序初始化时创建一个映射,您可以使用其名称或ID注册支持的类型,并在此映射中查找类型(这就像在Go中完成的那样)< / p>