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