3

我知道可以从这个链接中显示的符号创建一个实例:

从 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(''), []);

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

DSON 源代码

4

1 回答 1

1

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

另请参阅:如何调用类表单 dart 库字符串或文件

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

于 2014-03-28T06:07:36.560 回答