瀏覽代碼

Merge pull request #9 from northroom/master

修复mongo.bson 在iOS下的AOT问题
tanghai 8 年之前
父節點
當前提交
88cf0b92f6

+ 5 - 1
Unity/Assets/Plugins/MongoDB/MongoDB.Bson/Serialization/BsonClassMap.cs

@@ -1274,9 +1274,12 @@ namespace MongoDB.Bson.Serialization
                 Expression body;
                 var bindingFlags = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance;
                 var classTypeInfo = _classType.GetTypeInfo();
-                var defaultConstructor = classTypeInfo.GetConstructors(bindingFlags)
+                ConstructorInfo defaultConstructor = classTypeInfo.GetConstructors(bindingFlags)
                     .Where(c => c.GetParameters().Length == 0)
                     .SingleOrDefault();
+                #if AOT
+                _creator = () => defaultConstructor.Invoke(null);
+                #else
                 if (defaultConstructor != null)
                 {
                     // lambdaExpression = () => (object) new TClass()
@@ -1295,6 +1298,7 @@ namespace MongoDB.Bson.Serialization
 
                 var lambdaExpression = Expression.Lambda<Func<object>>(body);
                 _creator = lambdaExpression.Compile();
+                #endif
             }
             return _creator;
         }

+ 26 - 0
Unity/Assets/Plugins/MongoDB/MongoDB.Bson/Serialization/BsonMemberMap.cs

@@ -598,6 +598,24 @@ namespace MongoDB.Bson.Serialization
 
         private Func<object, object> GetGetter()
         {
+            #if AOT
+            PropertyInfo propertyInfo = _memberInfo as PropertyInfo;
+            if (propertyInfo != null)
+            {
+                MethodInfo getMethodInfo = propertyInfo.GetGetMethod();
+                if (getMethodInfo == null)
+                {
+                    var message = string.Format(
+                        "The property '{0} {1}' of class '{2}' has no 'get' accessor.",
+                        propertyInfo.PropertyType.FullName, propertyInfo.Name, propertyInfo.DeclaringType.FullName);
+                    throw new BsonSerializationException(message);
+                }
+                return (obj) => { return getMethodInfo.Invoke(obj, null); };
+            }
+
+            FieldInfo fieldInfo = _memberInfo as FieldInfo;
+            return (obj) => { return fieldInfo.GetValue(obj); };
+            #else
             var propertyInfo = _memberInfo as PropertyInfo;
             if (propertyInfo != null)
             {
@@ -625,10 +643,17 @@ namespace MongoDB.Bson.Serialization
             );
 
             return lambdaExpression.Compile();
+            #endif
         }
 
+
         private Action<object, object> GetPropertySetter()
         {
+            #if AOT
+            var propertyInfo = (PropertyInfo) _memberInfo;
+
+            return (obj, value) => { propertyInfo.SetValue(obj, value); };
+            #else
             var propertyInfo = (PropertyInfo)_memberInfo;
             var setMethodInfo = propertyInfo.SetMethod;
             if (IsReadOnly)
@@ -653,6 +678,7 @@ namespace MongoDB.Bson.Serialization
             );
 
             return lambdaExpression.Compile();
+            #endif
         }
 
         private void ThrowFrozenException()

+ 8 - 0
Unity/Assets/Scripts/Base/Object/Entity.cs

@@ -211,5 +211,13 @@ namespace Model
 				}
 			}
 		}
+
+		#if AOT
+		private void AvoidAot()
+		{
+			EnumSerializer<EntityType> e = new EnumSerializer<EntityType>();
+		
+		}
+		#endif
 	}
 }