internal static RealmSchema CreateFromObjectStoreSchema(Native.Schema nativeSchema) { var objects = new ObjectSchema[nativeSchema.objects_len]; for (var i = 0; i < nativeSchema.objects_len; i++) { var objectSchema = Marshal.PtrToStructure <Native.SchemaObject>(IntPtr.Add(nativeSchema.objects, i * Native.SchemaObject.Size)); var builder = new ObjectSchema.Builder(objectSchema.name); for (var n = objectSchema.properties_start; n < objectSchema.properties_end; n++) { var nativeProperty = Marshal.PtrToStructure <Native.SchemaProperty>(IntPtr.Add(nativeSchema.properties, n * Native.SchemaProperty.Size)); builder.Add(new Property { Name = nativeProperty.name, Type = nativeProperty.type, ObjectType = nativeProperty.object_type, IsPrimaryKey = nativeProperty.is_primary, IsNullable = nativeProperty.is_nullable, IsIndexed = nativeProperty.is_indexed }); } objects[i] = builder.Build(); } return(new RealmSchema(objects)); }
public void TestSchemas() { // :code-block-start: schema_property // By default, all loaded RealmObject classes are included. // Use the RealmConfiguration when you want to // construct a schema for only specific C# classes: var config = new RealmConfiguration { Schema = new[] { typeof(ClassA), typeof(ClassB) } }; // More advanced: construct the schema manually var manualConfig = new RealmConfiguration { Schema = new RealmSchema.Builder { new ObjectSchema.Builder("ClassA", isEmbedded: false) { Property.Primitive("Id", RealmValueType.Guid, isPrimaryKey: true), Property.Primitive("LastName", RealmValueType.String, isNullable: true, isIndexed: true) } } }; // Most advanced: mix and match var mixedSchema = new ObjectSchema.Builder(typeof(ClassA)); mixedSchema.Add(Property.FromType <int>("ThisIsNotInTheCSharpClass")); // `mixedSchema` now has all of the properties of the ClassA class // and an extra integer property called "ThisIsNotInTheCSharpClass" var mixedConfig = new RealmConfiguration { Schema = new[] { mixedSchema.Build() } }; // :code-block-end: Assert.AreEqual(2, config.Schema.Count); Assert.AreEqual(1, manualConfig.Schema.Count); Assert.AreEqual(1, mixedConfig.Schema.Count); ObjectSchema foo; mixedConfig.Schema.TryFindObjectSchema("ClassA", out foo); if (foo != null) { Property newProp; Assert.IsTrue(foo.TryFindProperty("ThisIsNotInTheCSharpClass", out newProp)); } }