/// <summary> /// Gets or creates a <see cref="Permission"/> instance for the named role /// on the object. /// </summary> /// <remarks> /// This function should be used in preference to manually querying for the /// applicable Permission as it ensures that there is exactly one Permission for /// the given Role on the object, merging duplicates or creating and adding new ones /// as needed. /// <para/> /// The given object must have a <c>IList<Permission></c> property defined on it. /// If more than one such property exists, the first one will be used. /// </remarks> /// <param name="role">The <see cref="PermissionRole"/> associated with that Permission.</param> /// <param name="obj"> /// The <see cref="RealmObject"/> inheritor whose permissions this /// <see cref="Permission"/> instance manipulates. /// </param> /// <returns> /// A <see cref="Permission"/> instance that can be used to inspect or modify the object /// permissions of that <see cref="PermissionRole"/>. /// </returns> public static Permission Get(PermissionRole role, RealmObject obj) { var permissionType = typeof(Permission).GetTypeInfo().GetMappedOrOriginalName(); var prop = obj.ObjectSchema.FirstOrDefault(o => o.Type == (PropertyType.Array | PropertyType.Object) && o.ObjectType == permissionType); if (prop.Name == null) { throw new ArgumentException("The given object doesn't have an IList<Permission> property.", nameof(obj)); } var permissions = obj.GetListValue <Permission>(prop.Name); return(Get(role, permissions)); }