lvjincheng 3 năm trước cách đây
mục cha
commit
5d11c8bf78
1 tập tin đã thay đổi với 12 bổ sung4 xóa
  1. 12 4
      Assets/ThirdAssets/JCUnityLib/CustomJson.cs

+ 12 - 4
Assets/ThirdAssets/JCUnityLib/CustomJson.cs

@@ -74,8 +74,12 @@ namespace JCUnityLib
                 FieldInfo targetField = fromObjectType.GetField(propertyName, BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public);
                 if (targetField == null) {
                     PropertyInfo propertyInfo = fromObjectType.GetProperty(propertyName, BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public);
-                    JToken propertyValue = ToJTokenByImporter(propertyInfo.GetValue(fromObject));
-                    target[propertyName] = propertyValue;
+                    if (propertyInfo == null) {
+                        throw new Exception($"获取PropertyInfo[{propertyName}]失败");
+                    } else {
+                        JToken propertyValue = ToJTokenByImporter(propertyInfo.GetValue(fromObject));
+                        target[propertyName] = propertyValue;
+                    }
                 } else {
                     JToken propertyValue = ToJTokenByImporter(targetField.GetValue(fromObject));
                     target[propertyName] = propertyValue;
@@ -136,8 +140,12 @@ namespace JCUnityLib
                 FieldInfo targetField = targetType.GetField(fieldName, BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public);
                 if (targetField == null) {
                     PropertyInfo propertyInfo = targetType.GetProperty(fieldName, BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public);
-                    object fieldValue = ParseByExporter(fromJson[fieldName], propertyInfo.PropertyType);
-                    propertyInfo.SetValue(target, fieldValue);
+                    if (propertyInfo == null) {
+                        throw new Exception($"获取PropertyInfo[{fieldName}]失败");
+                    } else {
+                        object fieldValue = ParseByExporter(fromJson[fieldName], propertyInfo.PropertyType);
+                        propertyInfo.SetValue(target, fieldValue);
+                    }
                 } else {
                     object fieldValue = ParseByExporter(fromJson[fieldName], targetField.FieldType);
                     targetField.SetValue(target, fieldValue);