Search code examples
c#genericsxamarin.formsrealm

Xamarin/Realm - The type cannot be used as type parameter 'T' in the generic type or method


I've been looking at this for sometime now and know there is something really simple that I'm missing. The code worked before the previous developer left but now that I need to put out on the iPad, it's not working. I'm using Visual Studio for Mac.

public class DemoPatientService : BaseService, IPatientService
{
    public ObservableCollection<PatientSummary> MockPatientSummary;

    public DemoPatientService()
    {
        MockPatientSummary = realm.All<PatientSummary>().ToObservableCollection();
    }
}

public class PatientSummary : IPatientSummary
{
    public string PatientID { get; set;}
}

public interface IPatientSummary
{
    string PatientID
}

public class BaseService
{
    protected readonly Realm realm;

    public BaseService()
    {
        var config = new RealmConfiguration();
            config = new RealmConfiguration(MYWSettings.Instance.DemoDatabasePath);

        realm = Realm.GetInstance(config);
    }
}

Error CS0311: The type 'VSTSQL.Data.Mobile.Models.PatientSummary' cannot be used as type parameter 'T' in the generic type or method 'Realm.All()'. There is no implicit reference conversion from 'PatientSummary' to 'Realms.RealmObject'. (CS0311)


Solution

  • Your object must inherit from RealmObject. Otherwise, it will not be able to be directly stored and accessible from the Realm Database. You can find more information at https://realm.io/docs/dotnet/latest/#models.