C
C#2y ago
artem

How can I work around this exception?

5 Replies
artem
artemOP2y ago
Note: I cannot just write where T : IRealmObject, because it will broke interface implementation
phaseshift
phaseshift2y ago
That's all you can do
artem
artemOP2y ago
can't I show that T is acceptable Type somehow else? like cast it
phaseshift
phaseshift2y 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
artemOP2y 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
Want results from more Discord servers?
Add your server