Java 反射,使用 GetDeclaredField 时忽略大小写 [英] Java Reflection, Ignore case when using GetDeclaredField

查看:160
本文介绍了Java 反射,使用 GetDeclaredField 时忽略大小写的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个名为myfield"的字符串字段的类,并使用反射来获取该字段,我发现 Object.getClass().getDeclaredField("myfield"); 区分大小写,例如,如果我使用 Object.getClass().getDeclaredField("MyField");

Let's say I have a class with a string field named "myfield", and use reflection to get the field, I've found that Object.getClass().getDeclaredField("myfield"); is case sensitive, it will throw an NoSuchFieldException if I for example use Object.getClass().getDeclaredField("MyField");

有什么办法可以解决吗?强制它忽略大小写?

Is there any way around it? forcing it to ignore case?

谢谢

推荐答案

只需使用 Class.getDeclaredFields() 并查看自己执行不区分大小写匹配的结果.

Just use Class.getDeclaredFields() and look through the results performing a case-insensitive match yourself.

这篇关于Java 反射,使用 GetDeclaredField 时忽略大小写的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆