如何把map 轉化為指定物件呢?
先看測試:
@test
public void test_convertmap2obj() throws instantiationexception, illegalacces***ception, securityexception, nosuchfieldexception, illegalargumentexception
執行結果:
黃威 21
1970-01-01 08:16:40.255
reflecthwutils.convertmap2obj實現如下:
/***
* convert map to object ,see setobjectvalue(obj, map)
*@param map : key是物件的成員變數,其value就是成員變數的值
*@param clazz
*@return
*@throws instantiationexception
*@throws illegalacces***ception
*@throws securityexception
*@throws nosuchfieldexception
*@throws illegalargumentexception
*/public
static object convertmap2obj(mapmap,class clazz) throws instantiationexception, illegalacces***ception, securityexception, nosuchfieldexception, illegalargumentexception
object obj=clazz.newinstance();
setobjectvalue(obj, map);
/*for(iterator it=map.entryset().iterator();it.hasnext();)*/
return obj;
}/***
* 利用反射設定物件的屬性值. 注意:屬性可以沒有setter 方法.
* *@param obj
*@param params
*@throws securityexception
*@throws nosuchfieldexception
*@throws illegalargumentexception
*@throws illegalacces***ception
*/public
static
void
setobjectvalue(object obj, mapparams)
throws securityexception, nosuchfieldexception,
illegalargumentexception, illegalacces***ception
if (valuewidget.isnullorempty(params))
class<?> clazz = obj.getclass();
for (iterator it = params.entryset().iterator(); it.hasnext();)
field name = getspecifiedfield(clazz, key);
if (name != null)
}}
依賴的方法:
/***
* 利用反射設定物件的屬性值. 注意:屬性可以沒有setter 方法.
* *@param obj
*@param params
*@throws securityexception
*@throws nosuchfieldexception
*@throws illegalargumentexception
*@throws illegalacces***ception
*/public
static
void
setobjectvalue(object obj, mapparams)
throws securityexception, nosuchfieldexception,
illegalargumentexception, illegalacces***ception
if (valuewidget.isnullorempty(params))
class<?> clazz = obj.getclass();
for (iterator it = params.entryset().iterator(); it.hasnext();)
field name = getspecifiedfield(clazz, key);
if (name != null) }}
/***
* get specified field
* *@param clazz
*@param fieldname
*@return
*/public
static field getspecifiedfield(class<?> clazz, string fieldname)
try catch (nosuchfieldexception e)
return getspecifiedfield(superclass2/*
* may be null if it is
* object .
*/, fieldname);
// e.printstacktrace();
}return f;
}
跨瀏覽器實現placeholder
map轉化為物件
如何把map 轉化為指定物件呢?先看測試 test public void test convertmap2obj throws instantiationexception,illegalacces ception,securityexception,nosuchfieldexception,il...
將物件轉化為DataTable
imports system.collections.generic imports system.reflection public class tablebuilder of t private selectinfos as list of propertyinfo private propin...
將view物件轉化為Bitmap
如題 private bitmap getviewbitmap view view view.builddrawingcache bitmap cachebitmap view.getdrawingcache if cachebitmap null bitmap bitmap bitmap.crea...