反射應用之一:根據控制項名、屬性名進行取值和賦值
'必須引用命名空間system.reflection,system.***ponentmodel
'以下根據控制項名和屬性名取值
public
function
getvaluecontrolproperty(
byval
classinstance
asobject
, byval
controlname
asstring
, byval
propertyname
asstring
) as
object
dimresult
asobject
dimmytype
astype
=classinstance.gettype
dimmyfieldinfo
asfieldinfo
=mytype.getfield("_
"&
controlname, bindingflags.nonpublic
or_
bindingflags.instance
orbindingflags.public
orbindingflags.instance)
ifnot
myfieldinfo
isnothing
then
dimproperties
aspropertydescriptorcollection
=typedescriptor.getproperties(mytype)
dimmyproperty
aspropertydescriptor
=properties.find(propertyname,
false
) if
notmyproperty
isnothing
then
dimctr
asobject
ctr
=myfieldinfo.getvalue(classinstance)
tryresult
=myproperty.getvalue(ctr)
catch
ex as
exception
msgbox
(ex.message)
endtry
endif
endif
return
result
end function
'以下根據控制項名和屬性名賦值
public
function
setvaluecontrolproperty(
byval
classinstance
asobject
, byval
controlname
asstring
, byval
propertyname
asstring
, byval
value
asobject
) as
object
dimresult
asobject
dimmytype
astype
=classinstance.gettype
dimmyfieldinfo
asfieldinfo
=mytype.getfield("_
"&
controlname, bindingflags.nonpublic _
orbindingflags.instance
orbindingflags.public
orbindingflags.instance)
'加"_"這個是特要緊的
ifnot
myfieldinfo
isnothing
then
dimproperties
aspropertydescriptorcollection
=typedescriptor.getproperties(mytype)
dimmyproperty
aspropertydescriptor
=properties.find(propertyname,
false) '
這裡設為true就不用區分大小寫了
ifnot
myproperty
isnothing
then
dimctr
asobject
ctr
=myfieldinfo.getvalue(classinstance)
'取得控制項例項
trymyproperty.setvalue(ctr, value)
result
=ctr
catch
ex as
exception
msgbox
(ex.message)
endtry
endif
endif
return
result
end function
'呼叫
'以下實現label1.text=textbox1.text,label2.text=textbox2
private
subbutton1_click(
byval
sender
assystem.object,
byval
e as
system.eventargs)
handles
button1.click
dimi
asinteger
fori =1
to2
me.setvaluecontrolproperty(
me,
"label"&
i.tostring,
"text
", getvaluecontrolproperty(
me,
"textbox"&
i.tostring,
"text
"))
next
i end sub
反射應用之一 根據控制項名 屬性名進行取值和賦值
必須引用命名空間system.reflection,system.ponentmodel 以下根據控制項名和屬性名取值 public function getvaluecontrolproperty byval classinstance as object,byval controlname as...
reduce 一知半解 一 根據id合併兩個陣列
日常遇到的,同id的兩陣列合併成一陣列的問題 arr.reduce function prev,cur,index,arr init arr 表示原陣列 prev 表示上一次呼叫 時的返回值,或者初始值 init cur 表示當前正在處理的陣列元素 index 表示當前正在處理的陣列元素的索引,若提...
棧的應用之一 括號匹配
括號匹配所用演算法 核心 遍歷整個字串,若遇到了左括號,將其下標進棧 若遇到了右括號,則將棧頂元素出棧 根據後進先出,棧頂元素即為與當前括號匹配的括號 若棧空,則無括號與當前括號匹配。當遍歷一遍後,若棧中還有元素,則說明左括號多,無右括號與其匹配。include include includeusi...