如何在VB.net中获取当前类或方法中的变量名称类型和值? [英] How to get the variable names types, and values in the current class or Method in VB.net?

查看:104
本文介绍了如何在VB.net中获取当前类或方法中的变量名称类型和值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨我正在开发一个项目,我的班级必须执行用户提供的 VB 代码,为了简单起见,我正在尝试重新创建自己的 eval 函数,我正在使用我在网上找到的以下代码来执行此任务

Hi I am working on a project where my class has to execute VB code provided by the user, to make it simple i am trying to recreate my own eval function, i am using the following code i found on the web to do this task

Imports Microsoft.VisualBasic
Imports System
Imports System.Text
Imports System.CodeDom.Compiler
Imports System.Reflection
Imports System.IO
Namespace PAB.Util
    Public Class EvalProvider

        Public Function VS_Eval(ByVal vbCode As String) As Object

            Dim c As VBCodeProvider = New VBCodeProvider
            Dim icc As ICodeCompiler = c.CreateCompiler()
            Dim cp As CompilerParameters = New CompilerParameters

            cp.ReferencedAssemblies.Add("system.dll")
            cp.ReferencedAssemblies.Add("system.xml.dll")
            cp.ReferencedAssemblies.Add("system.data.dll")
            ' Sample code for adding your own referenced assemblies
            'cp.ReferencedAssemblies.Add("c:\yourProjectDir\bin\YourBaseClass.dll")
            'cp.ReferencedAssemblies.Add("YourBaseclass.dll")
            cp.CompilerOptions = "/t:library"
            cp.GenerateInMemory = True
            Dim sb As StringBuilder = New StringBuilder("")
            sb.Append("Imports System" & vbCrLf)
            sb.Append("Imports System.Xml" & vbCrLf)
            sb.Append("Imports System.Data" & vbCrLf)
            sb.Append("Imports System.Data.SqlClient" & vbCrLf)
            sb.Append("Namespace PAB  " & vbCrLf)
            sb.Append("Class PABLib " & vbCrLf)

            sb.Append("public function  EvalCode() as Object " & vbCrLf)

            'sb.Append("YourNamespace.YourBaseClass thisObject = New YourNamespace.YourBaseClass()")
            sb.Append(vbCode & vbCrLf)
            sb.Append("End Function " & vbCrLf)
            sb.Append("End Class " & vbCrLf)
            sb.Append("End Namespace" & vbCrLf)
            Debug.WriteLine(sb.ToString()) ' look at this to debug your eval string
            Dim cr As CompilerResults = icc.CompileAssemblyFromSource(cp, sb.ToString())
            Dim a As System.Reflection.Assembly = cr.CompiledAssembly
            Dim o As Object
            Dim mi As MethodInfo
            o = a.CreateInstance("PAB.PABLib")
            Dim t As Type = o.GetType()
            mi = t.GetMethod("EvalCode")
            Dim s As Object
            s = mi.Invoke(o, Nothing)
            Return s



        End Function
    End Class
End Namespace

代码的问题在于它无法访问任何变量或值,所以我决定动态获取变量名称、值类型和类型,并在动态创建的类中重新创建它们.

The problem with code is that it can't access any variables or there values, so i have decided to get the variable names, there values types and there types dynamically and recreate them in the class that is being created dynamically.

请提出一种方法来获取当前类或方法中的类型和值的变量名称,以便我可以重新创建它们,并执行用户传递的代码,用户知道当前类或方法中有哪些变量以及有数据类型,但他不知道那里的值,因为它们可能已经改变,所以他无法初始化它们.

Please suggest a way to get the variable names there types and values in the current class or method, so that i can recreate them, and execute the user passed code, the user knows what variables are in the current class or method and there datatypes but he don't know there values as they may have changed, so he can't initialize them.

有没有办法做到这一点,这段代码会在asp.net页面的page_load事件中调用,用户传递的代码存储在作为参数传递的变量vbCode中.

Is there a way to do this, this code will be called in an asp.net page on page_load event, the code passed by the user is stored in the variable vbCode that is passed as a parameter.

如果有其他方法可以做到这一点,请提出

If there is any other method to do this, please suggest

谢谢

推荐答案

我终于找到了如何获取当前类实例的变量和值的解决方案.

I have finally found the solution of how to get the variables and values of the current instance of a class.

使用System.Reflection;很容易实现,你只需要写下面这行:

using System.Reflection; it is very easy to achieve, all you have to write is the following line:

Dim fields As FieldInfo() = Me.GetType().GetFields()

For Each fld As FieldInfo In fields
    Dim name As String = fld.Name
    Dim value = fld.GetValue(Me)
    Dim typ As Type = fld.FieldType
Next

注意:它只适用于公共变量

Note : It only works for public variables

这篇关于如何在VB.net中获取当前类或方法中的变量名称类型和值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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