One of the reflection applications: according to the control name, the property name is used to assign the value.

xiaoxiao2021-03-06  80

One of the reflection applications: according to the control name, the property name is used to assign the value.

'

You must reference the namespace System.Reflection, System.comPonentModel

'

The following is based on the control name and attribute name.

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

Oral

_ Bindingflags.instance

Oral

Bindingflags.public

Oral

Bindingflags.instance)

IF

NOT

MyfieldInfo

IS

Nothing

THEN

DIM

PROPERTIES

AS

PropertyDescriptorCollection

=

TypedScriptor.getProperties (MyTYPE)

DIM

MyProperty

AS

PropertyDescriptor

=

Properties.Find (PropertyName,

False

)

IF

NOT

MyProperty

IS

Nothing

THEN

DIM

Ctr

AS

Object

Ctr

=

MyfieldInfo.getValue (ClassInstance)

Try

Result

=

MyProperty.GetValue (CTR)

Catch

EX

AS

EXCEPTION

Msgbox

(EXMESSAGE)

End

Try

End

IF

End

IF

Return

Result

END FUNCTION

'

The following assignments according to the control name and attribute name

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_

Oral

Bindingflags.instance

Oral

Bindingflags.public

Oral

Bindingflags.instance)

'

Add "_" this is a special

IF

NOT

MyfieldInfo

IS

Nothing

THEN

DIM

PROPERTIES

AS

PropertyDescriptorCollection

=

TypedScriptor.getProperties (MyTYPE)

DIM

MyProperty

AS

PropertyDescriptor

=

Properties.Find (PropertyName, False

)

'

This is set to true if you don't have to write.

IF

NOT

MyProperty

IS

Nothing

THEN

DIM

Ctr

AS

Object

Ctr

=

MyfieldInfo.getValue (ClassInstance)

'

Acquisition of control instances

Try

MyProperty.SetValue (CTR, VALUE) RESULT

=

Ctr

Catch

EX

AS

EXCEPTION

Msgbox

(EXMESSAGE)

End

Try

End

IF

End

IF

Return

Result

END FUNCTION

'

transfer

'

Label1.Text = TextBox1.text = textBox2 is implemented below

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

End Sub

转载请注明原文地址:https://www.9cbs.com/read-120828.html

New Post(0)