예제 #1
0
        private static IntPtr GenerateObjectSchema(Type objectClass)
        {
            IntPtr objectSchemaPtr = IntPtr.Zero;

            if (ObjectSchemaCache.TryGetValue(objectClass, out objectSchemaPtr))
            {
                return(objectSchemaPtr); // use cached schema
            }

            objectSchemaPtr = NativeObjectSchema.create(objectClass.Name);
            ObjectSchemaCache[objectClass] = objectSchemaPtr;  // save for later lookup
            var propertiesToMap = objectClass.GetProperties(BindingFlags.Instance | BindingFlags.DeclaredOnly | BindingFlags.NonPublic | BindingFlags.Public)
                                  .Where(p =>
            {
                return(p.GetCustomAttributes(false).OfType <WovenPropertyAttribute>().Any());
            });

            foreach (var p in propertiesToMap)
            {
                var mapToAttribute = p.GetCustomAttributes(false).FirstOrDefault(a => a is MapToAttribute) as MapToAttribute;
                var propertyName   = mapToAttribute != null ? mapToAttribute.Mapping : p.Name;

                var objectIdAttribute = p.GetCustomAttributes(false).FirstOrDefault(a => a is ObjectIdAttribute);
                var isObjectId        = objectIdAttribute != null;

                var indexedAttribute = p.GetCustomAttributes(false).FirstOrDefault(a => a is IndexedAttribute);
                var isIndexed        = indexedAttribute != null;

                var isNullable = !(p.PropertyType.IsValueType ||
                                   p.PropertyType.Name == "RealmList`1") ||
                                 // IGNORING IList FOR NOW  p.PropertyType.Name == "IList`1") ||
                                 Nullable.GetUnderlyingType(p.PropertyType) != null;

                var objectType = "";
                if (!p.PropertyType.IsValueType && p.PropertyType.Name != "String")
                {
                    if (p.PropertyType.Name == "RealmList`1")  // IGNORING IList FOR NOW   || p.PropertyType.Name == "IList`1")
                    {
                        objectType = p.PropertyType.GetGenericArguments()[0].Name;
                    }
                    else
                    {
                        if (p.PropertyType.BaseType.Name == "RealmObject")
                        {
                            objectType = p.PropertyType.Name;
                        }
                    }
                }
                var columnType = p.PropertyType;
                NativeObjectSchema.add_property(objectSchemaPtr, propertyName, MarshalHelpers.RealmColType(columnType), objectType,
                                                MarshalHelpers.BoolToIntPtr(isObjectId), MarshalHelpers.BoolToIntPtr(isIndexed), MarshalHelpers.BoolToIntPtr(isNullable));
            }
            return(objectSchemaPtr);
        }