C
C#17mo ago
artem

How can I work around this exception?

5 Replies
artem
artem17mo ago
Note: I cannot just write where T : IRealmObject, because it will broke interface implementation
phaseshift
phaseshift17mo ago
That's all you can do
artem
artem17mo ago
can't I show that T is acceptable Type somehow else? like cast it
phaseshift
phaseshift17mo ago
That's whatwhere is for Where is compile time. You're trying to fix a compile problem with a run time check. No bueno You can change All<T> to All<IRealm...> Then also use OfType I guess That would not be efficient, I'm presuming
artem
artem17mo ago
public IQueryable<T> Select<T>()
{
if (!typeof(T).IsSubclassOf(typeof(IRealmObject)))
throw new TypeAccessException($"{typeof(T).Name} is not a subclass of {typeof(IRealmObject).Name}");

return realm?.All<IRealmObject>().OfType<T>();
}
public IQueryable<T> Select<T>()
{
if (!typeof(T).IsSubclassOf(typeof(IRealmObject)))
throw new TypeAccessException($"{typeof(T).Name} is not a subclass of {typeof(IRealmObject).Name}");

return realm?.All<IRealmObject>().OfType<T>();
}
At least it works this way