【问题标题】:Xamarin/Realm - The type cannot be used as type parameter 'T' in the generic type or methodXamarin/Realm - 该类型不能用作泛型类型或方法中的类型参数“T”
【发布时间】:2017-12-18 21:21:57
【问题描述】:

我已经研究了一段时间,并且知道我缺少一些非常简单的东西。该代码在前一个开发人员离开之前可以使用,但现在我需要在 iPad 上发布,它无法正常工作。我正在使用 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);
    }
}

错误 CS0311:类型“VSTSQL.Data.Mobile.Models.PatientSummary”不能用作泛型类型或方法“Realm.All()”中的类型参数“T”。没有从“PatientSummary”到“Realms.RealmObject”的隐式引用转换。 (CS0311)

【问题讨论】:

  • 这段代码以前真的有效吗?看起来All&lt;T&gt;() 方法需要Realms.RealmObject 类型的任何东西,而PatienSummary 显然不需要。
  • 我所知道的是,某些版本的代码已部署给测试人员,所以它在某处工作。
  • 那您如何了解版本历史并查找更改?
  • All&lt;T&gt; 一直有一个 where T : RealmObject 通用约束,所以即使使用旧版本的 Realm,这段代码也不可能在当前形式下工作。

标签: c# generics xamarin.forms realm


【解决方案1】:

您的对象必须继承自 RealmObject。否则,它将无法直接存储并从领域数据库中访问。您可以在https://realm.io/docs/dotnet/latest/#models 找到更多信息。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多