根据名称(以字符串形式)获取变量的值 [英] Get Value of a variable based on the name(as a string)

查看:104
本文介绍了根据名称(以字符串形式)获取变量的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想基于其名称将声明的变量的值作为字符串".

我有一个变量名和默认值存储在数据库中,想要在运行时将其与变量的实际值进行比较,如果它已更改,则将新值保存回数据库中.我可以使用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.如果成员是静态成员,也可以是OR System.Reflection.Static.
  • 对于属性,请按名称调用T.GetProperty(String, BindingFlags).使用第二个参数,与上面的项目相同.
  • 检查GetFieldGetProperty返回的值是否不是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 type T using GetType(). It will return variable of the type System.Type and assign to T.
  • For a field, call T.GetField(String, BindingFlags) by name. If a field is public, second parameter is not needed, if not, use System.Reflection.BindingFlags | System.Reflection.NonPublic. If the member is static, also OR System.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 or GetProperty is not Nothing. If it is Nothing, there is no such member, something is wrong. Consider we got field meta-data F of the type FieldIndo or property meta-data P of the type PropertyIndo.
  • For field, call F.GetValue(Instance, Nothing); it will return required object O. For a static field, both parameters are Nothing.
  • For property, call F.GetValue(Instance, Nothing); it will return required object O. For a static field, both parameters are Nothing.
  • You got an object O from GetValue. 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆