简体   繁体   English

在领域中使用反射(Android)

[英]Using Reflection with Realm (Android)

I made a switch to Realm last week. 我上周改用Realm。 Now, I've got this issue when using reflection: 现在,我在使用反射时遇到了这个问题:

Normally I'd use reflection to fetch field, which when available I'd use to replace url params in our API url f/e: blog/{blogId}/comments <- not an actual url but an example. 通常,我会使用反射来获取字段,如果可用,我会用它来替换API网址f / e中的网址参数:blog / {blogId} / comments <-不是实际网址,而是示例。

The code would check if there is a field called blogId, if yes it'll invoke the getter (getBlogId) and replace the value in the path. 该代码将检查是否存在一个名为blogId的字段,如果是,它将调用getter(getBlogId)并替换路径中的值。 Now with realm I get a "BlogRealmProxy" which does not contain the properties I was expecting on my RealmObject, it has fields like INDEX_BLOGID (or alike). 现在有了领域,我得到了一个“ BlogRealmProxy”,其中不包含我在RealmObject上期望的属性,它具有诸如INDEX_BLOGID(或类似)之类的字段。

Any idea how to use reflection still? 知道如何仍然使用反射吗? I need this to have generic functions available. 我需要具有通用功能。

Update: 更新:

Well I found a way, by doing something quite silly, any better options?: 好吧,我发现通过做一些愚蠢的事情,有更好的选择吗?:

Class clazz = obj.getClass();

    if (clazz.getName().endsWith("Proxy")) {
        clazz = clazz.getSuperclass();
    }

I'm 100% sure objects will never end with Proxy ;) 我100%确信对象永远不会以Proxy结尾;)

Well I found a way, by doing something quite silly, any better options?: 好吧,我发现通过做一些愚蠢的事情,有更好的选择吗?:

Class clazz = obj.getClass();

if (clazz.getName().endsWith("Proxy")) {
    clazz = clazz.getSuperclass();
}

I'm 100% sure objects will never end with Proxy ;) in my app. 我100%确信在我的应用中对象永远不会以Proxy;)结尾。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM