/ ** * Methodnname: getReflection <br> * Description : 解析 resxml 在通过反射设置对象属性值 * utilisateur: liqijing * Date: 2015-7-19 下午 12:42:55 * @param clzzname * @param respxml * @return * @throws classnotfoundException * @throws documentException * @throws illégalargumentException * @Throws InstantiationException * @Throws SecurityException * @throws NosuchFieldException * / public static <T> objet getReflection (String clzzname, string respxml) lance classnotfoundException, documentException, illégalargumentException, illégalccessException, instanciationException, SecurityException, NosuchFieldException {objet O = Class.forname (clzzName) .newInstance (); class clz = class.forname (clzzName) .newinstance (). GetClass (); document doc = null; doc = documenthelper.parsext (respxml); element el = doc.getrootelement (); pour (Field f: clz.getDeclatedFields () {Itère it = el.elementIterator (); while (it.hasnext ()) {element elt = (element) it.next (); if (f.getName (). equals (elt.getName ())) {f = clz.getDeclaredField (elt.getName ()); f.setaccessible (true); f.set (o, elt.get.get (); o;}Chers tous:
在开发过程中有类似的需求通过反射动态设置属性值 , 希望有帮助。也很高兴与大家分享 , 谢谢。
以上这篇 Java 通过反射机制动态设置对象属性值的方法就是小编分享给大家的全部内容了 , 希望能给大家一个参考 , 也希望大家多多支持武林网。