根据名称(以字符串形式)获取变量的值 [英] Get Value of a variable based on the name(as a string)
问题描述
我想基于其名称将声明的变量的值作为字符串".
我有一个变量名和默认值存储在数据库中,想要在运行时将其与变量的实际值进行比较,如果它已更改,则将新值保存回数据库中.我可以使用select case语句来做到这一点,但是每个表单有10个变量,并且有50多个表单.
示例:
数据库字段为:
VariableName(作为字符串)|值(作为字符串)
_____________________________________________
"bTest" | 真实"
_________________________________________
有一个局部变量:
I want to get the value of a declared variable based on its Name as a "String".
I have a variable name and default value stored in database and want to compare that to the actual value of the variable during runtime and if it has changed save the new value back to the database. I can do this with a select case statement but there are 10 variables per form and over 50 forms.
Example:
database fields are:
VariableName (as string) | Value (as string)
_____________________________________________
"bTest" | "True"
_________________________________________
There is a Local Variable:
Dim bTest as Boolean = false
我想这样做. (我知道没有"GetVariable")
I would like to do this. (I know there is no "GetVariable")
If DataTable.Rows(i).Item("Value") = GetVariable("DataTable.Rows(i).Item("VarialbeName")).Value Then
End If
可能是一个更简单的示例.
A simpler example may be this.
If DataTable.Rows(i).Item("Value") = GetVariable("bTest")).Value Then
我可以用这种方法来做,但这是一堆代码:
I can do it this way but it is a ton of code:
Select Case DataTable.Rows(i).Item("VarialbeName").ToString
case "bTest"
If bTest = CType(DataTable.Rows(i).Item("Value"),Boolean)
'Do some Task
End if
End Select
推荐答案
什么是变量"?您可以使用某些类或结构的成员,字段或属性来执行此操作,而不管它是否为私有.您将需要知道如何使用Reflection并拥有一个声明您的成员的结构类变量,仅对成员的引用是不够的,并且不是必需的.
这是模式:
- 您有一个类或结构的实例
Instance
,它们声明您需要的成员.使用GetType()
获取其类型T
.它将返回类型为System.Type
的变量并分配给T
.- 对于字段,请按名称调用
T.GetField(String, BindingFlags)
.如果字段是公共字段,则不需要第二个参数,否则,请使用System.Reflection.BindingFlags | System.Reflection.NonPublic
.如果成员是静态成员,也可以是ORSystem.Reflection.Static
.- 对于属性,请按名称调用
T.GetProperty(String, BindingFlags)
.使用第二个参数,与上面的项目相同.- 检查
GetField
或GetProperty
返回的值是否不是Nothing
.如果它是Nothing
,则没有这样的成员,这是错误的.假设我们得到了类型为FieldIndo
的字段元数据F
或类型为PropertyIndo
的属性元数据P
.- 对于字段,请调用
F.GetValue(Instance, Nothing)
;它将返回所需的对象O
.对于静态字段,两个参数都是Nothing
.- 对于属性,请调用
F.GetValue(Instance, Nothing)
;它将返回所需的对象O
.对于静态字段,两个参数均为Nothing
.- 您从
GetValue
获得了对象O
.将其强制转换为必需的类型.对于值类型,这将导致取消装箱.- 利润!
What''s the "variable"? You can do it with the member of some class or structure, a field or property, no matter if it is private or not. You will need to know how to use Reflection and have a variable of the class of structure declaring your member, having only a reference to your member is not enough, and not required.
This is the schema:
- You have an instance
Instance
of class or structure which declare a member you need. Obtain its typeT
usingGetType()
. It will return variable of the typeSystem.Type
and assign toT
.- For a field, call
T.GetField(String, BindingFlags)
by name. If a field is public, second parameter is not needed, if not, useSystem.Reflection.BindingFlags | System.Reflection.NonPublic
. If the member is static, also ORSystem.Reflection.Static
.- For a property, call
T.GetProperty(String, BindingFlags)
by name. Use the second parameter is needed, same is in the above item.- Check up if the value returned by
GetField
orGetProperty
is notNothing
. If it isNothing
, there is no such member, something is wrong. Consider we got field meta-dataF
of the typeFieldIndo
or property meta-dataP
of the typePropertyIndo
.- For field, call
F.GetValue(Instance, Nothing)
; it will return required objectO
. For a static field, both parameters areNothing
.- For property, call
F.GetValue(Instance, Nothing)
; it will return required objectO
. For a static field, both parameters areNothing
.- You got an object
O
fromGetValue
. Cast it to required type. For value types it will cause un-boxing.- PROFIT!
您可以使用反射. 此 [
You could use Reflection. This[^] discussion might give you some ideas.
这篇关于根据名称(以字符串形式)获取变量的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!